Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wololodev/e9ed539527396b77f340 to your computer and use it in GitHub Desktop.
Save wololodev/e9ed539527396b77f340 to your computer and use it in GitHub Desktop.
// 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
Copy link
Author

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

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

@shaneharter
Copy link

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