Go to anchor before page load

1

I have a page that every time I refresh, it returns to the top after full loading. However, I have a second page that has a link as follows: index.html#empresa , I would like it to stop, "regardless" of your upload, in the company anchor / p>

Pastebin of index.html

    
asked by anonymous 28.08.2017 / 15:41

2 answers

0

The code below should work for your purpose:

<script>
    curr_url = window.location.href; //pego a URL da página
    if(curr_url.indexOf("#") != -1){ // verifico se existe # na URL
       parametro = curr_url.substring(curr_url.indexOf("#"),curr_url.length); // extraio da URL a string com tudo que vier depois de "#" (inclusive o "#")
       $('html, body').animate({
           scrollTop: $(parametro).offset().top
       }, 10); // desço a janela até a div com o id extraido da string na URL
    }
</script> 

You can use the code without waiting for page load ( $(document).ready ). However, the code must be at the end of <body> . This is because the page loads line by line, from top to bottom and the element in question (#company) has already been loaded.

  

Note: This does not work for the very moment when the page is called. Go   depending on the speed of page loading until you get to the script,   because it is not possible to execute a script before the page loads normal.

    
28.08.2017 / 18:37
0
$(window).load(function(){
    if(window.location.hash) {
        $('html, body').animate({
            scrollTop: $(window.location.hash).offset().top + 'px'
        }, 1000, 'swing');
    }
});
    
29.08.2017 / 00:33