history-events
Version:
Adds missing window.history events onpushstate, onreplacestate and onchangestate.
2 lines (1 loc) • 1.26 kB
JavaScript
!function(){function e(e,t,n){var o;document.createEvent?(o=document.createEvent("HTMLEvents"),o.initEvent(t,!0,!0)):document.createEventObject&&(o=document.createEventObject(),o.eventType=t),o.state=n,o.eventName=t,e.dispatchEvent?e.dispatchEvent(o):e.fireEvent&&htmlEvents["on"+t]?e.fireEvent("on"+o.eventType,o):e[t]?e[t]():e["on"+t]&&e["on"+t]()}function t(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent&&htmlEvents["on"+t]?e.attachEvent("on"+t,n):e["on"+t]=n}function n(e,t,n){e.removeventListener?e.removeEventListener(t,n,!1):e.detachEvent&&htmlEvents["on"+t]?e.detachEvent("on"+t,n):e["on"+t]=null}function o(){return"undefined"!=typeof window&&window.history&&"pushState"in window.history}if(o()){var a=window.history,i=a.pushState;a.pushState=function(t){var n=i.apply(a,arguments);return e(window,"pushstate",t),e(window,"changestate",t),n};var r=a.replaceState;a.replaceState=function(t){var n=r.apply(a,arguments);return e(window,"replacestate",t),e(window,"changestate",t),n},t(window,"popstate",function(t){e(window,"changestate",t.state)})}var v={isHistorySupported:o,addEventListener:t,removeEventListener:n,triggerEvent:e};console.log(o()),o()&&(window.HistoryEvents=v),"undefined"!=typeof module&&(module.exports=v)}();