1.0.4 • Published 10 years ago
history-events v1.0.4
history-events
Adds missing window.history events onpushstate, onreplacestate and onchangestate.
This module patches browser's window.history object and adds support for additional history events. You can use this module in a polymorphic applications (it only loads if inside a browser and does not raise an error on server side).
Setup
<script src="dist/history-events.min.js" type="text/javascript"></script>If you are compiling assets using a bundler (e.g. webpack) then you just do the import inside your main file.
// es5
var history = require('history-events');
// es6
import history from 'history-events';Usage
var history = require('history-events');
if (history.isHistorySupported()) {
window.addEventListener('changestate', function(e) {
console.log('URL changed');
});
window.history.pushState(null, null, '/login'); // `changestate` will be triggered
}API
Window Events
- onpopstate is triggered when browser's back/forward button is pressed.
- onpushstate is triggered on
window.history.pushStatemethod call. - onreplacestate is triggered on
window.history.replaceStatemethod call. - onchangestate is triggered on
window.history.pushStateorwindow.history.replaceStatemethod call or when browser's back/forward button is pressed.
Module Methods
- history.isHistorySupported() returns true when
window.historyfeature is supported by the environment. - addEventListener registes a new event.
- removeEventListener unregistes a new event.
- triggerEvent triggers an event.