Go to anchor before full page loading

Asked

Viewed 124 times

1

I have a page that every time when updating, returns to the top after the full upload. However, I have a second page that has a link as follows: index.html#empresa, I would like it to stop, "regardless of" your shipment, at the anchor enterprise.

Pastebin of index.html

  • I suggest putting part of the code here inside the stackoverflow or use the snippet option.

  • Your question is "How to navigate to an anchor inside the javascript page?"

  • @Leonancarvalho, no. I need that when loading the link index.html#empresa, the same stops at the anchor enterprise and not at the top of the page. Because, the #company parameter via URL, is simply being "disregarded" by the full page load call: $(document).ready(function(e) {});, but I don’t know if this is really it and how this problem is occurring.

  • 1

    @Leonancarvalho, the archive index.html exceeds the allowed character limit for publication.

  • If you navigated to anchor in the on-load page?

  • We can go to the chat? @Leonancarvalho

  • I didn’t see the indicator on your page code

  • I did not see the indicator in your page code. See this example http://kithomepage.com/kit/tutorial/html/hyperlinks.php#indicator

Show 3 more comments

2 answers

0

$(window).load(function(){
    if(window.location.hash) {
        $('html, body').animate({
            scrollTop: $(window.location.hash).offset().top + 'px'
        }, 1000, 'swing');
    }
});

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 the page to load ($(document).ready). But the code must be at the end of the <body>. This is because the page is loaded row by row from top to bottom and the element in question (#company) has already been loaded.

Obs.: It doesn’t work on exactitude time when the page is called. Go depending on the page load speed until you get to the script, as it is not possible to execute a script before normal page loading.

Browser other questions tagged

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