Skip to content

Instantly share code, notes, and snippets.

Last active February 25, 2024 21:38
Show Gist options
  • Save tbranyen/1142129 to your computer and use it in GitHub Desktop.
Save tbranyen/1142129 to your computer and use it in GitHub Desktop.
hijack links for pushState in Backbone
// All navigation that is relative should be passed through the navigate
// method, to be processed by the router. If the link has a `data-bypass`
// attribute, bypass the delegation completely.
$(document).on("click", "a[href]:not([data-bypass])", function(evt) {
// Get the absolute anchor href.
var href = { prop: $(this).prop("href"), attr: $(this).attr("href") };
// Get the absolute root.
var root = location.protocol + "//" + + Application.root;
// Ensure the root is part of the anchor href, meaning it's relative.
if (href.prop.slice(0, root.length) === root) {
// Stop the default event to ensure the link will not cause a page
// refresh.
// 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);
Copy link

Thank you!

Copy link

aroc commented Feb 3, 2013

Awesomeness! Thanks for sharing.

Copy link

jabbett commented Mar 19, 2013

Thanks, Tim!

I'm using mod_rewrite to get "normal" URLs, so I also check href if it begins with #

Look out for javascript: links, too.

Copy link

Nice tip. Thanks!

Copy link

Thank you, really cool.

Copy link


Copy link

This doesn't seem to work anymore. Backbone.history._hasPushState is undefined now.

Copy link

Cool. Doesn't work with anchors though. I just shoved in a

if (!href.match(/^#/)) {

before the protocol check to get it all working.

Copy link

Thank you very much!!!

Copy link

Thanks. This is a massive help.

Copy link

Thanks a lot for this! Works great except if you are holding the cmd button to open links in a new tab

Copy link

Copy link

gabceb commented Mar 18, 2014

This gist seems to be close to #1 on Google when you search for backbone router links so here is the Coffeescript version

Copy link

Updated gist with protection for internal #div hash links, better cross domain link support, more efficient with $document caching, and does not use deprecated "delagate" method from jQuery.

Copy link

calmdev commented Jun 21, 2014

Nice work — this works great!

Copy link

isayme commented Jul 9, 2014

why use href.slice(protocol.length) instead of href.slice(0, protocol.length) ?
If href is relative url), the previous will return while the later return http://...

Copy link

GitHub never notified me of any of these comments >_> I just updated the Gist to latest version I've been using.

Copy link

Shouldn't it be href.attr in the navigate function?

Copy link

jaapz commented Jun 18, 2015

@XemsDoom yes it should

You also should probably add a check whether or not someone called preventDefault on the event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment