Fix IE7 absolute URL issue for link hijacking in main.js #215

Closed
engram-design opened this Issue Apr 20, 2013 · 2 comments

Comments

Projects
None yet
2 participants

Thought I would suggest the following - in IE7, for some strange reason (it's IE after all), links are parsed as absolute URLs, rather than relative ones, which would be as expected. This causes some issues with the link hijacking code.

I'm aware support for IE7 is pretty low on the list, but it's a pretty simple fix to incorporate the below one-liner, which wouldn't have any adverse effects otherwise...

$(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 + "//" + location.host + app.root;
    // IE7 fix
    href.attr = href.attr.replace(root, '/'); // IE7 fix for absolute URLs

    // 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.
      evt.preventDefault();

      // `Backbone.history.navigate` is sufficient for all Routers and will
      // trigger the correct events. The Router's internal `navigate` method
      // calls this anyways.  The fragment is sliced from the root.
      Backbone.history.navigate(href.attr, true);
    }
  });

Feel free to ignore this if not considered important - I've just found this is something I've needed to add constantly for my projects.

Owner

tbranyen commented Jul 23, 2013

No this is definitely important. We should get a unit test added and ensure it passes.

Owner

tbranyen commented Aug 6, 2013

This is removed from the latest codebase.

tbranyen closed this Aug 6, 2013

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