Where, or if you prefer when, is this code executed?
To be precise, what event* do you use to execute the function?
I ask that because, (I'll go out on a limb here) the code may be being executed as the page loads, but (perhaps) the script can only function correctly when the page is loaded fully. Forcing you to answer a modal message box {via alert()} causes the execution of the remainder of the function to be temporarily paused, meaning that when the important parts of the function are executed, the page has loaded.
Like I hope that I implied, that's an assumption founded on a working knowledge rather than any practical tests or even directly related experience**. The easiest way to find out if this is the case is to try putting the alert() after the function's main body, and posting back what happens.
Also; do say where the code is called from/when it is called.
*'inside script tags' is a viable event of sorts.
**or infact, a complete understanding of what you're trying to achieve. If you're calling this function from a user invoked event (like a click) on a fully loaded page; I have no idea.
MattEvans
Veteran Poster
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
what happens if you try this code:
function loadJSintoDIV(id, url, switchdiv) {
obj = document.getElementById(id);
if(obj.innerHTML == '' || !switchdiv) {
oelem=document.createElement("script");
oelem.src=url;
oelem.type="text/javascript";
oelem.defer=true;
document.getElementsByTagName("head")[0].appendChild(oelem);
obj.style.height = '';
obj.style.visibility = 'visible';
} else {
obj.innerHTML = '';
obj.style.height = '0px';
obj.style.visibility = 'hidden';
}
alert('test');
}
Notice the movement of the alert() line. This isn't an attempt to solve your problem, but let me know if it has the same effect as having the alert() line first, or if there is a difference
MattEvans
Veteran Poster
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
what about the event that calls that function during loading? is it called like this:
<body onload="loadJSintoDIV(etc);">
...
or like this:
<script type="text/javascript">
loadJSintoDIV(etc);
</script>
or by some other means?
MattEvans
Veteran Poster
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
try the first way.
the second way executes at an arbitrary time (possibly as it is encountered and thus before the document is loaded); the first way executes when the document has finished loading.
MattEvans
Veteran Poster
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64