利用JS简单判断IOS/安卓/PC客户端并运行指定代码

流氓凡 PHP程序源码 2019-02-24 5.17 K 0
  <script>
  if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
  //alert(navigator.userAgent); 
  window.location.href = "iphone.html";//此处更改想要在IOS端运行的JS代码
} else if (/(Android)/i.test(navigator.userAgent)) {  //判断Android
  //alert(navigator.userAgent); 
  window.location.href = "android.html";//此处更改想要在安卓端运行的JS代码
} else { //pc
  window.location.href = "pc.html";//此处更改想要在PC端运行的JS代码
};
  </script>

当然,简单的判断很容易出现个别情况失效的问题,需要更加复杂的js判断跟随,以后有机会会分享的。

评论