Given: βif mobile, and then does not load JS, β and assuming that βmobileβ is defined by screens with a width of 480 pixels or less, then the following should work:
<script> if (screen && screen.width > 480) { document.write('<script type="text/javascript" src="foo.js"><\/script>'); } </script>
This will add a script element only if the screen size is more than 480 pixels.
CSS rule in OP:
<... media="only screen and (max-device-width: 480px)" ...>
which will target screens of 480 pixels or less, which contradicts the first statement. Therefore, change > to <= if the script should run on small screens and not run on large ones.
Robg
source share