Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Updated protocol check to respect javascript: pseudo-protocol and hash links (#foo) Updated to support IE
// Use absolute URLs to navigate to anything not in your Router.
// Note: this version works with IE. Backbone.history.navigate will automatically route the IE user to the appropriate hash URL
// Use delegation to avoid initial DOM selection and allow all matching elements to bubble
$(document).delegate("a", "click", function(evt) {
// Get the anchor href and protcol
var href = $(this).attr("href");
var protocol = this.protocol + "//";
// Ensure the protocol is not part of URL, meaning its relative.
// Stop the event bubbling to ensure the link will not cause a page refresh.
if (href.slice(protocol.length) !== protocol && protocol !== 'javascript://' && href.substring(0, 1) !== '#') {
evt.preventDefault();
// Note by using Backbone.history.navigate, router events will not be
// triggered. If this is a problem, change this to navigate on your
// router.
Backbone.history.navigate(href, true);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment