0

I have this issue with that when I click on anchors, that the page reloads, which I want to avoid even though I'm using pushstate:true

So, inside my main.js I have:

Backbone.history.start({ pushState: true, root: App.ROOT });

$(document).on('click', 'a:not([data-bypass])', function (evt) {

    var href = $(this).attr('href');

        if (href && href.indexOf('#') === 0) {
            evt.preventDefault();
            Backbone.history.navigate(href, true);
        }
});

So, I tried to change it like mentioned here Preventing full page reload on Backbone pushState - but without success.

I'm using BackboneJS, NodeJS and HandlebarsJS

Is there any solution to this?

Community
  • 1
  • 1
ST80
  • 3,565
  • 16
  • 64
  • 124

1 Answers1

0

pushState: true has nothing to do with preventing loading full page when you click links, it's only a way to update url address in the browser address bar. It's evt.preventDefault() that should prevent default behaviour of links.

Your provided code applies evt.preventDefault() very selectively in a sense that, according to your code, links that don't start with # work just like links are usually expected to work - loading that page afresh.

Yura
  • 2,690
  • 26
  • 32
  • When I add `evt.preventDefault()` before the `if`-statement wouldnt help either!?... :-/ – ST80 Sep 25 '14 at 12:49
  • Not sure what you mean by that not helping either, but before `if` `evt.preventDefault()` will actually prevent any link from acting like a link, i.e. will not take you to the linked page, which is what you want if you handle that action with javascript. The answer that you linked to is actually great in also checking if the link is external and only preventing link loading for internal links. – Yura Sep 25 '14 at 14:12