如果移动设备告诉html doc不要加载js [英] tell html doc not to load js if mobile device

查看:84
本文介绍了如果移动设备告诉html doc不要加载js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很乐意在这里动态寻找一两行代码:

I'm cheekily seeking a line or two of code on the fly here:

有人会善良地提供代码以放置在html doc的开头部分中,该代码指出如果移动设备则不加载JS吗?

Could someone be kind enough to provide code to place in the head section of html doc that says if mobile then do not load JS?

这与以下CSS媒体查询结合使用:

This is being used in conjunction with the following CSS media query:

<link rel="stylesheet" type="text/css" media="only screen and (max-device-width: 480px)" href="m/styles_mobile.css" />

所以我正在寻找基于相同规则的一段代码:media ="only screen and(max-device-width:480px)"

So I'm looking for a piece of code that is based on the same rule: media="only screen and (max-device-width: 480px)"

将非常感谢

推荐答案

给出:如果移动,则不加载JS ",并且假定移动"是由宽度为480的屏幕定义的像素或更少,则应执行以下操作:

Given: "if mobile then do not load JS", and presuming "mobile" is defined by screens with a width of 480 pixels or less, then something like the following should work:

<script>
if (screen && screen.width > 480) {
  document.write('<script type="text/javascript" src="foo.js"><\/script>');
}
</script>

仅当屏幕宽度大于480像素时,才会添加脚本元素.

That will add the script element only if the screen is greater than 480 pixels wide.

OP中的CSS规则是:

The CSS rule in the OP is:

<... media="only screen and (max-device-width: 480px)" ...>

将以480像素或更小的屏幕为目标,这与第一句话相反.因此,如果脚本应在小屏幕上运行而不在大屏幕上运行,请将>更改为<=.

which will target screens of 480 pixels or less, which is contrary to to the first statement. Therefore change > to <= if the script should run on small screens and not run on larger ones.

这篇关于如果移动设备告诉html doc不要加载js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