如何在桌面显示时附加普通jQuery,在移动端显示时附加jQuery Mobile ?

How can I attach plain jQuery when displayed on the desktop and attach jQuery Mobile when displayed on mobile?

本文关键字:jQuery 显示 移动 Mobile 桌面      更新时间:2023-09-26

我正在建立一个网站,我想使用jQuery移动做javascript时,在移动设备上,但由于jQuery移动不支持所有桌面浏览器,但我想要正常的jQuery附加。我该怎么做呢?

下面是在客户端执行此操作的一种方法:

<html>
    <head>
        <script type="text/javascript">
              function isMobileDevice() {
                var index = navigator.appVersion.indexOf("Mobile");
  return (index > -1);
              }
              function loadJQuery() {
                if(isMobileDevice())
                     document.write("<script type='text/javacript src='"+path_to_mobile_jquery+"'></script>");
                else
document.write("<script type='text/javacript src='"+path_to_normal_jquery+"'></script>");
              }
        </script>
    </head>
    <body onload="loadJQuery();">
      <!-- Content -->
    </body>
</html>

也许建立一个可能的浏览器类型的枚举,然后对window.navigator.userAgent进行测试?您可以使用Modernizr之类的库来测试功能,但这并不能让您获得实际的浏览器类型。没有userAgent prop来识别浏览器类(桌面,平板电脑,手机等),所以你只能做一个愚蠢的UA比较。

像51°这样的项目旨在简化这种评估,但它们会产生隐藏的成本——即51°需要10秒以上的时间来评估客户端浏览器并相应地重定向。当然,在您的情况下,您需要条件样式—而不是重定向。这表明window.navigator.userAgent检查可能适合if(true) document.write('<javascript resource />')类型的方法。

您必须在服务器上测试浏览器请求头(User-Agent),然后相应地绑定框架:

。看到http://pgl.yoyo.org/http/browser-headers.php用户代理:Mozilla/5.0 (Windows;U;Windows NT 6.1;德;rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18