By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

Cannot Call External Javascript From a Text Link

P: n/a
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 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

de******@earthlink.net 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;

Apr 5 '06 #2

P: n/a
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 '06 #3

P: n/a
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 '06 #4

P: n/a
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 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.