Cannot Call External Javascript From a Text Link

Discussion in 'Javascript' started by dennise9@earthlink.net, Apr 5, 2006.

  1. Guest

    My exeternal JS works fine when the call is placed in the page head or
    body.

    But if I call the same javascript from a (clicked) text link on the
    page, the script throws a JS error when it executes.

    Help! I can't figure this out.

    The external JS file is "members.js". Here's the code:

    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">
    </head>
    <body bgcolor="#FFFFFF">
    <script Language="JavaScript"></script>
    <table width="90%" border="0" align="center">
    <tr>
    <td>
    <div align="center"><a href="home.htm"
    target="mainFrame">home</a></div>
    </td>
    <td>
    <div align="center"><a href="members.js"
    target="mainFrame">members</a></div>
    </td>
    <td>
    <div align="center"><a href="misc.htm"
    target="mainFrame">misc</a></div>
    </td>
    </tr>
    </table>
    </body>
    </html>
     
    , Apr 5, 2006
    #1
    1. Advertising

  2. [on] Guest

    wrote:
    > My exeternal JS works fine when the call is placed in the page head or
    > body.
    >
    > But if I call the same javascript from a (clicked) text link on the
    > page, the script throws a JS error when it executes.
    >
    > Help! I can't figure this out.
    >
    > The external JS file is "members.js". Here's the code:
    >
    > <html>
    > <head>
    > <title>Untitled Document</title>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=iso-8859-1">
    > </head>
    > <body bgcolor="#FFFFFF">
    > <script Language="JavaScript"></script>
    > <table width="90%" border="0" align="center">
    > <tr>
    > <td>
    > <div align="center"><a href="home.htm"
    > target="mainFrame">home</a></div>
    > </td>
    > <td>
    > <div align="center"><a href="members.js"
    > target="mainFrame">members</a></div>
    > </td>
    > <td>
    > <div align="center"><a href="misc.htm"
    > target="mainFrame">misc</a></div>
    > </td>
    > </tr>
    > </table>
    > </body>
    > </html>


    You load an external Javascript File like this. (best if you use it in
    the <head> tag)

    <script language="javascript" type="text/javascript"
    src="javascriptfile.js"></script>

    To then call a javascript function when you click a <a>-tag you can use
    any of these (the second one might work the best for you)

    1: <a onclick="function();">Some Name</a>

    2: <a href="javascript:function();">Some Name</a>

    3.1: <a id="someid">Some Name</a>

    3.2: Code in javascript file:
    var atag = document.getElementById("someid");
    atag.onclick = function;
     
    [on], Apr 5, 2006
    #2
    1. Advertising

  3. Guest

    Thanks for your reply.

    But if I load my external Javascript like this (in the 'head' section):

    <script language="javascript" type="text/javascript"
    src="members.js"></script>

    The script "fires" right away when the page loads. I want it to "fire"
    only when I click on the text link.

    What can I be doing wrong?
     
    , Apr 5, 2006
    #3
  4. Guest

    OK, by placing <script language="javascript" type="text/javascript"
    src="members.js"></script> immediately after the <head> tag, the
    external script correctly doesn't fire when loaded as before. But my
    HTML call to this script (members.js) does not work.

    1. The external script (members.js):

    function pw() {
    pass = prompt("Please enter the "test" password","**********");
    if (pass=="test") {
    //alert("Click OK for the member list.");
    location.href="members.htm";
    } else {

    alert("Sorry, you must have the password to view this page. Please try
    again.");
    location.href="index.htm";
    //set this to your index page
    //try the absolute path to your page
    }}


    2. And the HTML page with the "pw()" call:

    <html>
    <head><script language="javascript" type="text/javascript"
    src="members.js"></script>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF">
    <div align="center">
    <a href="JavaScript:pw();">Click Here</a>
    </div>
    </body>
    </html>

    Can you tell me why this simple example (HTML) call doesn't work?

    Must I use a HTML function call to file the external script? I'm trying
    to keep this very simple.

    Dennis
     
    , Apr 5, 2006
    #4
  5. Lee Guest

    said:
    >
    >OK, by placing <script language="javascript" type="text/javascript"
    >src="members.js"></script> immediately after the <head> tag, the
    >external script correctly doesn't fire when loaded as before. But my
    >HTML call to this script (members.js) does not work.
    >
    >1. The external script (members.js):
    >
    >function pw() {
    >pass = prompt("Please enter the "test" password","**********");
    >if (pass=="test") {
    > //alert("Click OK for the member list.");
    > location.href="members.htm";
    >} else {
    >
    > alert("Sorry, you must have the password to view this page. Please try
    >again.");
    > location.href="index.htm";
    >//set this to your index page
    >//try the absolute path to your page
    >}}
    >
    >
    >2. And the HTML page with the "pw()" call:
    >
    ><html>
    ><head><script language="javascript" type="text/javascript"
    >src="members.js"></script>
    ><title>Untitled Document</title>
    ><meta http-equiv="Content-Type" content="text/html;
    >charset=iso-8859-1">
    ></head>
    >
    ><body bgcolor="#FFFFFF">
    ><div align="center">
    ><a href="JavaScript:pw();">Click Here</a>
    > </div>
    ></body>
    ></html>
    >
    >Can you tell me why this simple example (HTML) call doesn't work?


    Because it's an abuse of the javascript: pseudo-protocol.

    <a href="somepage.html" onclick="pw();return false">Click Here</a>

    where "somepage.html" tells the user that they must have
    Javascript enabled to use this functionality.

    You do know that it's very easy to read your external .js file
    to find the password, don't you? Or to just see the full path
    of the page that you're "protecting".


    --
     
    Lee, Apr 5, 2006
    #5
  6. Guest

    Yes, I know this pw technoque is insecure. I'm just using it as an
    example for learning javascript.

    I've made some progress, but still can't get an external javascript to
    fire from an href text link. The script fires and works OK when
    triggered by a separate onclick event, but not from a normal href link.
     
    , Apr 6, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    860
    Kevin Spencer
    May 17, 2005
  2. Mike
    Replies:
    1
    Views:
    343
    Toby A Inkster
    Nov 16, 2003
  3. Joey
    Replies:
    5
    Views:
    995
  4. Blue®
    Replies:
    4
    Views:
    812
    Blue®
    Sep 27, 2003
  5. P2P
    Replies:
    0
    Views:
    158
Loading...

Share This Page