0

everybody... have a good day... I want to ask that i'm making a code to change some javascript file from the Head section of the Html document, when the browser width got change to different resolutions... But it does not seems working... If you guys tell me what is the problem in this code that will be really helpful for me.. thanks...

Here is the code...

        <script id="size-banner" type="text/javascript" src=" "></script>

        <script type="text/javascript">
            function adjustBanner(width) {
                    width = parseInt(width);
                    if (width < 701) {
                        $("#size-banner").attr("src", " ");
                    } else if ((width >= 960) && (width < 1280)) {
                        $("#size-banner").attr("src", "edge_includes/index_edgePreload.js");
                    } else {
                        $("#size-banner").attr("src", "edge_includes/index1250_edgePreload.js"); 
                    }
                }

                $(function() {
                    adjustBanner($(this).width());
                    $(window).resize(function() {
                        adjustBanner($(this).width());
                    });
                });
        </script>
4
  • 1
    Please describe "not working." Commented Aug 16, 2013 at 23:34
  • ya it suppose to change the files but not changing the src of (<script id="size-banner" type="text/javascript" src=" "></script>) script tag. Commented Aug 16, 2013 at 23:37
  • so what i do to load new script Commented Aug 16, 2013 at 23:42
  • what is "$(function(){..."? Maybe it must be "$(document).ready(function()..." Commented Aug 16, 2013 at 23:54

1 Answer 1

1

If you really and truly want to add a script to your page after it loads, the following code will work:

(function() {
  var po = document.createElement('script');
  po.type = 'text/javascript'; po.async = true;
  po.src = 'http://pathtoyourscript.com/script.js';
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(po, s);
})();

Your final adjustBanner function would be something like:

function adjustBanner(width) {
  width = parseInt(width);

  var src = ' ';
  if ((width >= 960) && (width < 1280)) {
    src = 'edge_includes/index_edgePreload.js';
  } else if (width >= 1280) {
    src = 'edge_includes/index1250_edgePreload.js'; 
  }

  var po = document.createElement('script');
  po.type = 'text/javascript'; po.async = true;
  po.src = src;
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(po, s);
}

What this code is doing is appending a script tag to your page, causing your browser to load it immediately. When the script loads, the JavaScript interpreter in your browser will just go ahead and run the script sources. If you mutate or modify a variable, global, or module in JavaScript, that modification will happen as soon as the JavaScript interpreter receives the sources.

Depending on what your script sources are doing, you might want to be careful about calling this on every window.resize event without cleaning up old scripts (e.g. setting modules to undef). This is because every time that you resize the window, the script associated with the current resolution will be appended to the document sources, mutating any variables already set in JavaScript's context.

Call me crazy, but you might want to consider using CSS media queries instead of JavaScript/jQuery to accomplish what you want. You would define the style for a specific resolution and then magically the browser will do the right thing based on its current window's resolution. Your conditionally loaded scripts (running animations) would need to be resolution-aware but this seems better than swapping out JavaScript on your page.

As an example:

<html>                                                                          
  <style>                                                                       
    @media (max-width: 700px){                                                  
      #smallCage { display: block; }                                            
      #mediumCage { display: none; }                                            
      #largeCage { display: none; }                                             
    }                                                                           
    @media all and (min-width: 701px) and (max-width: 1000px) {                 
      #smallCage { display: none; }                                             
      #mediumCage { display: block; }                                           
      #largeCage { display: none; }                                             
    }                                                                           
    @media all and (min-width: 1001px) {                                        
      #smallCage { display: none; }                                             
      #mediumCage { display: none; }                                            
      #largeCage { display: block; }                                            
    }                                                                           
  </style>                                                                      
  <body>                                                                        
    <div id="smallCage">                                                        
      <img src="http://www.placecage.com/200/300" />                            
    </div>                                                                      
    <div id="mediumCage">                                                       
      <img src="http://www.placecage.com/400/600" />                            
    </div>                                                                      
    <div id="largeCage">                                                        
      <img src="http://www.placecage.com/800/1200" />                           
    </div>                                                                      
  </body>                                                                       
</html>    

In this example, I have 3 different sized images and can render them selectively based on the browser resolution. You could do the same thing with canvas elements or whatever you are using for your JS animations.

A final alternative would be to have the actual JavaScript performing the animation be aware of the browser width and then do the right thing when the screen resizes.

Sign up to request clarification or add additional context in comments.

3 Comments

Ya but don't know how to change js files throught CSS media queries
You are right but i don't want to change styles i want to change Javascript code to function according to display size. Actually it's for banner that animate some graphics elements in it.. So nothing to do with styles... Thanks for your help..
I have update the answer with code for loading a script based on the browser width, however there are some implications you need to consider.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.