occamsrazor-history-browser

0.0.3 • Public • Published

occamsrazor-history-browser

This function provides a bridge between the history API and the event bus created using occamsrazor It exposes a convenient "changestate" event that fires when pushstate/replacestate are called or popstate event is triggered. See the history API

var or = require('occamsrazor');
var registerHistoryAPI = require('occamsrazor-history-browser');

var events = or();
registerHistoryAPI(events);

events.on('changestate', function (evt, newState) {
  // either pushstate was triggered on "events"
  // or popstate fired on "window". For example the user clicked back or forward
});

events.trigger('pushstate', { state: { ... }, title: 'newTitle', url: 'new.html' });

Syntax: registerHistoryAPI(events, window);

  • events: an occamsrazor instance
  • window (optional): the global object, it defaults to "window"

You can interact with the history API using 2 events: pushstate and replacestate. They takes as second argument an object with state, title, url: the arguments are then passed to history.pushstate or history.replacestate.

These events trigger 'changestate' that is called with the new state. changestate is also called by the browser when popstate is triggered (clicking the back button for example).

Readme

Keywords

Package Sidebar

Install

npm i occamsrazor-history-browser

Weekly Downloads

0

Version

0.0.3

License

MIT

Last publish

Collaborators

  • sithmel