JavaScript implementation prevents links in iOS app from opening Safari browser _javascript tips

Source: Internet
Author: User

Last time I got a web APP for my website based on the tutorials on the web, but it was very bad for the users.

Problem Description:

What's wrong? It turns out that when you open the Web app and open the connection on the home page, you'll open the Safari browser. The original good disguise and mood are all destroyed. What's the best way to do that? The original solution is very simple. Just add the code. The test was passed on my iphone (iOS 7.1) and ipod (iOS 6.1.4), according to the original author's account, the latest IOS 7.0.4 (IPhone and IPad) test passed, the code should be good compatibility, here to share:

Problem solving:

Copy Code code as follows:

<script type= "Text/javascript" >
The question of clicking the link in the IOS Web app to jump to the new tab page of Safari browser
if (("standalone" in Window.navigator) && Window.navigator.standalone) {
var noddy, remotes = false;
Document.addeventlistener (' click ',
function (event) {
Noddy = Event.target;
while (Noddy.nodename!== "A" && noddy.nodename!== "HTML") {
Noddy = Noddy.parentnode;
}
if (' href ' in Noddy && noddy.href.indexOf (' http ')!==-1 && (Noddy.href.indexOf (document.location.host) !==-1 | | Remotes)) {
Event.preventdefault ();
Document.location.href = Noddy.href;
}
},
FALSE);
}
</script>

It is recommended to put the code before the/head tag, of course, another save as a JS file reference is also possible.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.