Force link to open in mobile safari from a web app with javascript

Aron Woost picture Aron Woost · Oct 28, 2011 · Viewed 49.1k times · Source

When calling window.open() in a iOS web app, the page opens in the web app instead of mobile safari.

How can I force the webpage to open in mobile safari?

Note: Using straight <a href> links is not an option.

Answer

Samuli Hakoniemi picture Samuli Hakoniemi · Jan 12, 2012

This is possible. Tested with iOS5 stand-alone web app:

HTML:

<div id="foz" data-href="http://www.google.fi">Google</div>

JavaScript:

document.getElementById("foz").addEventListener("click", function(evt) {
    var a = document.createElement('a');
    a.setAttribute("href", this.getAttribute("data-href"));
    a.setAttribute("target", "_blank");

    var dispatch = document.createEvent("HTMLEvents");
    dispatch.initEvent("click", true, true);
    a.dispatchEvent(dispatch);
}, false);

Can be tested here: http://www.hakoniemi.net/labs/linkkitesti.html