Cannot Call External Javascript From a Text Link

D

dennise9

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>
 
O

[on]

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;
 
D

dennise9

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?
 
D

dennise9

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
 
L

Lee

(e-mail address removed) 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".


--
 
D

dennise9

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top