Search code examples
javascriptjqueryajaxgetscript

lock browser while loading external scripts


I'm loading external javascript using jquery $.getScript(url, callback), what I want to achieve is to lock the browser untill the script is loaded, I've tried using $.ajax instead whith async: false but with no better results


Solution

  • Try utilizing $.holdReady()

    $.holdReady(true);
    
    var data;
    
    function callback(response) {
       if (response) {
       data = response;
       $.holdReady(false)
       };
    };
    
    var request = $.getScript(url);
    
    $(document).ready(function() {
      $("body").append(data.result)
    });
    

    $.holdReady(true);
    
    var data;
    
    function callback(response) {
       if (response) {
       data = response;
       $.holdReady(false)
       };
    };
    
    var request = $.getScript("https://gist.githubusercontent.com/guest271314/e2edd364cc20ad7e9e14/raw/f7d7b756005ad6d2b88cf0211f78a2990d7d2dc7/content.json");
    
    $(document).ready(function() {
      $("body").append(data.result)
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>