Home Web Front-end JS Tutorial How to determine client type using JS

How to determine client type using JS

Jun 19, 2018 am 09:36 AM

This article mainly summarizes and introduces four methods of using JS to determine the client type, such as judging the userAgent of the browser, checking whether it is a mobile terminal (Mobile), ipad, iphone, WeChat, QQ, etc. Friends who need it can refer to it. The following

Preface

When we write responsive layout, we always have to consider whether it is On the mobile side, based on this, here are four methods to determine whether the client is ios or android. Share it for everyone’s reference and study. Let’s take a look at the detailed introduction with the editor.

The method is as follows:

1. The first one: by judging the userAgent of the browser, use Regular rules to determine whether it is an ios and Android client

User Agent is called user agent in Chinese. It is part of the Http protocol and is a component of the header domain. User Agent is also referred to as UA. It is a special string header, an identifier that provides the browser type and version, operating system and version, browser kernel, and other information you are using to the visiting website. Through this logo, the websites visited by users can display different layouts to provide users with a better experience or conduct information statistics; for example, accessing Google on a mobile phone is different from accessing on a computer. These are determined by Google based on the UA of the visitor. of. UA can disguise itself.

The standard format of the browser's UA string: browser identification (operating system identification; encryption level identification; browser language) rendering engine identification version information. But each browser is different.

The code is as follows:

<script type="text/javascript">
 var u = navigator.userAgent;
 var isAndroid = u.indexOf(&#39;Android&#39;) > -1 || u.indexOf(&#39;Adr&#39;) > -1; //android终端
 var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
 alert(&#39;是否是Android:&#39;+isAndroid);
 alert(&#39;是否是iOS:&#39;+isiOS);
</script>
Copy after login

2. The second type: check whether it is mobile, ipad, iphone, WeChat, QQ, etc.

2.1 The code is as follows:

<script type="text/javascript">
//判断访问终端
var browser={
 versions:function(){
  var u = navigator.userAgent, 
   app = navigator.appVersion;
  return {
   trident: u.indexOf(&#39;Trident&#39;) > -1, //IE内核
   presto: u.indexOf(&#39;Presto&#39;) > -1, //opera内核
   webKit: u.indexOf(&#39;AppleWebKit&#39;) > -1, //苹果、谷歌内核
   gecko: u.indexOf(&#39;Gecko&#39;) > -1 && u.indexOf(&#39;KHTML&#39;) == -1,//火狐内核
   mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
   ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
   android: u.indexOf(&#39;Android&#39;) > -1 || u.indexOf(&#39;Adr&#39;) > -1, //android终端
   iPhone: u.indexOf(&#39;iPhone&#39;) > -1 , //是否为iPhone或者QQHD浏览器
   iPad: u.indexOf(&#39;iPad&#39;) > -1, //是否iPad
   webApp: u.indexOf(&#39;Safari&#39;) == -1, //是否web应该程序,没有头部与底部
   weixin: u.indexOf(&#39;MicroMessenger&#39;) > -1, //是否微信 (2015-01-22新增)
   qq: u.match(/\sQQ/i) == " qq" //是否QQ
  };
 }(),
 language:(navigator.browserLanguage || navigator.language).toLowerCase()
}
</script>
Copy after login

2.2 How to use

/判断是否IE内核
if(browser.versions.trident){ alert("is IE"); }
//判断是否webKit内核
if(browser.versions.webKit){ alert("is webKit"); }
//判断是否移动端
if(browser.versions.mobile||browser.versions.android||browser.versions.ios){ alert("移动端"); }
Copy after login

2.3 Detect browser language

currentLang = navigator.language; //判断除IE外其他浏览器使用语言
if(!currentLang){//判断IE浏览器使用语言
currentLang = navigator.browserLanguage;
}
alert(currentLang);
Copy after login

3. Determine iPhone|iPad|iPod|iOS|Android client

The code is as follows:

if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
 //alert(navigator.userAgent); 
 window.location.href ="iPhone.html";
} else if (/(Android)/i.test(navigator.userAgent)) { //判断Android
 //alert(navigator.userAgent); 
 window.location.href ="Android.html";
} else { //pc
 window.location.href ="pc.html";
};
Copy after login

4. Determine pc or mobile terminal

The code is as follows:

<script>
  //判断是否手机端访问
 var userAgentInfo = navigator.userAgent.toLowerCase();
 var Agents = ["android", "iphone",
    "symbianos", "windows phone",
    "ipad", "ipod"];
 var ly=document.referrer; //返回导航到当前网页的超链接所在网页的URL
 for (var v = 0; v < Agents.length; v++) {
  if (userAgentInfo.indexOf(Agents[v]) >= 0&&(ly==""||ly==null)) {
   this.location.href=&#39;http://m.***.com&#39;; //wap端地址
  }
 }
</script>
Copy after login

5. Commonly used jump codes

Look at the code

<script type="text/javascript">
 // borwserRedirect
 (function browserRedirect(){
  var sUserAgent = navigator.userAgent.toLowerCase();
  var bIsIpad = sUserAgent.match(/ipad/i) == &#39;ipad&#39;;
  var bIsIphone = sUserAgent.match(/iphone os/i) == &#39;iphone os&#39;;
  var bIsMidp = sUserAgent.match(/midp/i) == &#39;midp&#39;;
  var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == &#39;rv:1.2.3.4&#39;;
  var bIsUc = sUserAgent.match(/ucweb/i) == &#39;web&#39;;
  var bIsCE = sUserAgent.match(/windows ce/i) == &#39;windows ce&#39;;
  var bIsWM = sUserAgent.match(/windows mobile/i) == &#39;windows mobile&#39;;
  var bIsAndroid = sUserAgent.match(/android/i) == &#39;android&#39;;
  var pathname = location.pathname
  if(bIsIpad || bIsIphone || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM || bIsAndroid ){
  window.location.href = &#39;http://m.geekjc.com&#39;+pathname; //wap端地址
  }
 })();
 </script>
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

The problem that beforeRouteLeave cannot be triggered when the browser goes back when using Vue

How to solve the tap "point in the fastclick code "Through"

How to implement animated checkboxes in anime.js

Commonly used components and framework structures in vue (detailed tutorial )

The above is the detailed content of How to determine client type using JS. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles