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

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

问题描述

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



这是我的链接标签:

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

但是这不工作,它链接到JS的一切。



如果满足这些查询条件,如何只链接一个JS文件?



如果我不能如何只运行JS


b pre> < script type =text / javascript>

if(screen.width< 980){

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

< / script>


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

Here's my link tag:

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

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

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

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

解决方案

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>

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

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