https://github.com/digipost/pager.js
Fork of the no longer published pager.js lib
https://github.com/digipost/pager.js
Last synced: about 2 months ago
JSON representation
Fork of the no longer published pager.js lib
- Host: GitHub
- URL: https://github.com/digipost/pager.js
- Owner: digipost
- License: mit
- Created: 2017-03-20T08:47:20.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-11-09T07:48:55.000Z (over 1 year ago)
- Last Synced: 2025-01-29T05:38:02.744Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 57.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Pager.js
Pager.js runs JavaScript functions based on the current path name. This is useful if you have a traditional website
and you want to run specific JS on each page, but still include all JS files on all pages.While this is a quite narrow use case, I've found it useful on a number of projects which need a little JS applied to certain pages. Paths are matched by the following rules:
* Leading and trailing slashes are removed.
* Any file extension (like .html) is removed.
* Handler must match the entire path name to run.
* If there's no path, any handler named "index" gets called.## Example
```javascript
// Add a default handler for the index page:
pager.on('index', function() {
console.log('Hello from index!');
});// Add a handler for the 'foo' path
pager.on('foo', function() {
console.log('Hello from foo!');
});// Add a handler for for the 'foo/bar' path
pager.on('foo/bar', function() {
console.log('Hello from bar!');
});// Run page handler on page load (or at any other time):
$(function() { pager.run(); });// When the user navigates to a page, a handler is run.
window.location = '/'; // => "Hello from index!"
window.location = '/foo'; // => "Hello from foo!"
window.location = '/foo/bar'; // => "Hello from bar!"// Get the current page name:
pager.get(); // => 'foo/bar'// Remove one handler:
pager.off('foo/bar');// Remove all handlers:
pager.off();
```