Jquery function works in safari, but not in chromal

I have a function where I view the page at the top when the page loads using document.ready() . However, in Chrome, this function only works when I am on the page and I update it.

If I access the page by entering a link in the address bar, the function does not start. If I access a page from a link from another page, it also does not start. The only way it works 100% of the time in Chrome is if I am on the page and refreshing it.

However, in Safari, the function is launched 100% every time, accessing the page from a link, entering it in the address bar upon rebooting.

This is my code:

 jQuery(document).ready(function($) { if (location.hash) { // do the test straight away window.scrollTo(0, 0); // execute it straight away setTimeout(function() { window.scrollTo(0, 0); // run it a bit later also for browser compatibility }, 1); //location.reload(); } }); 

This also works in Safari, not putting it in document.ready() , but again not in Chrome.

Does anyone know what causes this, and if it can be fixed? I used to have this problem and it was very unpleasant.

+7
javascript jquery
source share
1 answer

Just increase the setTimeout delay time by 20 ms or more by more than 20 ms, possibly because the interval time is too short because of 1 ms.

 $(document).ready(function() { if (location.hash) { window.scrollTo(0, 0); setTimeout(function() {window.scrollTo(0, 0);}, 20); } }); 
+1
source share

All Articles