# Other Programming > AJAX >  IE window.onload firing too early!! before page completely loads

## boobface2009

There wasn't a plain javascript forums so I figured this to be the most appropriate place for this thread.

I have a page that runs a script on the window.onload event. My problem is that when the page is viewed in IE (6,7 & 8, have not checked other versions) a javascript error is thrown due to an element on the page not being loaded yet... and also... this does not happen when the page is first loaded but only on post backs and any time the page is loaded (after the very first load) unless you refresh (navigating back to the page without refresh throws the javascript error).

This same problem does not occure in firefox.

You can see the page yourself here:
http://www.enterprisesofcbg.com/erp/template.aspx

When you first load the page in IE it will load correctly. If you click on the URL and then press Enter to reload the page w/o refreshing, you will see the error I'm talking about. You can also see the error by pressing the "Post Back" button on the page.

You can scroll to line 341 on the page to see the window.onload script that calls each function. The error is occuring within the InitWorkArea() function called by InitApp(). Both of these functions are contained within the script file @ http://www.enterprisesofcbg.com/erp/scripts/globals.js. If you try to call the function manually by typing "javascript**:InitWorkArea();" into the URL bar once the page has loaded it runs completely fine!?!?!

I hope this is all clear. I am pretty familure w/ client/server systems as I have been developing them since 2001.

I hope someone has some ideas because I am running out.

----------


## boobface2009

I hope everyone is aware that you must have the show javascript errors option enabled in the internet options window to see this error message in Inernet Explorer...

----------


## dannystommen

I'm a big fan of jQuery It's a javascript library which makes life much easier in my opinion.

Anyway, to get to your question, jQuery has a function 'ready', which is executed after the whole document has completed.

About the function: http://docs.jquery.com/Tutorials:Int...29.ready%28%29

----------


## boobface2009

Hey Danny, thanks for the reply. My problem is that the window.onload event is supposed to fire after the document, its images, scripts, styles, and all other refrenced files are completely loaded on the client's machine. So it's supposed to do exactly what you're describing... it just isn't.

I've used this same exact approach for years and for this is the first time this has ever happened.

Something on the page is causing the onload event to fire before specific elements on the page are loaded. You can see because when the error dialog pops up there are several placeholders on the document where content has not been fully loaded.

----------


## monalin

Try using an eventHandler instead of attaching this code in the body



```
function CustomLoadCode()
{
         /// RUN CODE HERE
}
window.addEventListener("load", CustomLoadCode, false)
```

----------


## boobface2009

well window.addEventListener only works in FireFox and my problem is with the IE browser.

right now I already am attaching to the onload event with window.onload (IEs event subscription counterpart). I have also attached to the window.addEventListener on local tests although it cant be seen on the site posted above.

The script is currently running to attach to the event at the bottom of the page within the <body> tag. If I move it around it makes no difference (according to local tests). It's placed at the bottom of the page in the posted example because I was thinking that since the problem is that the event is firing before the page actually loads then placing the code that attaches to the event at the bottom of the page would ensure that atleast the html objects have been initiated when the event is triggered but even that doesnt work. 

I am subscribing to the event like I have done a thousand times before. For some reason it doesn't work this time... I have other examples of this same protocal published & running right now w/o any problems.

----------


## boobface2009

Just so everyone knows, if you want to run local tests on your own all the script, css & image references contain the full virtual path so you can view the source and save it as html file on your local machine. Then you can modify it w/ text editor and run the file in your browser.

----------

