使用媒体查询仅在移动设备上包含 JS 文件 [英] Using Media Queries To Only Include JS Files On Mobile

查看:19
本文介绍了使用媒体查询仅在移动设备上包含 JS 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 media 属性仅将我的 javascript 文件链接到移动设备,而不是桌面设备.

I am trying to use the media attribute to only link my javascript files on to mobile devices, not on desktop ones.

这是我的链接标签:

<script type="text/javascript" media="screen and (max-width : 850px)" src="mobile.js"></script>

但这不起作用,它链接到 JS 到所有内容.

But this isn't working, it links to JS to everything.

如何只链接符合这些查询条件的 JS 文件?

How do I only link a JS file if it meets those query conditions?

如果我不能,我如何只在移动设备上运行 JS?

If I can't how do I only run JS if it is on mobile?

推荐答案

使用 javascript 怎么样?

How about using javascript for that?

<script type="text/javascript">

if (screen.width < 980) { 

    document.write('<script type="text/javascript" src="mobile.js"></script>');  
 } 

</script>

这篇关于使用媒体查询仅在移动设备上包含 JS 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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