Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch mobile safari from full screen web app on iPhone

I've Googled this question in a few variations and can only find answers in the context of using PhoneGap or jQuery mobile. However, I'm using neither... just plain old html and javascript.

I'm trying to launch mobile safari from a full screen web app using window.open()... not an inline anchor. No matter what I do, the url opens in the web app, not in Safari. Does anyone have any suggestions?

Thanks.

like image 402
Vince Avatar asked Oct 11 '22 11:10

Vince


2 Answers

It took me a while but I was able to piece this solution together. In an iOS standalone web app mode, it creates a link element using jQuery, adds it to the body, simulates a click on it, and then removes the link. You can accomplish the same thing without jQuery, it just takes more code using the naive DOM methods.

if (window.navigator.standalone) {
  var $a = $('<a href="' + url + '" target="_blank"/>');
  $("body").append($a);

  var a = $a.get(0);

  var mouseEvent = a.ownerDocument.createEvent('MouseEvents');
  mouseEvent.initMouseEvent('click');
  a.dispatchEvent(mouseEvent);

  $a.remove();
}
else {
  window.open(url, '_blank');
}
like image 194
Steve Avatar answered Oct 19 '22 22:10

Steve


As of iOS 4.3, the only way I know of is to convert the <div> into an <a target="_blank"> and let the default browser to handler it. It launches the new page into an external Safari.

like image 25
Thomas Yip Avatar answered Oct 20 '22 00:10

Thomas Yip