Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Cannot Call External Javascript From a Text Link (http://www.velocityreviews.com/forums/t924019-cannot-call-external-javascript-from-a-text-link.html)

dennise9@earthlink.net 04-05-2006 01:51 AM

Cannot Call External Javascript From a Text Link
 
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>


[on] 04-05-2006 06:36 AM

Re: Cannot Call External Javascript From a Text Link
 

dennise9@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;


dennise9@earthlink.net 04-05-2006 01:14 PM

Re: Cannot Call External Javascript From a Text Link
 
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?


dennise9@earthlink.net 04-05-2006 03:33 PM

Re: Cannot Call External Javascript From a Text Link
 
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


Lee 04-05-2006 04:06 PM

Re: Cannot Call External Javascript From a Text Link
 
dennise9@earthlink.net 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".


--


dennise9@earthlink.net 04-06-2006 07:25 PM

Re: Cannot Call External Javascript From a Text Link
 
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.



All times are GMT. The time now is 07:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.