In fact, there are many ways of mobile phone and PC, we often encounter such demand in the construction of responsive design and responsive website. Let's talk about the requirements we encountered in the project:
Different JS customer service codes are loaded on the mobile and PC side (note: The different JS files are loaded separately on the PC and the mobile phone)
First method (Simple Type):
- <script type="Text/javascript" >
- Depending on the screen size
- if (screen && screen.width > 480) {
- document.write (' <script type= ' Text/javascript "charset=" Utf-8 "src="123.js "><\/script>");
- }
- UserAgent judgment
- if (/android|webos|iphone|ipod|ipad| Blackberry/i.test (navigator.useragent)) {
- document.write (' <script type= ' Text/javascript "charset=" Utf-8 "src="123.js "><\/script>");
- }
- </script>
The second method (has been tested, the project is actually in use):
- <script type="Text/javascript" >
- function Browserredirect () {
- var suseragent = Navigator.userAgent.toLowerCase ();
- var bisipad = Suseragent.match (/ipad/i) = = "ipad";
- var Bisiphoneos = Suseragent.match (/iphone os/i) = = "iphone OS";
- var BISMIDP = Suseragent.match (/midp/i) = = "MIDP";
- var bIsUc7 = Suseragent.match (/rv:1.2.3.4/i) = = "rv:1.2.3.4";
- var bisuc = Suseragent.match (/ucweb/i) = = "UCWeb";
- var bisandroid = Suseragent.match (/android/i) = = "Android";
- var bisce = Suseragent.match (/windows ce/i) = = "Windows CE";
- var biswm = Suseragent.match (/windows mobile/i) = = "Windows Mobile";
- Document.writeln ("Your browsing device is:");
- if (Bisipad | | bisiphoneos | | bismidp | | bIsUc7 | | bisuc | | bisandroid | | bisce | | biswm) {
- Alert ("Mobile browsing! ");
- document.write (' <script type= ' Text/javascript "charset=" Utf-8 "src="123.js "><\/script>");
- } Else {
- Alert ("PC browsing! ");
- document.write (' <script type= ' Text/javascript "charset=" Utf-8 "src="123.js "><\/script>");
- }
- }
- Browserredirect ();
- </script>
This way we directly in the PC side and the phone end of the output of different JS. Therefore, we in the mobile phone and the PC side load different JS needs to solve the perfect.
Different JS files are loaded on the mobile and PC side respectively