Search code examples
jqueryloadgetscript

How to getScript only once


So this is a simplified question of this ( which im using for reference, to make this question easy...)

Using resize to getScript if above x pixels (jQuery)

If I call for a function that gets a script, to, if that function fires again, it will not load the script again.

Which in ^ that code it is.

getScript - How to only call if not already called

Is an example, but i tried that code awhile ago.

EX:

$(function() 
{
  var gotscript=false;   
  $(window).resize(function() 
  {
      //Dekstop
      if (window.innerWidth >= 768) 
      {
           if (!gotscript) 
           {
               // GET DESKTOP SCRIPT TO KEEP THINGS QUICK
               $.getScript("js/desktop.js", function() 
               { 
                   gotscript=true;
               });
           }
       }
       if (window.innerWidth < 768) 
       {
           if (window.innerWidth >= 768) 
           {
               if (!gotscript) 
               {
                   // GET DESKTOP SCRIPT TO KEEP THINGS QUICK
                   $.getScript("js/desktop.js", function() 
                   { 
                       gotscript=true;
                   });
               }
            }
       }
   }) .resize(); // trigger resize event
})

This loads the script on any window resize event.

I forgot to mention...

var $window = $(window);

function checkWidth() {
    var windowsize = $window.width();

    ////// LETS GET SOME THINGS IF IS DESKTOP 
    var $desktop_load = 0;
    if (windowsize >= 768) {
        if (!$desktop_load) {
            // GET DESKTOP SCRIPT TO KEEP THINGS QUICK
            $.getScript("js/desktop.js", function() { 
                $desktop_load = 1;
            });
        }
    }
    ////// LETS GET SOME THINGS IF IS MOBILE
    if (windowsize < 768) { 

    } 
}

// Execute on load
checkWidth();

This will work when window size is above 768, and since its only loading this function once, it will not load the script again. What can i do to make the script load if is less then 768, then goes above 768, and not load it again.


Solution

  • So no one came up with a good answer, maybe it was me not explaining it well. Regardless I solved my own issue.

    It will load the script @ 768 or above, and only once. But if the browser loads @ less then 768 then the browser is re-sized above 768 it will load the script, once. I use this to load code blocks and images from within desktop.js by ajax. So when it wasnt working, it would re-load the content, an example was a loading gif that would show up and reset the content. The idea behind this was to stay responsive with css, and use jquery to load bigger loops when needed.

    $.ajaxSetup({
      cache: true
    });
    
    var $window = $(window);
    var onlyonce = 0;
    
    function resizing() {
      var windowsize = $window.width();
      if (windowsize >= 768) {
        getdeskTop();
    }};
    
    function getdeskTop() {
      if (onlyonce === 0) {
        grabScript ();
      } else {
      };
    };
    
    function grabScript() {
      onlyonce = 1;
      $.getScript("js/desktop.js", function() { });
    }
    
    $(window).resize(resizing);
    resizing();
    

    This may be a bit ugly, but I got it to work :).