【问题标题】:User agent detection to include js file包含 js 文件的用户代理检测
【发布时间】:2014-05-04 02:19:00
【问题描述】:

我正在尝试编写一些检测代码。如果用户代理是 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>

有什么想法吗?

【问题讨论】:

  • 使用用户代理嗅探是个坏主意,document.write 也是如此(尽管我认为后者仅用于调试?)
  • 尝试分解结束脚本标签并将其连接起来:'+'ipt>'
  • '+'ipt> 做到了。感谢机器人伍兹。 @beardtwizzle 我不是很懂js。

标签: javascript user-agent


【解决方案1】:

试试这个(来自 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>');
}

【讨论】:

    猜你喜欢
    • 2017-12-22
    • 1970-01-01
    • 2014-10-30
    • 2022-11-11
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    相关资源
    最近更新 更多