如果用户使用智能手机/平板电脑或笔记本电脑,我希望加载不同的脚本。
下面是我想要的结果:
笔记本电脑:
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 智能手机和平板电脑
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>我该怎么做呢?
发布于 2013-02-14 06:40:13
您可以通过嗅探User-Agent来检测特定设备,也可以使用browser/screen width来确定您的用户属于哪个类别。
要添加基于浏览器宽度的脚本,您可以这样做:
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
(function ($) {
//check if the browser width is less than or equal to the large dimension of an iPad
if ($(window).width() <= 1024) {
//create an AJAX request for the CSS file
$.ajax({
url : 'jquery.mobile-1.2.0.min.css',
success : function (response) {
$('head').append('<style>' + response + '</style>');
}
});
//create an AJAX request for the JS file, setting the dataType to 'script' will have jQuery automatically evaluate the JS code in the global scope
$.ajax({
url : 'jquery.mobile-1.2.0.min.js',
dataType : 'script'
});
}
})(jQuery);
</script>不过,这有几个警告。
<=1024px范围的,因此平板电脑和桌面用户之间会有一些重叠。无论您如何检测移动/平板设备,CSS/JS都很可能对您起作用。有一些脚本可以检测运行服务器端和客户端的设备,在Google上快速搜索就会找到许多这样的脚本。但是,我没有使用过这些脚本,所以我不能提供太多关于它们用法的指导。
https://stackoverflow.com/questions/14861954
复制相似问题