Dispatch window load event
I开发者_StackOverflow社区 need to dispatch the window's load
event in Javascript. There is plenty of documentation on how to dispatch mouse events, But I can't find any on load events. It only needs to work in Firefox. It would be equally helpful if I could "spy" on the window's load event, because then I could just call the function. Using the "Event spy" addon isn't working because it requires me to open the dom inspector, which i can't do until after the page is finished loading.
I'm making a grease monkey script. So I don't know which function it is that I'm trying to invoke.
var load_event = document.createEvent('Event');
load_event.initEvent('load', false, false);
window.dispatchEvent(load_event);
works fine in my environment.
HTML events are fired differently than mouse events. Here is how to fire a load event:
var load_event = document.createEvent("HTMLEvents");
load_event.initEvent("load", true, true);
window.dispatchEvent(load_event);
References
UIEvents: initEvent
DOM Features
Update for 2021 year
var load_event = new Event("load");
window.dispatchEvent(load_event);
Event
精彩评论