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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function call from external js file 1

Status
Not open for further replies.

jockm

Technical User
Aug 13, 2002
41
0
0
GB
Good day

I have been cleaning up my web pages and moving all the various bits of javascript that I have dotted around on various pages to one single external javascript file:
On one of my pages ( I have a js function called "change()", which regulates a popdown list on that page.

For some inexplicable reason, the function works fine when the code is on the page, but not when it is only in the scripts.js file. I'm very puzzled about this. Can anyone help? At the moment top.htm has the function in both the external scripts.js file *and* on the page, which works. But soon as I take the function off the page it doesnt seem to be called anymore.

thanks for help here

jockm
 
How about posting the code so we can see what's up? There's always a better way...
 
Okay, here's the code in the <HEAD> section of top.html, and its also repeated in the scripts.js file, (along with a few other functions.):

<script language=&quot;javascript&quot; src=&quot;scripts.js&quot;> </script>


<SCRIPT LANGUAGE=JAVASCRIPT TYPE=&quot;TEXT/JAVASCRIPT&quot;>
<!-- Hide from old browsers

function change(){
var loc;
selected=document.dropdownnav.drop.selectedIndex;
loc=document.dropdownnav.drop[selected].value;
if (loc.indexOf (&quot;javascript&quot;,0) == -1)
{
parent.main.location=loc;
}
else {
document.location=loc;
}
}

// done hiding -->
</script>


Then a bit lower down in the <BODY> section of top.htm there is

<form NAME=&quot;dropdownnav&quot;>

<p align=&quot;center&quot;><FONT SIZE=&quot;+1&quot; COLOR=&quot;#FFFF33&quot;><B>-- Workshops --</B></FONT><font size=&quot;2&quot;> </font>
<SELECT onChange=&quot;change()&quot; NAME=&quot;drop&quot; size=&quot;1&quot;>
<OPTION SELECTED value=&quot;main.htm&quot;>Please Select
<option value=&quot;writeups/rogerlinden.html&quot;>Advaita Satsang</option>
<option value=&quot;writeups/beverley.html&quot;>Art & Deep Ecology</option>

//quite a few more options left out here to save space

<option value=&quot;writeups/workcamp.html&quot;>Work Camp</option>
</select></p>
</form>

If you want to look at the entire scripts.js file its at
Thanks!

jock
 
Hi Jockm,
Please try top.main.location=loc;
instead of parent.main.location=loc;
into function change of external js file

Hope it helps [bigcheeks]
Experience teaches slowly and at the cost of mistakes [pipe]
 
Raxg, Thank you very much.

Substituting

top.main.location=loc;

in the external scripts.js file makes it work just fine.

But why? I wish I could fathom why. How could it work as it was before when it was in the body of the html document and not in the external scripts.js file? I thought that all that happened when you put your scripts into an external file is that the browser brings in the external code and lays it down right there on the page.

Well, I ordered a couple of javascript books and maybe I will get the clues from perusing them. I must say that the two I have here, javascript for dummies and Visual Quick start to Javascript dont really help much to understand how the language really works.

jock
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top