public
Last active

Normalized hide address bar for iOS & Android

  • Download Gist
hideaddrbar.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
* 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 );

what's the purpose of having the y pos to be either 0 or 1? is it better than just scroll to 0?

In testing, we found that Android uses 1px y pos for hiding, while it's 0 in iOS. This is meant to normalize the two.

On Sep 10, 2011, at 2:56 AM, Shi Chuan wrote:

what's the purpose of having the y pos to be either 0 or 1? is it better than just scroll to 0?

Reply to this email directly or view it on GitHub:
https://gist.github.com/1183357

Hi Scott,
the logical operators in line 6 are not making much sense to me:
if( !location.hash || !win.addEventListener ){

If there is no location hash, the first operand is true and the second operand is skipped. So the if clause will be executed even if win.addEventListener is undefined. I think it should read:
if (!location.hash && win.addEventListener ) {

Hey Scott,
I just implemented htm5bp mobile template and I called the helper.js function MBP.hideUrlBar() at the bottom of the page after MBP.scaleFix();.

Issue:
I did a quick test on a short page that has no scroll bar, and I noticed that the URL bar did not go away.
This test was done on my iPhone 4.
However, when I added a lot of white space so the page grew in height and a scroll bar showed, right away, this function worked and the URL bar was hidden.

Do you think this is a bug?

Tested in iOS. Url bar is removed on refresh, but not on initial load.

I like it simple (and stupid...):

MBP.hideUrlBar = function () {
if (!location.hash && window.addEventListener) { // changed due to De Morgans Laws...;-)
var zero_or_one = ( /iPhone|iPad/.test(MBP.ua) && !/Opera Mini/.test(MBP.ua) ) ? 0 : 1;

window.addEventListener( "load", function(){
setTimeout(function(){
if( window.pageYOffset < 20 ){
window.scrollTo( 0, zero_or_one );
}
}, 0);
}, false );
}
};

with MBP.ua = navigator.userAgent

Thanks, but the goal here was to detect the behavior and react accordingly, not just detect the UA and assume it'll always behave this way. That UA test above will also include non-safari browsers, like Opera Mini; not sure that'd be intentional.

Hi Scott, thanks for the response. I have added the part to exclude opera mini. In mobile boilerplate there is already the UA detection due to the iPhone viewport scale bug, so I thought it makes sense to use something we already have. But I must admit your approach with detecting the behavior is naturally much cooler and also not depending on other code.

After analyzing this code I found some issues that in my opinion need some more discussion.

1) Line 6
( !location.hash || !win.addEventListener ) is according to De Morgans Laws equivalent to:
!( location.hash && win.addEventListener ) and this is probably not, what we want to have here. I think the idea is to stop, when one (or both) of the operands are false. So I suggest to replace line 6 with: ( !location.hash && win.addEventListener )

2) Line 12
The getScrollTop function always returns 0 because of a hard to catch error. This results always in a final y offset of 1, also on iOS! Well, 1 pixel is hard to see and the url bar is hidden. But our intention was to detect the behaviour and hide the bar on iOS with 0 pixel. But why is the function failing? Firstly, "scrollTop" in doc.body is always true. So always doc.body.scrollTop is returned. And this is always 0! It seems to be a mobile browser implementation issue. The idea here was to set scrollTop and get the vertical scroll position with it. But this is not working. If you do something like:

window.document.body.scrollTop = 99;
var x = window.document.body.scrollTop;

x remains 0 on all mobile browsers I have tested so far. So I suggest to replace Line 12 with:
return ( win.pageYOffset === doc.body.scrollTop ) ? 1 : 0;

Now we get 1 as return value on iOS and 0 on other systems. Frankly speaking, the return value depends more on the browser than of the OS. And yes, we can hide the url bar with a vertival scroll of 0 on Android! Eg. the Dolphin Browser is very happy with zero!

Through our change in line 12 we don't get the „hypothetical“ scroll position that line 27 expects. So I change line 27 to:
if( !( ( win.pageYOffset || doc.body.scrollTop ) > 20 ) ){

Some browsers deliver the first operand, others the second and even some both. With the implementation above we are happy whatever we get.

@joshuakim
If you have no space to scroll you can't scroll. It's that simple. I don't consider this a bug. Seems to be very reasonable...;-)
But you can analyze the size of you page and add something to the bottom if needed. You can add this little snippet to make it work:

if ( (doc.body.clientHeight - 100) < win.innerHeight ) {
var pageFill = doc.createElement("DIV");
pageFill.style.height = ( win.innerHeight - doc.body.clientHeight + 100 ) + "px";
doc.getElementsByTagName('body')[0].appendChild( pageFill );
};

After that the url bar is hidden as wanted. I have uploaded my version of the „hide url bar“-problem https://gist.github.com/1257461 which also looks at the screen width to avoid 1 px scrolling on desktop browsers. You might delete this part and also the page extension part above if not needed. Both are very rough approaches that probably need more fine tuning but they are ok for the web project I am working on at the moment .

Anyway, the basic idea behind Scott's approach is great and I learned a lot by analyzing it. I guess my approach is far away from beeing perfect or errorfree, so let me know what you think about it and what to improve. Thanks!

@JulesNeck -
Yes, I realized that afterwards & was trying to set some sort of min-height with CSS and somehow add the height of the URL bar, but failed to get it working, so I'll definitely try out your js code. Thanks you!
~ JK ~

Is this going to make it into jQuery Mobile?

I wrote this. Works on iOS and Android:

hideAddressBar : function(){
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;
}

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

Thanks

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

$(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;
}

Also be sure to include jQuery in your project BEFORE this script tag.

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

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

Hi Isaac,
I can't get on skype right now but the url is http://bit.ly
This is my first time trying a mobile site so I've probably done something
silly
Thanks for helping me out with this :)

On Sat, Dec 10, 2011 at 8:07 PM, Isaac Shapira <
reply@reply.github.com

wrote:

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


Reply to this email directly or view it on GitHub:
https://gist.github.com/1183357

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

Thanks for that Isaac. The mobile sites still a bit of a mess, a work in
progress. Appreciate the help!!

On Sat, Dec 24, 2011 at 2:52 AM, Isaac Shapira <
reply@reply.github.com

wrote:

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


Reply to this email directly or view it on GitHub:
https://gist.github.com/1183357

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.

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

There's a repo here with more recent code, fwiw https://github.com/scottjehl/Hide-Address-Bar

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

Thanks, Viktor

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.