Skip to content

Instantly share code, notes, and snippets.

@scottjehl
Created August 31, 2011 11:42
Show Gist options
  • Save scottjehl/1183357 to your computer and use it in GitHub Desktop.
Save scottjehl/1183357 to your computer and use it in GitHub Desktop.
Normalized hide address bar for iOS & Android
/*
* Normalized hide address bar for iOS & Android
* (c) Scott Jehl, scottjehl.com
* 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(){
setTimeout(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 );
@hedefalk
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

@NeroLang
Copy link

NeroLang commented May 6, 2014

it doesn't work.

@MichelArendt
Copy link

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

@harshamv
Copy link

does this work?

@FabianGabor
Copy link

It doesn't work in Android v4.x

@frontenddeveloping
Copy link

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

@yazeryanski
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