/backbone_pushstate_router.js
Forked from tbranyen/backbone_pushstate_router.js
Last active May 18, 2018
// Only need this for pushState enabled browsers | |
if (Backbone.history && Backbone.history._hasPushState) { | |
var $document = $(window.document); | |
var openLinkInTab = false; | |
$document.keydown(function(e) { | |
if (e.ctrlKey || e.keyCode === 91) { | |
openLinkInTab = true; | |
} | |
}); | |
$document.keyup(function(e) { | |
openLinkInTab = false; | |
}); | |
$document.on("click", "a", function(e) { | |
var $this = $(this); | |
var href = $this.attr("href"); | |
var domain = $this.prop("hostname"); | |
// if the href is not equal to outpost.travel | |
var isCSRF = domain !== window.document.location.hostname; | |
var hasHashLink = href.indexOf("#") === -1; | |
// if it's been indicated that we don't want to open in a new tab | |
// and that the link is the same domain then preventDefault | |
// protection againts internal #div links | |
if (!openLinkInTab && !isCSRF && hasHashLink) { | |
e.preventDefault(); | |
Backbone.history.navigate(href, true); | |
} | |
}); | |
} |
This comment has been minimized.
This comment has been minimized.
Thanks for sharing this. YMMV but i needed to support using Backbone.history.root to deal with my app not being at the / root of the domain. I also cleared up a couple things I found confusing and missing: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
to be inserted after jQuery/Backbone has been loaded, and after this line: