Home > Web Front-end > JS Tutorial > body text

How to Detect iPad/iPhone WebViews Using JavaScript?

Patricia Arquette
Release: 2024-10-20 16:52:29
Original
336 people have browsed it

How to Detect iPad/iPhone WebViews Using JavaScript?

Detecting iPad/iPhone WebViews with JavaScript

Question: Can JavaScript detect if a website is running within iPad's Safari browser or an application's WebView?

Answer:

JavaScript provides an efficient way to determine the operating environment. Here's a comprehensive approach that leverages the combination of window.navigator.userAgent and window.navigator.standalone:

<code class="js">var standalone = window.navigator.standalone,
    userAgent = window.navigator.userAgent.toLowerCase(),
    safari = /safari/.test(userAgent),
    ios = /iphone|ipod|ipad/.test(userAgent);

if (ios) {
    if (!standalone && safari) {
        // browser
    } else if (standalone && !safari) {
        // standalone
    } else if (!standalone && !safari) {
        // uiwebview
    }
} else {
    // not iOS
}</code>
Copy after login

Explanation:

This code evaluates the following scenarios:

  • Browser: UserAgent contains "Safari," and Standalone mode is false.
  • Standalone App: Standalone mode is true, and UserAgent does not contain "Safari."
  • UIWebView: Standalone mode is false, and neither "Safari" nor "iOS" is found in UserAgent.
  • Non-iOS Environment: "iOS" is not present in UserAgent.

The above is the detailed content of How to Detect iPad/iPhone WebViews Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!