Skip to content Skip to sidebar Skip to footer

Manually Dispatchevent Domcontentloaded

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. T

Solution 1:

This works for me in Firefox:

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

Solution 2:

Since initEvent is deprecated here, it's better to use Event constructor like this:

window.document.dispatchEvent(newEvent("DOMContentLoaded", {
  bubbles: true,
  cancelable: true
}));

Post a Comment for "Manually Dispatchevent Domcontentloaded"