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

.js file not loading in time for function call, problem only in Netscape/Mozilla/Firefox

P: n/a
I was under the assumption that javascript loads all the <head></head>
elements before processing the <body> tag in Mozilla/Netscape/Firefox.
It doesn't seem like it, with my problem.

I have navigation that has onMouseOver and onMouseOut triggers, that
use function calls in my external .js file. When I mouseOver a button
in my navigation quickly enough to catch it before the rest of the body
loads, it returns a function "not defined" error. The function is
definitely defined in my .js file. If I wait for the whole page to
load, and try mousing over and out of my nav, I don't get that error.

I don't have this problem with IE.
Any help would be much appreciated.
Danny

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a


da*********@gmail.com wrote:
I was under the assumption that javascript loads all the <head></head>
elements before processing the <body> tag in Mozilla/Netscape/Firefox.
It doesn't seem like it, with my problem.

I have navigation that has onMouseOver and onMouseOut triggers, that
use function calls in my external .js file. When I mouseOver a button
in my navigation quickly enough to catch it before the rest of the body
loads, it returns a function "not defined" error. The function is
definitely defined in my .js file. If I wait for the whole page to
load, and try mousing over and out of my nav, I don't get that error.


URL? And if it has lots of content describe what your navigation buttons
are.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

P: n/a
This is definately a problem in these browsers that has been
long known esp. in Netscape

The only solution that may be any use to you is:

define/attach your external .js file in the <head></head> as
normal then the rest of your page as normal...

then near the bottom just before the </html> end tag try:

<script language="javascript" type="text/javascript">
functionToCall();</script>

This gives your page a chance to load the JS before calling the
routine... You could also try:

<body onLoad="javascript:functionToCall();">

which theorically waits till all is loaded

Hope this helps...

Regards

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 5
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------

Jul 23 '05 #3

P: n/a
Sorry, just re-read your question and my answer probably aint much
help... Apologies

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 5
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------

Jul 23 '05 #4

P: n/a
<da*********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I was under the assumption that javascript loads all the <head></head>
elements before processing the <body> tag in Mozilla/Netscape/Firefox.
It doesn't seem like it, with my problem.


The contents of the <head></head> are processed, it does not mean that
the external script file referred to by <script type="text/javascript"
src="yourfile.js"></script> has been downloaded and parsed before the
<body> is rendered. These events happen asynchronously, yourfile.js can
be downloading while the <body> is being rendered.

As a result, you need to do something to test before dispatching to the
function defined in the external file.

<head>
<script type="text/javascript" src="yourfile.js"></script>
<script type="text/javascript">
function safeRedirector(f, args) {
if ('undefined' == typeof f) {
alert('The code needed to support that is not yet loaded');
} else {
f(args);
}
}
</script>
</head>
<body>
<a href="..."

onmouseover="safeRedirector(yourMouseOverFunctionD efinedInYourFileDotJs,
[ arg1, arg2, arg3 ]);">Link</a>

Or maybe:

<head>
<script type="text/javascript" src="yourfile.js"></script>
<script type="text/javascript">
var bodyLoaded = false;
function safeRedirector(f, args) {
if (bodyLoaded) {
f(args);
} else {
alert('The code needed to support that is not yet loaded');
}
}
</script>
</head>
<body onload="bodyLoaded = true;">
<a href="..."

onmouseover="safeRedirector(yourMouseOverFunctionD efinedInYourFileDotJs,
[ arg1, arg2, arg3 ]);">Link</a>

You can set up safeRedirector() to be a little more transparent
(actually passing parameters instead of requiring they be part of an
array) I just didn't have the time to write all that.

The other thing to do is not have a single generic redirector, but to
have 'stub' functions for each of your main handlers that dispatches to
it if it's available.

Lastly, you could not define your events in the HTML, but instead attach
them to the elements dynamically. Define the function in your external
js file:

function setAllHrefMOut(f, d, inLayer) {
if (!inLayer) {
d = document;
}

if (d) {

var i;

if (d.links) {
i = d.links.length;
while (i-- > 0) {
if (!d.links[i].onmouseout) {
d.links[i].onmouseout = f;
}
}
}

if (d.layers) {
i = d.layers.length;
while (i-- > 0) {
setAllHrefMOut(f, d.layers[i].document, true);
}
}
}
} // setAllHrefMOut()

Then call it -onload-:

<body onload="setAllHrefMOut(someFunction);">

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #5

P: n/a
da*********@gmail.com wrote:
I was under the assumption that javascript loads all the <head></head>
elements before processing the <body> tag in Mozilla/Netscape/Firefox.
It doesn't seem like it, with my problem.

I have navigation that has onMouseOver and onMouseOut triggers, that
use function calls in my external .js file. When I mouseOver a button
in my navigation quickly enough to catch it before the rest of the body
loads, it returns a function "not defined" error. The function is
definitely defined in my .js file. If I wait for the whole page to
load, and try mousing over and out of my nav, I don't get that error.

I don't have this problem with IE.
Any help would be much appreciated.


You could specify that your <body> has a CSS attribute visibility:hidden.

Then the last line of your script file could overturn that. That way
there is nothing visible to mouseover during loading of the script.

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.