Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

javascript - Using Media Queries To Only Include JS Files On Mobile

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...