pjax
Version:
Easily enable fast AJAX navigation on any website (using pushState + XHR)
32 lines (28 loc) • 844 B
JavaScript
var forEachEls = require("../foreach-els");
module.exports = function(els, events, opts) {
events = typeof events === "string" ? events.split(" ") : events;
events.forEach(function(e) {
var event;
event = document.createEvent("HTMLEvents");
event.initEvent(e, true, true);
event.eventName = e;
if (opts) {
Object.keys(opts).forEach(function(key) {
event[key] = opts[key];
});
}
forEachEls(els, function(el) {
var domFix = false;
if (!el.parentNode && el !== document && el !== window) {
// THANK YOU IE (9/10/11)
// dispatchEvent doesn't work if the element is not in the DOM
domFix = true;
document.body.appendChild(el);
}
el.dispatchEvent(event);
if (domFix) {
el.parentNode.removeChild(el);
}
});
});
};