<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