I am currently developing Unit Tests for a Javascript method that determines the readiness of a document. This code is already at the structure level, so please avoid mentioning it already implemented in jQuery or another library.
I successfully modeled the readystatechange change event with the following code:
var event; event = document.createEventObject(); event.type = 'readystatechange'; document.fireEvent('onreadystatechange',event);
I could not do the same for the load event. The following code results in an invalid argument error in IE7 caused by calling fireEvent on the last line:
event = document.createEventObject(); event.type = 'load'; document.fireEvent('onload',event);
Has anyone done this or not done this before? I am also interested in any suggestion to trigger the event differently.
Edit: as suggested by Crescent Fresh, I changed my code to:
event = document.createEventObject(); event.type = 'load'; document.body.fireEvent('onload',event);
The error no longer occurs, but the listener for "onload" does not work. Here's how I set it up:
document.attachEvent('onload',listener);
javascript internet-explorer events unit-testing document
Eric Brรฉchemier
source share