1

我一切正常,但是在添加了一个外部 javascript 文件(顺便说一下,只有几行短行)之后,它延迟了 popup.html 的加载。

这种延迟很烦人,我认为通过异步加载javascript文件,它会摆脱这种延迟。

该文件写在 popup.html 中,如下所示:

<script src="https://domain.com/myexternalscript.js"></script>

我不确定如何异步加载此文件。那么我该怎么做呢?

4

3 回答 3

1

由于您正在为 Chrome 开发,我确实理解在加载之前内联脚本的问题。我写了这个 AJAX(jQuery) 片段,希望你会发现它有用:

   $.ajax({
            type: "GET", //or post?
            url: "http://FOOBAR.COM",   //change the url obviously..
            datatype: "script", //identify the expected income 
            async: "true", //async is "true" by default, but let's make sure it's #t
            success: function(result) {
                /**now we append the script to the document, nothing too special,
                   just pay attention we inject it INSIDE the item and not as the src**/
                var scr = document.createElement('script');
                scr.innerHTML = result;
                document.body.appendChild(scr)

            },
            error: function(result) { 
            //a simple error handling using the same method we used for the success
                console.log(result) 
                var scr = document.createElement('script');
                scr.innerHTML = "alert('ERROR!')";
                document.body.appendChild(scr)
            }

       }); 
于 2014-11-25T17:32:17.067 回答
0

有两种解决方案:

  1. 将脚本标签移动到 html 页面的末尾。
  2. 使用deferscript 标签上的属性,但是旧浏览器不支持该属性。
于 2013-08-29T18:25:35.710 回答
0

延迟可能是因为源是安全的 http (https) 请求,而不仅仅是普通的 http 请求。根据您的特定托管计划,这可能会或可能不会有所作为。已经提出并提出了论据和证据来支持这一观点和相反的观点。

我认为异步加载文件不会解决您的问题,因为您将发出相同的 http 请求......这就是 AJAX 所做的;它允许我们在不重新加载页面的情况下从服务器请求信息。

如果它只是几行 JS,那么为什么不将它包含在您的 .html 文档中呢?

此外,您还没有很好地描述您遇到的问题。也许你可以详细说明?

于 2013-08-29T18:31:22.637 回答