Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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);
}
});
}
@wololodev

This comment has been minimized.

Copy link
Owner Author

commented Jun 7, 2014

to be inserted after jQuery/Backbone has been loaded, and after this line:

Backbone.history.start({pushState: true});
@shaneharter

This comment has been minimized.

Copy link

commented Sep 3, 2014

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:
https://gist.github.com/shaneharter/c1458d8ddf504515c244

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.