Two iPhone development tips, and jQuery to the rescue

This post is more than 2 years old.

I've spent the last week or so doing iPhone web development. Let me be clear - that's web development. I'm not actually writing any fancy native applications for the iPhone. While most of my time was spent on just the basic work of the app (and I'll have some server side details on that tomorrow at ArgumentCollection), I wanted to look into what - if any - tweaks were available to on the iPhone web browser. I've found two things that I think are pretty interesting.

The first and simplest tip is working with the view port. (You may need to register at Apple in order to view that link. Two points off to Apple to requiring a login for simple docs!) The view port is a simple meta tag. Here is an example:

<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">

This lets you configure the size of the content on screen when viewed on the iPhone. Read the docs as it takes a bit of play to get it just right, but it can make a major difference in how your web site looks on the iPhone.

The second tip also involves meta tags. While perusing the docs, I found a section on hiding the browser interface components. For web applications saved to the iPhone desktop, this lets you run a page without the normal address bar or status bar at the bottom. This is a great tip! It really does a good job of turning your web page into something closer to a native web app. This can be done with a grand total of two lines of code:

<meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" />

In order to test this, you will need to open your page in Safari (on the iPhone), then add it to your desktop and reopen it. However this feature has a major flaw. As soon as you click a link it will 'break' out of your full screen web application and open a whole new Safari window. But get this - if you do a form post, it won't do this! So normal links are bad, form posts are fine. So when I first tested, and the app prompted me to login, I thought it was all fine, until I clicked a link after logging in. This isn't documented as far as I can tell, which kind of sucks. One Google search turned up a comment from a user who thought that maybe Apple was assuming only Ajax-based applications would use this feature - but if so - I don't get why form posts would work ok.

And this is where jQuery came in. The web application was already done (well, done enough) so it was too late to rewrite it to be Ajax based just for a minor (if cool) UI update. But then I remembered something. jQuery has a feature called selectors (Spry has this as well now!) that lets you select items from the document. You can select on various attributes, including simple all tags of a certain type. If I could select all the links, I could possibly add a click handler.

This is the test script I came up with:

<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <meta name="format-detection" content="telephone=no"> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" />

<script src="/jquery/jquery.js"></script> <script>

function test() { $("#content").load('index3.cfm?log=<cfoutput>#randRange(1,100)#</cfoutput>'); }

