我有一个网页,需要在移动设备上正确显示。为此,在页面头部加载了JQuery Mobile脚本。头标签如下:
<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>
使用页面主体元素中的data-role属性来显示内容。
该页面在移动设备上看起来很好,但即使请求来自非移动浏览器,它看起来也类似。
我想知道是否有人知道一种方法,只在请求来自移动设备时加载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;
}