Use JavaScript to Hide the iPhone Safari Toolbar/Addressbar

| No TrackBacks
iphone-window-scrollto-before-after.pngHappy July!  I apologize for my recent lack of blogagge bloggage (is that a word?); work and life have been keeping me plenty busy and I haven't had much time to sit down and write up any lengthy blog posts.  Well, tonight is the exception.

In the last week, I stumbled across a few mobile sites that hide the iPhone's Safari browser toolbar on page load.  In other words, the little address bar at the top of the browser disappears (automatically scrolls up) once the page finishes loading.  I did a little research to figure out how this worked, and discovered that a simple JavaScript one-liner can take care of this for you:

window.scrollTo(0,1);

According to this post on iPhone Microsites, window.scrollTo(0,1) is the officially documented method for hiding the iPhone toolbar.  On my mobile blog portal at http://mark.kolich.mobi I added this JavaScript one-liner, and sure enough, it does work!  Here's a before and after screenshot (yes, this is a screenshot from an emulator, not an actual iPhone).

Continue reading for some additional HOWTO details ...

1 - Front end

On the front end, I added the window.scrollTo(0,1) to my mobile JavaScript onload event listener as shown here:

if (!window.Kolich) {
Kolich = {};
}

Kolich.Mobile = {};
Kolich.Mobile.init = function(){
window.scrollTo(0,1);
}

if(window.addEventListener){
window.addEventListener('load', Kolich.Mobile.init, false);
}else if(window.attachEvent){
window.attachEvent('onload', Kolich.Mobile.init);
}

When the mobile page loads, the browser will call my init() function which calls window.scrollTo().  Note that I name spaced my JavaScript a bit for readability, but you don't have to.  The window.scrollTo() call will work anywhere as long as it's called once the browser is finished rendering the page.


2 - Back end

On the back end, I'm serving up my mobile blog portal with PHP.  Generally speaking, you don't necessarily want to trigger the window.scrollTo() function call in non iPhone web-browsers.  So, I configured my PHP to only serve up the iPhone JavaScript above if the HTTP User-Agent is that of an iPhone:

<?php

// True if we detect an iPhone, false if not.
$iPhone = (stristr($_SERVER['HTTP_USER_AGENT'],"iphone")!==FALSE);

// If we detect an iPhone, then source the iPhone JavaScript
// into the rendered page content.
if( $iPhone ){
?> <script type="text/javascript" src="/lib/iphone.js"></script> <?
}

?>

Enjoy.

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

Send Mark a Direct Message

If you'd like to send me a direct message, please do so below. However, I do not publicly post comments or messages submitted directly to me. So, if you're going to try to SPAM me, or my blog, you're pretty much wasting your time.

400 characters remaining

Error

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer, a casual entrepreneur, and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

No TrackBacks

No trackbacks attached to this entry.

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on July 8, 2009 11:59 PM.

Whoa, dude what's with the green face icon? #green4iran #twitter was the previous entry in this blog.

Apache Commons HTTPClient: "Response content length is not known" Warning from HttpMethodBase.readResponseBody() is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.