Home Web Front-end JS Tutorial How does js determine the browser category that opens the project

How does js determine the browser category that opens the project

Mar 09, 2020 am 10:50 AM
js Browser

How does js determine the browser category that opens the project

通过以下方法判断浏览器:

代码如下:

var browser = { 
  versions: function () { 
  var u = navigator.userAgent, app = navigator.appVersion; 
  return { //移动终端浏览器版本信息 
    trident:u.indexOf('Trident') > -1, //IE内核 
    presto: u.indexOf('Presto') > -1, //opera内核 
    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 
    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
    mobile: !!u.match(/AppleWebKit.\*Mobile.\*/), //是否为移动终端
    ios: !!u.match(/\\(i\[^;\]+;( U;)? CPU.+Mac OS X/), //ios终端 
    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器 
    iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器 
    iPad: u.indexOf('iPad') > -1, //是否iPad webApp: 
    u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部 
    }; 
  }(),
  language: (navigator.browserLanguage || navigator.language).toLowerCase() 
}
Copy after login

(推荐教程:javascript教程

以下为在各个浏览器中要执行的操作

代码如下:

if (browser.versions.mobile) {//判断是否是移动设备打开。browser代码在下面 
  var ua = navigator.userAgent.toLowerCase();//获取判断用的对象 
  if (ua.match(/MicroMessenger/i) == "micromessenger") { 
    //在微信中打开 
  } 
  if (ua.match(/WeiBo/i) == "weibo") { 
    //在新浪微博客户端打开 
  } 
  if (ua.match(/QQ/i) == "qq") { 
    //在QQ空间打开 
  } 
  if (browser.versions.ios) {
    //是否在IOS浏览器打开 
  } 
  if(browser.versions.android){ 
    //是否在安卓浏览器打开 
  } 
} else { 
//否则就是PC浏览器打开 
}
Copy after login

更多编程相关内容,请关注php中文网编程入门栏目!

The above is the detailed content of How does js determine the browser category that opens the project. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The width of emsp spaces in HTML is inconsistent. How to reliably implement text indentation? The width of emsp spaces in HTML is inconsistent. How to reliably implement text indentation? Apr 04, 2025 pm 11:57 PM

Regarding the problem of inconsistent width of emsp spaces in HTML and Chinese characters in many web tutorials, it is mentioned that occupying the width of a Chinese character, but the actual situation is not...

How to achieve gap effect on the card and coupon layout with gradient background? How to achieve gap effect on the card and coupon layout with gradient background? Apr 05, 2025 am 07:48 AM

Realize the gap effect of card coupon layout. When designing card coupon layout, you often encounter the need to add gaps on card coupons, especially when the background is gradient...

How to select and style elements of the first specific class using CSS and JavaScript? How to select and style elements of the first specific class using CSS and JavaScript? Apr 04, 2025 pm 11:33 PM

How to select and style elements of the first specific class using CSS and JavaScript? In web development, you often encounter the need to select and modify specific classes...

How to use the clip-path attribute of CSS to achieve the 45-degree curve effect of segmenter? How to use the clip-path attribute of CSS to achieve the 45-degree curve effect of segmenter? Apr 04, 2025 pm 11:45 PM

How to achieve the 45-degree curve effect of segmenter? In the process of implementing the segmenter, how to make the right border turn into a 45-degree curve when clicking the left button, and the point...

How to achieve the gradient effect of CSS fonts? How to achieve the gradient effect of CSS fonts? Apr 04, 2025 pm 10:12 PM

Implementing the CSS font gradient effect Many developers hope to achieve cool font gradient effect on web pages. This article will explain in detail how to use CSS3 to implement the graph...

How to distinguish between closing a browser tab and closing the entire browser using JavaScript? How to distinguish between closing a browser tab and closing the entire browser using JavaScript? Apr 04, 2025 pm 10:21 PM

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...

Vue2 project online iframe white screen: How to troubleshoot and solve it? Vue2 project online iframe white screen: How to troubleshoot and solve it? Apr 05, 2025 am 06:21 AM

Troubleshooting and solving the online white screen of iframe in Vue2 project. In the development of Vue2 project, we often use iframes to embed other web content. However, the item...

How to obtain real-time application and viewer data on the 58.com work page? How to obtain real-time application and viewer data on the 58.com work page? Apr 05, 2025 am 08:06 AM

How to obtain dynamic data of 58.com work page while crawling? When crawling a work page of 58.com using crawler tools, you may encounter this...

See all articles