I was working on an iPhone web app recently (my first, actually) and came across an annoyance: any links in a web app with apple-mobile-web-app-capable
set to "yes" will open in Safari, not the app itself. I wanted to keep things as simple as possible, and not have to change my code to support AJAX loading. Some research turned up this article which did almost exactly what I wanted, except I didn't want to load a page into a container. I wanted all links to Just Work how you'd expect links to work. So I dropped this bit of jQuery code into my base template, and everything seems to work great:
$(document).ready( function() {
$('a').each( function(idx,elem) {
var url = $(elem).attr( 'href' );
$(elem).attr( 'href', "javascript:window.location.href='" + url + "';" );
} );
} );
This could probably be done just as easily without jQuery using getElementsByTagName
, but I'll leave that as an exercise for the reader.
Leave a comment