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

ThinkingStiffAron Woost提出了一个问题:Force link to open in mobile safari from a web app with javascript,或许与您遇到的问题类似。

回答者zvona给出了该问题的处理方式:

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

希望本文对你有帮助,欢迎支持JavaScript中文网