Today is a good day to code

Watch Out For IE and readyState

Posted: December 31st, 1969 | Author: | Filed under: Uncategorized | No Comments »

Watch Out For IE and readyState

Picture of IrvinSo I spent half a day the other day trying to figure out what was wrong with my lazy loading JavaScripts. It is bad enough that I have to fork my lazy loader for IE, but their changing response string is pretty silly.

So what am I talking about. So, in IE you have to insert your JavaScript into the DOM by adding script tags in the head using appendChild. We have to do this because IE is pretty screwed up and won't use window.eval. I can accept that, it is a potential security issue. Inserting the tags into the DOM works fine. The JavaScript files are loaded dynamically, and IE offers the onreadystatechange attribute to allow us to check the status of the files. This worked flawlessly. Then in the event handler you check for the readyState member variable on the event args object. This also worked like a champ when someone was visiting the page for the first time. The readyState member variable contains the string “Loading” until the script has finished loading, then it contains “Loaded.” Fine I thought, I'll check for “loaded.” This worked OK until….. a user refreshed the page, or if they navigated to another site and then came back to my site, my safe function call would never happen because, as I found out later, the conditional that was evaluating never equaled “loaded.” The reason, Microsoft decided to change it to “complete” if the page was cached. Arrrrggggghhhh! Why would anyone do that. It forces developers to put a messy, hacky or statement in their conditional block in the event function.

Hopefully this post will prevent anyone else from having to lose time with this sillyness.