Jquery change location then scroll to element

When I am on the index.php and press the home button, it scroll down to the element. However, when I’m not on the index.php and press the same button, I want to change the URL location, then scroll to the element. This is how I tried:

$('.home').on('click', function(event) {

// If location is index.php do this:
if (location.href.indexOf("/index.php") > -1 ) {

    $('html, body').animate({
        scrollTop: $("#anotherelement").offset().top
    }, 1000);

// If location is not index.php do this:
} else {
     window.location = "index.php";

     $('html, body').animate({
        scrollTop: $("#anotherelement").offset().top
    }, 1000); 
}

});

It just changes the url but it doesn’t scroll to element when I’m not on the index.php


Source: jquery

Leave a Reply