Fix Mobile WebKit target behavior on OS 3.0

25 Dec
2009

This took me hours, maybe it helps someone out there.

On OS 3.1 and up, the MobileSafari Frame works as you would expect. Links are opened in the current frame as there is no possibility to open another tab if the browser is embedded.

But that’s not the case for 3.0. So we add a little bit of javascript magic to fix this.
This brutally changes all link targets to self. You can keep it in 3.1 and up, as it does no harm (and has almost no performance impact)

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"{ var a = document.getElementsByTagName(\"a\");  for (var i=0; i
[webView stringByEvaluatingJavaScriptFromString:@"window.open = function( inurl, blah, blah2 ) {  document.location = inurl; }"];
}

Related posts:

  1. Add Facebook Like Button with Facebook Connect iPhone SDK

Comment Form

top

Switch to our mobile site