Created
April 27, 2012 17:39
-
-
Save tonywok/2511126 to your computer and use it in GitHub Desktop.
Normalized hide address bar for iOS & Android on page load and orientation change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Normalized hide address bar for iOS & Android | |
* (c) Scott Jehl, scottjehl.com | |
* | |
* Modified by | |
* (c) Tony Schneider | |
* | |
* 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; | |
var getScrollTop = function() { | |
return win.pageYOffset || doc.compatMode === "CSS1Compat" && doc.documentElement.scrollTop || doc.body.scrollTop || 0; | |
}; | |
//reset to 0 on bodyready, if needed | |
var bodycheck = setInterval(function(){ | |
if( doc.body ){ | |
clearInterval( bodycheck ); | |
scrollTop = getScrollTop(); | |
win.scrollTo( 0, scrollTop === 1 ? 0 : 1 ); | |
} | |
}, 15 ); | |
function removeBrowserChrome() { | |
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); | |
}; | |
if (win.onorientationchange !== undefined) { | |
win.onorientationchange = removeBrowserChrome; | |
} | |
win.addEventListener( "load", function(){ | |
removeBrowserChrome(); | |
}); | |
} | |
})(this); |
May it serve you well. And if not, I expect to see a fork. :)
lololol. I might just have to immortalize this in the spec.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lol... I was looking for a random gist, and look at whose gist i found!