Home  >  Article  >  Web Front-end  >  How to Detect iPad/iPhone WebViews Using JavaScript?

How to Detect iPad/iPhone WebViews Using JavaScript?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-20 16:52:29340browse

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>

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!

Statement:
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