Skip to content

A dead simple JavaScript plugin to help manage page-specific JavaScript code in the browser.

Notifications You must be signed in to change notification settings

benhowdle89/fwd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Fwd

A dead simple JavaScript plugin to help manage page-specific JavaScript code in the browser.

Use case

When you want certain JavaScript to only be ran on certain pages (or certain URL patterns), ie. some form validation code to be initialised when the user is on your contact page.

If you need anything more heavy-duty than this, I'd suggest looking at Sammyjs which has much deeper support for richer applications.

Installation

Just include a reference to fwd.js somewhere in your page

<script src="/path/to/fwd.js"></script>

Usage

var fwd = new Fwd();

fwd.links({

	routes: {
		"about": "about",
		"services/:id": "showService",
		"services": "services",
		"contact": "contact"
	},

	about: function() {
		console.log('about');
	},

	services: function() {
		console.log('services');
	},

	showService: function(id) {
		console.log(id);
	},

	contact: function() {
		console.log('contact');
	}

});

Here is a sample "router" made with Fwd. We create a "routes" object which maps URL patterns to callback functions.

When the URL matches any of the patterns, the relevant callback function is invoked. It also supports dynamic URL parameters, as you can see with :id, which gets passed to our showService method as id.

Fwd supports multiple URL patterns, from standard page load URLs, ie. /about, or dynamic pushState/hash URL changes, ie. /#about.

Fwd also provides a go method which takes a new "route", ie. "contact", which will update the URL via pushState (or fallback to hash) and then invoke the, for example, "contact" method:

fwd.go('contact');

Development

This is WIP, so it will almost certainly contain edge-case/cross-browser bugs. Feel free to contribute you spot any low hanging fruit fixes!

About

A dead simple JavaScript plugin to help manage page-specific JavaScript code in the browser.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages