Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating an external .js file

Status
Not open for further replies.

rshandy

Technical User
Dec 26, 2003
91
US
I'm trying to create an external .js file and it just doesn't seem to work.

I found an old example while searching the forum and tried to use it, but I can't seem to get it to call the file.

Here's the example:

myFile.js:


/* begin of js file */
// NOTE there are NOT <script> tags included
function foo() {
alert("foo");
}
/* end of js file */

myPage.html:

CODE
<script type="text/javascript" src="myFile.js"></script>
<!-- now call a function from it: -->
<script type="text/javascript">foo();</script>

NOt sure why I can't get it to work. what am I missing?

In addition, I actually want to the the external .js from within a script within a function...

Thanks,

Rich
 
There is nothing wrong with the code you showed here. There must be something wrong with the path or name of the file. Did you type the info in here, or copy and paste? If you typed it in, please copy and paste so we can see what the files really have in them.

Lee
 
Are you sure the .js file is in the same directory as the page including it?

Adam
 
Hi guys,

thanks for the quick reply... Lee, the code I'm using in the page is the same that is copied and pasted in the thread.

I'm not sure why its not working.

Adam, I double checked and made sure the .js file is in the same directory.

Here's a stupid question even though I've tried it both ways but, does it matter if its called from head or body section of the page?

Is there anything else I can try to see where the program is stopping?

Rich
 
Put an alert before you load the external file, then one afterwards to see if that's working. Add one just in the main block of the external file, not in a function.

As well, are you on a Windows machine, or something where file names are case sensitive. I noticed that your external JS file is named with mixed case letters in the code. It's wise, in case you end up sticking the file on server that is case sensitive, to keep the file names all lower case.

Lee
 
Try calling the script from a anchor to see if that works:
Code:
<a href="#" onclick="javascript:foo()">

staffa
 
The // comment, if it's in your .js file, could be your problem. Not all VMs accept this as a comment delimiter.
 
Ok!! thanks to all - I found the main problem. I'm using Softcart and I have to use a js redirect - so I can't use the relative address, I have to use absolute addressing:

Here's the page code now:
<html>
<head>
<title>jstest</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<SCRIPT LANGUAGE="JavaScript">
SCPath = "/cgi-bin/SoftCart.exe";
config = "scstore"; if (location.pathname.substring(0,SCPath.length)!= SCPath) {
window.location.replace(SCPath + location.pathname + "?E+" + config);
}



</Script>
<script type="text/JavaScript" src="
<script type="text/javascript" >foo2();foo();</script>

</head>

<body bgcolor="#ffffff" leftmargin="0" topmargin ="0" marginwidth="0" marginheight="0">
<script language="JavaScript1.2" src="

</body>

</html>
--------------------------------
and here's the js script code:

function foo() {
alert("foo1 is alert when function foo is called");
}
alert ('This only exists between the functions');
function foo2() {
alert("Okay, foo you too is only when function foo2 is called");
}

-------------------------
All the calls above work fine. Now what I'd like to do is be able to call the external js script within the header tags within a function, for example:


<html>
<head>
<title>jstest</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<SCRIPT LANGUAGE="JavaScript">
SCPath = "/cgi-bin/SoftCart.exe";
config = "scstore"; if (location.pathname.substring(0,SCPath.length)!= SCPath) {
window.location.replace(SCPath + location.pathname + "?E+" + config);
}

function main {

"call the functions foo2() and foo() here"

}


</Script>

</head>

<body bgcolor="#ffffff" leftmargin="0" topmargin ="0" marginwidth="0" marginheight="0">


</body>

</html>


how do I do that?

Thanks,

Rich
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top