用户代理检测到包含js文件

User agent detection to include js file

本文关键字:js 文件 包含 检测 用户代理      更新时间:2023-09-26

我正在尝试编写一些检测代码。如果用户代理是iPad,那么编写这个javascript,但它不起作用。

<script type="text/javascript">
  if(navigator.userAgent.match(/iPad/i)) {
    document.write('<script type="text/javascript"
       src="photoswipe/code.photoswipe-3.0.4.min.js"></script>');
  }
</script>

有什么想法吗?

试试这个(来自David Walsh的博客:http://davidwalsh.name/detect-ipad):

// For use within normal web clients 
var isiPad = navigator.userAgent.match(/iPad/i) != null;
// For use within iPad developer UIWebView
var ua = navigator.userAgent;
var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);

使用它:

if(isiPad){
  document.write('<script type="text/javascript" src="photoswipe/code.photoswipe-3.0.4.min.js"><'/script>');
}