bonaca
bonaca

Reputation: 125

How to set the hash without reloading the page?

I have this code to switch the content of a div, and need a hash in the url, but with the line document.location.hash = a; the page is going to reload. How can I set the hash without reloading the page?

$('.leftPass').click(function() {
    var a = $(this).attr('id');
    var b = 'chapters/' + a + '.php';
    $('#storyR').load(b);
    $("html, body").animate({
        scrollTop: 0
    }, 1000);
    $('.leftAct').removeClass('leftAct').addClass('leftPass');
    $(this).removeClass('leftPass').addClass('leftAct');
    document.location.hash = a;
});

Upvotes: 1

Views: 98

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318468

location.hash is correct. Changing it will not reload the page, ever. Is .leftPass a link by any chance? If yes, call e.preventDefault() in your event handler. e should be the first argument of the function:

$('.leftPass').click(function(e) {
    e.preventDefault();
    // ...
});

Upvotes: 3

Related Questions