且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

手动dispatchEvent DOMContentLoaded

更新时间:2022-10-16 21:41:51

这适用于Firefox: p>

  var DOMContentLoaded_event = document.createEvent(Event)
DOMContentLoaded_event.initEvent(DOMContentLoaded,true,true)
window.document.dispatchEvent(DOMContentLoaded_event)


Is there any way to manually fire the DOMContentLoaded event? I'm trying to write a unit-test for some client-side javascript which does some stuff on the DOMContentLoaded event.

The following did not work: document.dispatchEvent("DOMContentLoaded") or document.body.dispatchEvent("DOMContentLoaded");

This works for me in Firefox:

var DOMContentLoaded_event = document.createEvent("Event")
DOMContentLoaded_event.initEvent("DOMContentLoaded", true, true)
window.document.dispatchEvent(DOMContentLoaded_event)