Before you can do this you must first identify that it is a timing error and the easiest way to do that is to add an alert immediately before the line of code that isn't working correctly. The alert will interrupt the loading of the web page and so you will have a visual clue as to how much of the page has actually loaded at that point. If your code is running in the head of the page then the web page will be completely blank since none of the body will have started loading yet. If your script is running within the body of the page then everything in the page to that point will have loaded (although all of the images that are referenced may not have finished downloading yet). If the entire web page is there then the ascript is either running as the very last thing in the page or has been triggered by the window.onload event.