Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 853
Joshua Soileau Avatar asked Dec 16 '22 06:12

Joshua Soileau


1 Answers

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>
like image 118
frenchie Avatar answered Dec 21 '22 09:12

frenchie