function init() { $("a").click(function(e) { $("#content").load(;return false; }); }


</script> <div id="content"> <cfoutput> <p> <a href="index3.cfm?link=#urlEncodedFormat(createUUID())#">link to me, bad</a> </p> <p> <a href="" onClick="document.location.href='test.cfm?link=#urlEncodedFormat(createUUID())#'">link to me, js</a> </p>

<p> <a href="test3.cfm?dontrun=1" onClick="test();return false;">link 3</a> </p>

<p> <a href="index.cfm">Naked Link</a> </p> </cfoutput> <cfdump var="#url#">


So what's going on here? The first link is a vanilla link. If I clicked on it, even though it was linking back to itself, it broke my full screen view. The second link used JavaScript and document.location.href. That also failed. I then tried Ajax. My third link used jQuery to load the contents of a file into a div I wrapped around my main content. Finally I tried one more simpe link, and then added jQuery code to find all the links and edit the click event:

$("a").click(function(e) { $("#content").load(;return false; });

This worked perfectly! In theory I just add this to my main template and all the links I have will now use AJAX to load their content.

I only have one issue left. Spry has a feature that works like the load() function in jQuery. But along with simply loading content, it can also filter to a particular div within the content. If jQuery can do that (I'm a bit tired now but I'm sure about 200 of my readers will chime in), then I can use this to load the page and not reload in the CSS/header/etc.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by marc esher posted on 10/10/2008 at 1:41 PM

you should be able to get what you want with something like this:

$("#mydiv").load("blah.cfm #TheDivIWant");

Comment 2 by Raymond Camden posted on 10/10/2008 at 3:16 PM

That worked perfectly Marc. I did this:

$("a").click(function(e) { $("#content").load(' #content');return false; });

Thank you.

Comment 3 by marc esher posted on 10/10/2008 at 3:22 PM


is this for the cfbloggers iphone page?

Comment 4 by Raymond Camden posted on 10/10/2008 at 3:24 PM

No, but I may use it. I find the view fine right now for CFB. But the full screen stuff may be nice. I also forgot to mention the tip on selecting an icon for your web app. Thats another meta tag. I'll blog that a bit laer.

Comment 5 by Raymond Camden posted on 10/10/2008 at 3:49 PM

New problem. The content loads fine - and just the div. But because I'm not rerunning the "mod my link" again, on the 2nd link it pops a new window. I need to mod my .load func to run the callback and update the links.

Comment 6 by Raymond Camden posted on 10/10/2008 at 3:51 PM

This seems to work just fine:

function init() {
$("a").click(function(e) { $("#content").load(' #content', init);return false; });

Comment 7 by Pollux posted on 11/26/2008 at 12:19 AM

Hi, can you explain me how to do to have the apps always in full screen mode please. thanks.

Comment 8 by Raymond Camden posted on 11/26/2008 at 12:22 AM

Did you not see the meta tags above?

Comment 9 by Rex posted on 12/14/2008 at 3:43 PM

Sorry, just a little help. What I have to add exactly to my web page to make it works!! I downloaded jquery.js I put that in my main folder. I added the line in head part:
<meta name="apple-mobile-web-app-capable" content="yes" />
I linked the jquery plug in
I put the script function (init).
I tried all that in iphone, and the fullscreen works but when I try the link, Safari opens.
Any little help? What I have to write exactly?
Thank you very much!!

Comment 10 by Raymond Camden posted on 12/15/2008 at 2:18 AM

You may want to make sure the function is being run - you can add an alert() and see if the alert runs.

Comment 11 by chris posted on 2/2/2009 at 12:18 AM

There's a much easier way I discovered:

<a href="#" onClick="javascript:window.location.href='targetURL'">Click here to change page within webapp</a>

Works just like a link but keeps you in the webapp.

<meta name="apple-mobile-web-app-status-bar-style" content="black" />
seems unneccessary

Comment 12 by nono posted on 2/14/2009 at 4:48 AM

This doesn't work for me:

<a href="#" onClick="javascript:window.location.href='targetURL'">Click here to change page within webapp</a>

work on first click but not on subsequent (launch Safari).
However this did the trick:

<a href="javascript:window.location.href='MyUrl.html'">Click me</a>

Comment 13 by Sam Nicholson posted on 3/3/2009 at 9:19 PM

So just to be sure, function init() {
$("a").click(function(e) { $("#content").load(' #content', init);return false; });
} will load the content of the URL into the div with the id of content? If so, the same applies for any links which are within the returned content? Sorry if this sounds dumb but I have very little experience with AJAX and none with jQuery.

Comment 14 by Raymond Camden posted on 3/3/2009 at 9:23 PM

As far as I know, yes. Been a while since I looked at this code.

Comment 15 by Sam Nicholson posted on 3/3/2009 at 9:55 PM

So would function init() {
$("#content a").click(function(e) { $("#content").load(' #content', init);return false; });
} apply it to only the A tags within the content div?

Comment 16 by Raymond Camden posted on 3/3/2009 at 10:00 PM


Comment 17 by Sam Nicholson posted on 3/4/2009 at 1:45 AM

Cool. Thanks man, you're a legend. :)

Comment 18 by Raymond Camden posted on 3/4/2009 at 1:59 AM

To be clear, this was something I tested. We did NOT end up using this in production. It has not been tested. Usage of this code does not apply any rights to your person. Using this code may cause drowsiness, spotting, and slight digestive pain. 4 out of 5 patients reported mild death after using this code. As with all code, be sure to consult with your doctor before making use of it. Thank you and have a nice day.

Comment 19 by Sam Nicholson posted on 3/4/2009 at 3:37 PM

Ok. That was a bit random.

Comment 20 by chris posted on 2/10/2010 at 5:07 AM

Hi I realise it has been some time since there was a post here. I implemented your code and it works like a dream. I load content on the same domain/site, but I couldn't get this to work for a remote site. It struck me that this could be an "iframe like" substitute. Would it be possible to load a remote site with a bit of tweaking?

Comment 21 by Raymond Camden posted on 2/10/2010 at 5:09 AM

While iframe's may work, what you really want is JSON/P:

Comment 22 by Jeff King posted on 9/14/2010 at 12:26 AM

Thanks for this solution. It did not meet my needs, but it got me on the right track. I had need to not just load new content, but also make sure any javascript on the page ran.

What I did that worked, is on the click event,

<li>I appended a new form to the body</li>
<li>set the action to the link's href (less any query that was appended)</li>
<li>parsed the query in the link (if present) and created fields as necessary in the new form</li>
<li>and submitted the form</li>

Not exactly elegant, but it works with a new page load for every click.

Comment 23 by Don Alex posted on 1/13/2011 at 1:02 AM

First sorry @ all but my english is bad, but I try my best!

I got a website and im used this things:

<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="apple-touch-startup-image" href="http://***IMAGE_LINK.png***">

and it works great. except one thing, if I'm click on a link, the link start loading but not in the "apple-mobile-web-app-capable"-mode but rather in the normals safari app.

what can i do?

thank you

Comment 24 by Raymond Camden posted on 1/13/2011 at 10:11 PM

You got me there. Are you saying you leave the current browser and open a new one?

Comment 25 by Marc posted on 4/11/2013 at 10:10 PM

Sounds like Don is having exactly the issue you describe and fix in this post. @Don, use AJAX or Ray's code above. You also have the fix in the mobile boilerplate (see lines 24 to 34).