The one below works for me every time.
This site also has several other suggestions, but this carefree, no problem one of them is available on github: gist and answers your question (inserted here for convenience):
function hideAddressBar() { if(!window.location.hash) { if(document.height < window.outerHeight) { document.body.style.height = (window.outerHeight + 50) + 'px'; } setTimeout( function(){ window.scrollTo(0, 1); }, 50 ); } } window.addEventListener("load", function(){ if(!window.pageYOffset){ hideAddressBar(); } } ); window.addEventListener("orientationchange", hideAddressBar );
As far as I can tell, the combination of the extra height added to the page (which caused problems for you) and the scrollTo () operator cause the address bar to disappear.
From the same site, the "simplest" solution to hide the address bar uses the scrollTo () method:
window.addEventListener("load", function() { window.scrollTo(0, 1); });
This will hide the address bar until the user scrolls.
This site puts the same method in a timeout function (the excuse is not explained, but claims that the code does not work without it):
// When ready... window.addEventListener("load",function() { // Set a timeout... setTimeout(function(){ // Hide the address bar! window.scrollTo(0, 1); }, 0); });
Adam Huddleston Jan 02 '13 at 2:48
source share