Sunday, May 12, 2024
57
rated 0 times [  59] [ 2]  / answers: 1 / hits: 21632  / 15 Years ago, wed, december 9, 2009, 12:00:00

I am currently developing Unit Tests for a Javascript method that detects the readiness of the document. This code is already at framework level, so please avoid mentions of this being already implemented in jQuery or another library.



I have successfully simulated the 'readystatechange' change event with the following code:



var event;
event = document.createEventObject();
event.type = 'readystatechange';
document.fireEvent('onreadystatechange',event);


I failed to do the same for the 'load' event. The following code results in an invalid argument error in IE7, thrown by the call to fireEvent on the last line:



event = document.createEventObject();
event.type = 'load';
document.fireEvent('onload',event);


Has anyone done this, or failed to do this before? I am also interested in any suggestion to fire the event in a different way.



Edit: following the suggestion by Crescent Fresh, I changed my code to:



event = document.createEventObject();
event.type = 'load';
document.body.fireEvent('onload',event);


There is no more error, but the listener for 'onload' does not fire. Here is how I configured it:



document.attachEvent('onload',listener);

More From » unit-testing

 Answers
12

According to this page at MSDN, there's no onload event for document.



You want either window.onload or document.body.onload. These are identical in IE: for historical reasons, <body onload=...> actually sets window.onload, so MS decided to make document.body.onload an alias of window.onload.



The problem with this is - as Eric mentioned in the comments - that there doesn't seem to be a way to manually fire window events, which means that there might not be a solution for Eric's problem.


[#98113] Saturday, December 5, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckinley

Total Points: 15
Total Questions: 101
Total Answers: 94

Location: Liechtenstein
Member since Fri, Sep 11, 2020
4 Years ago
mckinley questions
Sun, Oct 17, 21, 00:00, 3 Years ago
Tue, Jun 15, 21, 00:00, 3 Years ago
Thu, Apr 8, 21, 00:00, 3 Years ago
;