2

on a website, I have these lines on the tag

  <script type="text/javascript" src="http://www.domain.com/js/jquery.min.js"></script>
  <script type="text/javascript" src="http://www.domain.com/js/jquery.colorbox.min.js"></script>
  <script type="text/javascript" src="http://www.domain.com/js/inner-code-colorbox.min.js"></script>

Which are loading JS code, that shouldn't be loaded at all on portable systems

Is there a way to do it?

I tried in this way:

  <script type="text/javascript" media="only screen and (min-width: 950px)" src="http://www.domain.com/js/jquery.min.js"></script>

But it's not working

Thank you in advance

Tormy Van Cool
  • 658
  • 10
  • 29
  • 2
    possible duplicate of [Conditionally load JavaScript file](http://stackoverflow.com/questions/15521343/conditionally-load-javascript-file) – Singular1ty Aug 06 '15 at 06:41
  • possible duplicate of [Call external JS file based on "media screen" value](http://stackoverflow.com/questions/15823137/call-external-js-file-based-on-media-screen-value) – rahimv Aug 06 '15 at 06:46

6 Answers6

8

Replace your script tags with this script tag, which will create the other three script tags ONLY of the window width is larger than a certain amount.

This answer does not rely on jQuery, so you can use it to determine whether you want to load jQuery.

<script>
    if (window.innerWidth > 500) {
        var head = document.getElementsByTagName('head')[0];

        var s1 = document.createElement("script");
        s1.type = "text/javascript";
        s1.src = "http://www.domain.com/js/jquery.min.js";
        head.appendChild(s1);

        var s2 = document.createElement("script");
        s2.type = "text/javascript";
        s2.src = "http://www.domain.com/js/jquery.colorbox.min.js";
        head.appendChild(s2);

        var s3 = document.createElement("script");
        s3.type = "text/javascript";
        s3.src = "http://www.domain.com/js/inner-code-colorbox.min.js";
        head.appendChild(s3);
    }
</script>
Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
  • Thank you so much. Indeed the script above worked perfectly I thank also all the other friends for their suggestions. I implemented this last script just because it was fully complete for my purposes. Grazie :) – Tormy Van Cool Aug 06 '15 at 08:02
4

In your script

 var width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
 var head = document.getElementsByTagName('head').item(0);
 if(width > 950){
       var script = document.createElement("script");
       script.type = "text/javascript";
       script.src = "http://www.domain.com/js/jquery.min.js";
       head.appendChild(script);
    }
Amila Iddamalgoda
  • 4,166
  • 11
  • 46
  • 85
2

Try something like this :

<!DOCTYPE html>
<html>
   <head>
      <script id="waiting"></script>
   </head>
   <body>
      <script>
         (function(){
          if(screen.width > 800) 
             //checks if the screens width is greater than 800
             document.querySelector("#waiting").setAttribute("src", "URL HERE");
         }());
      </script>
   </body>
</html>
mehmetseckin
  • 3,061
  • 1
  • 31
  • 43
Kodejuice
  • 21
  • 2
1

Check on Javascript your screen width and then append your script like this.

if(window.innerWidth > 950){
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = "http://www.domain.com/js/jquery.min.js";
    $("head").append(s);
}

I hope it helps.

Sapikelio
  • 2,594
  • 2
  • 16
  • 40
0

<script> tags don't support the media attribute. You can check the screen resolution with JS and then dynamically inject a script tag if needed. Unlike CSS which can add or remove rulesets based on the current resolution, note the script either runs or not, so if you need some code to change based on current screen dimensions, add checks in the code itself.

<script>
  if (window.innerWidth >= 950) {
    var script = document.createElement('script');
    script.src = 'http://www.domain.com/js/jquery.min.js';
    document.getElementsByTagName('script')[0].insertBefore(script);
  }
</script>
Ronny
  • 4,295
  • 2
  • 24
  • 30
0

You can read about async (dynamic) loading scripts. Before load you can check resolution and load needed scripts.

You can filter list of scripts on the server side (check headers for mobile devices) and include in header only needed scripts.

steppefox
  • 1,784
  • 2
  • 14
  • 19