javascript - 仅为移动设备加载 JQuery Mobile 脚本

标签 javascript mobile jquery-mobile

我有一个网页必须在移动设备上正确显示。 为此,我在页面头部加载了 JQuery Mobile 脚本。 head 标签如下所示:

<head> 
    <title>Page Title</title> 

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>

并在页面的正文元素中使用数据 Angular 色属性来显示事物。 这些页面在移动设备上看起来相当不错,但即使请求来自非移动浏览器,它看起来也很相似。

我想知道是否有人知道仅当请求来自移动设备时才加载 JQuery Mobile 脚本的方法。

到目前为止我尝试的是使用一个函数来检测我的用户代理并加载脚本(如果它是移动设备):

function init() {

    if(isMobile()) {
        document.write('<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />');
document.write('<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>');
dcument.write('<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>');
    }
}


function isMobile() {

    if(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/iPhone|iPad|iPod/i) || navigator.userAgent.match(/IEMobile/i))
            return true;
        return false;
}

最佳答案

很难检测您的设备是手机还是平板电脑还是带触摸的大屏幕, 但从

开始
  1. 使用脚本检测 http://detectmobilebrowsers.com/
  2. 使用 http://yepnopejs.com/ 进行测试

像这样(应该适用于 http://detectmobilebrowsers.com/ 的 jQuery 脚本):

yepnope({
  test : jQuery.browser.mobile,
  yep  : 'mobile_specific.js',
  nope : ['normal.js','blah.js']
});

编辑:

另请查看 https://github.com/borismus/device.js , 用于根据条件加载内容。我不确定它是否允许您加载条件 JS 文件。

关于javascript - 仅为移动设备加载 JQuery Mobile 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341956/

相关文章:

css - jQuery Mobile 弹出窗口宽度因里面的内容而异?

移动设备的CSS调整

javascript - Chartjs - 图形值类型 float

javascript - jQuery UI 按钮在 IE 7 上不起作用

android - 移动应用程序是否相互共享缓存并与默认浏览器共享缓存

android - 如何通过免费发送OTP来验证手机号码

android - Cordova Google Map 插件会降低 Android 上的应用程序性能

javascript - 如何为计算器 HTML 设置按钮样式

javascript - 我想通过for循环使用jquery创建标签

jquery - 将页面更改为外部对话框时圆角丢失