Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Normalized hide address bar for iOS & Android
* Normalized hide address bar for iOS & Android
* (c) Scott Jehl,
* MIT License
(function( win ){
var doc = win.document;
// If there's a hash, or addEventListener is undefined, stop here
if( !location.hash && win.addEventListener ){
//scroll to 1
window.scrollTo( 0, 1 );
var scrollTop = 1,
getScrollTop = function(){
return win.pageYOffset || doc.compatMode === "CSS1Compat" && doc.documentElement.scrollTop || doc.body.scrollTop || 0;
//reset to 0 on bodyready, if needed
bodycheck = setInterval(function(){
if( doc.body ){
clearInterval( bodycheck );
scrollTop = getScrollTop();
win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
}, 15 );
win.addEventListener( "load", function(){
//at load, if user hasn't scrolled more than 20 or so...
if( getScrollTop() < 20 ){
//reset to hide addr bar at onload
win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
}, 0);
} );
})( this );
Copy link

ambientp commented Dec 9, 2011

Hi this is my first time using github and I may be doing it wrong

If I put the raw code inside script tags in the head or body nothing happens
if I use the embed code it seems to give the desired effect only the code appears on the pages as the page loads and then disappears once the content is loaded. If the page is not long enough then the code is visible after where the page ends.

I would really appreciate some advice on this. I've spent a long time looking for an address bar solution and this is the only one that comes close


Copy link

@ambientp The code I pasted was raw from my project, and the hideAddressBar function is part of an object. If you want to use it in the most vanilla sense do this:

<script type="text/javascript"> $(function(){ hideAddressBar(); }); function hideAddressBar(){ if (navigator.userAgent.match(/Android/i)) { window.scrollTo(0,0); // reset in case prev not scrolled var nPageH = $(document).height(); var nViewH = window.outerHeight; if (nViewH > nPageH ) { nViewH = nViewH / window.devicePixelRatio; $('BODY').css('height',nViewH + 'px'); } window.scrollTo(0,1); }else{ addEventListener("load", function() { setTimeout(hideURLbar, 0); setTimeout(hideURLbar, 500); }, false); } function hideURLbar(){ if(!pageYOffset){ window.scrollTo(0,1); } } return this; } <script> Also be sure to include jQuery in your project BEFORE this script tag.

Copy link

ambientp commented Dec 9, 2011

Hi @Fresheyeball
I copy/pasted it just before the and it doesn't seem to be working.
I'm kinda new to all this, is there anything else I need to do to get it to work?
Also I'm testing this on an itouch. Not sure if that makes a difference or not

Copy link

@ambientp can I get the url of your site? You can contact me on skype at skype id = 'fresheyeball'

Copy link

ambientp commented Dec 10, 2011 via email

Copy link

@ambientp, I just tested your site on ios4 iso5 and android 3. and the address bar got hidden.

Copy link

ambientp commented Jan 3, 2012 via email

Copy link

kreaninw commented Feb 2, 2012

Thanks for the code. At first I tested and the code didn't work, I though it has to be something wrong. And just to note that, if your site contains an event that use jQuery scrollTop to y 0, it will override the code and cause it not to work on Android. So, I added +1 to my scrollTop value and it solved the issue completely.

Copy link

Not working :/

Copy link

It's working perfectly(not test on ICS yet). The other thing that it won't work is your page is too shot.

Copy link

There's a repo here with more recent code, fwiw

Copy link

chriv commented Apr 30, 2012

I swear, I cannot get any version of this to work. window.scrollTo(0,0) ; window.scrollTo(0,1) ; hideAddressBar() above ; hideaddressbar.js ; anything. here's where I'm trying it: . Cannot imagine what I'm doing wrong. I keep uploading code to my server and I test it in four browsers:

  • Android (ICS) Browser (usually fails)
  • Android Dolphin HD Browser (only scrolls 1 pixel or nothing)
  • Android Chrome Beta Browser (doesn't scroll)
  • Android FireFox Browser (not beta) (worse than doesn't scroll - extends page too far)
    I don't have an iPhone to test on.
    I'm open to suggestions. I've been tweaking code to do this for 2 months (no, it's not the only thing I've been coding).

Copy link

hedefalk commented Mar 1, 2013

Does this work in ios6? I had a simple window.scrollTo( 0, 1 ); that works in <= ios5 but not in ios6.

Thanks, Viktor

Copy link

NeroLang commented May 6, 2014

it doesn't work.

Copy link

Apparently, not working properly anymore. It adds extra padding...

Copy link

does this work?

Copy link

It doesn't work in Android v4.x

Copy link

This is outdated. Thanks for this snippet. Works on iOS 7- only.

Copy link

2020 | That does not work (

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment