views:

211

answers:

3

A piece of javascript code I'm working on is causing the nasty "Operation Aborted" message in IE. I am well aware that you cannot modify the DOM until after it has loaded. Sure enough the line of javascript code causing the error is the one which appends a new div tag to the body. However the function which the line is located within is called via FastInit.addOnLoad! I thought this meant the function would be called after the DOM was ready. Is there a better solution to this problem that using FastInit?

+1  A: 

I'm not sure about FastInit, but I just answered a similar question about waiting for the DOM to be ready here:

http://stackoverflow.com/questions/1153858/initiate-onclick-faster-than-with-document-onload/1153988#1153988

Basically, you can do it in IE by using the defer attribute on your script tag:

<script type="text/javascript" defer>
</script>

This will delay parsing the script until after the DOM is ready.

Andy E
@Andy, thanks, I'll try that.
Josh
@Josh: fastInit already uses that trick to work out when to trigger on IE: you can see it at the bottom of the FastInit source in a "document.write".
NickFitz
@NickFitz: that's what I thought -- which is why I was confused as to why this isn't working.
Josh
@Andy, unfortunately, the defer tag didn't help.
Josh
A: 

Sorry...

window.onload = function() {
    // Your code??
}
SleepyCod
I'd prefer not to wait until all images have loaded. Also, when I do window.onload, sometimes my code is never actually run!
Josh
A: 
NickFitz
The script in questions is in the <head> and is called via FastInit so the DOM should be ready already.
Josh
FastInit has to use some pretty weird tricks to get a simulation of DOMready on IE. The definitive Microsoft explanation of the causes for this error is at http://support.microsoft.com/kb/927917
NickFitz
@NickFitz: The Microsoft document seems to refer to scripts that are grandchildren (or deeper) of the body. My script is a child of the <head> tag. Are you saying my script should be a child of the <body> tag instead? It appears to me that article doesn't apply in this case.
Josh
@NickFitz putting the script in the body tag didn't help, unless it was the very last item in the DOM.
Josh
I ended up just putting the script tag as the final element in the DOM... still don't know why this wasn't work in the first place.
Josh