简体   繁体   English

如何让浏览器等待资产加载?

[英]how to make the browser wait for assets to load?

I am loading scripts and style-sheets dynamically from javascript like this . 我从像JavaScript加载脚本和样式表动态

The problem is that browser does not wait for the script to load. 问题是浏览器不等待脚本加载。

consider i have a function named functionToBeCalled() inside a script file named script-file.js 我认为我在一个名为script-file.js的脚本文件中有一个名为functionToBeCalled()的函数

i have a function to load script file. 我有一个加载脚本文件的功能。

<script type="text/javascript">
   var listOfJavaScriptsLoaded = new Array();

    function LoadScriptFile(scriptUrl){
      var isScriptLoaded = false;
      var i = 0;
      for(i = 0; i < listOfJavaScriptsLoaded.length; i ++){
        if(listOfJavaScriptsLoaded[i] == scriptUrl){
           isScriptLoaded = true;
           break;
         }  
      } 
    if(isScriptLoaded == false){        
     var headTag= document.getElementsByTagName('head')[0];      
     var scriptTag= document.createElement('script');
     scriptTag.type= 'text/javascript';
     scriptTag.src= scriptUrl;
     headTag.appendChild(scriptTag);
     listOfJavaScriptsLoaded.push(scriptUrl);
    }
    }
   LoadScriptFile("script-file.js");
   functionToBeCalled();
</script>

now, what happens is that the browser does not wait for the script tag to load and goes to the next command. 现在,会发生什么是浏览器不等待脚本标记加载并转到下一个命令。 I get a " undefined functionToBeCalled() " error. 我收到“ 未定义的functionToBeCalled() ”错误。 this is natural. 这很自然。 But the fact is that when i inspect in firebug, the script tag has been formed and the file has loaded. 但事实是,当我在firebug中检查时,脚本标签已经形成并且文件已经加载。

So how do i make the browser to pause loading and resume after the asset has been loaded? 那么如何让资源加载后浏览器暂停加载和恢复?

Edit1: This problem occurs only when i am loading the page in ajax and not in normal page loads Edit1:仅当我在ajax中加载页面而不是在正常页面加载时才会出现此问题

Edit2: Or is there a possibility to read a script/css file from javascript and write it directly in the markup within script tags Edit2:或者是否有可能从javascript中读取脚本/ css文件并将其直接写入脚本标记内的标记中

If i use window.stop() the loading stops completely. 如果我使用window.stop(),则加载完全停止。 how can i make it resume from the same line? 我如何让它从同一行恢复?

Or is it possible to make the browser to consider that the loading is still happening and reset it in the onload event? 或者是否可以让浏览器考虑加载仍在发生并在onload事件中重置?

You may have specific reasons to load the script dynamically, but to present the option, if you write out the script element in your HTML output like so: 如果您在HTML输出中写出脚本元素,则可能有特定的理由动态加载脚本,但要显示该选项:

<script src="script-file.js"></script>
<script>functionToBeCalled();</script>

the browser will halt parsing until that script has been loaded, and interpreted. 浏览器将暂停解析,直到该脚本加载并解释为止。

This is also valid in the BODY . 在BODY中也有效。

Check out LABjs ( http://labjs.com/ ) by Getify Solutions. 查看Getify Solutions的LABjs( http://labjs.com/ )。 LABjs allows script-inserted scripts to be loaded concurrently but run in order. LABjs允许同时加载脚本插入的脚本,但按顺序运行。

pretty much every tag which loads a resource has an onload event. 几乎每个加载资源的标签都有一个onload事件。 so in plain javascript this means in your case something like this: 所以在普通的javascript中这意味着在你的情况下这样的事情:

var s = document.createElement('script');
s.src = "script-file.js";
s.type = 'text/javascript';
head.appendChild(s);

s.onload = function(){
    functionToBeCalled();
}

I would recommend looking at Cuzillion . 我建议看看Cuzillion It will allow you to experiment with calling and in many different ways to see how they react in the browser. 它允许您尝试以多种不同方式调用 ,以查看它们在浏览器中的反应。

This should answer your question. 这应该回答你的问题。 Just execute it before your page is done loading the body . 之前做你的网页加载只是执行它body

<script type="text/javascript">
    var loadScriptFile = (function(){
        var listOfJavaScriptsLoaded = [];

        return function(scriptUrl){
            var isScriptLoaded = false;
            for(var i = 0; i < listOfJavaScriptsLoaded.length; i ++){
                if(listOfJavaScriptsLoaded[i] == scriptUrl){
                    isScriptLoaded = true;
                    break;
                }
            }
            if(!isScriptLoaded){
                document.write('<scr' + 'ipt type="text/javascript" src="' + scriptUrl + '"></scr' + 'ipt>');
            }
        };
    }());
    loadScriptFile("script-file.js");
    functionToBeCalled();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM