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.
I suggest putting part of the code here inside the stackoverflow or use the snippet option.
– LeonanCarvalho
Your question is "How to navigate to an anchor inside the javascript page?"
– LeonanCarvalho
@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.– lucasbento
@Leonancarvalho, the archive
index.html
exceeds the allowed character limit for publication.– lucasbento
If you navigated to anchor in the on-load page?
– LeonanCarvalho
We can go to the chat? @Leonancarvalho
– lucasbento
I didn’t see the indicator on your page code
– user60252
I did not see the indicator in your page code. See this example http://kithomepage.com/kit/tutorial/html/hyperlinks.php#indicator
– user60252