Back button of browser delete a class

Asked

Viewed 58 times

1

I’m making a website that works on a single page. So when I click on a menu button it scrolls to the area I want with:

$('html, body').animate({scrollTop: $('#nomedoid').offset().top},Math.abs(0));

And at the same time I put the window.history.pushState:

window.history.pushState(null, 'blablabla', 'link');

Works as I expected, if I click the back button of the browser (or mobile) the site goes back to the last "page" of the site, including scrolling (scroll) for it.

THE PROBLEM: There is now a "page" that opens over the site, enters a div in front of the site with addClass and also gives the window.history.pushState:

$('.frente_site').addClass('frente_site_aparece');
window.history.pushState(null, 'blablabla', 'link-frente');

Then if I go back in the browser the URL changes, but it does not remove it in class '.frente_site'. Would you be able to make it happen? I’m doing it wrong?

  • What exactly are you changing in the url? The hash? ex: www.exemplo.com#div1, www.exemplo.com#div2 etc....?

  • No, I switch from www.site.com.br to www.site.com.br/link-front

1 answer

1


I ended up finding a way, if it’s not ideal let me know!

I used that function:

window.onpopstate = function (event) {
    if(qualLink == 'perfil'){
        // faz algo
    }
}

I only feed the variable qualLink every time I change the URL and then check it with onpopstate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.