Skip to content

Instantly share code, notes, and snippets.

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
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) {
Backbone.history.navigate(href, true);

This comment has been minimized.

Copy link
Owner Author

@wololodev wololodev commented Jun 7, 2014

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

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

This comment has been minimized.

Copy link

@shaneharter shaneharter 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:

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