0

我刚刚阅读了这篇SE 帖子,其中解释了所问的问题与我所问的类似。如果可能,Yslow 建议组合所有外部 javascript。根据我在某个网站上找到的一些建议,我实际上已经采用了内部 javascript 并将它们制作成一个外部 .js 文件并将其放在我的 /scripts 文件夹中。但这引出了我的 2 个(类似)问题;我认为不需要单独的帖子。

(1) - 我将如何组合 javascript 代码?只留下一个空行,然后复制并粘贴另一个javascript,依此类推,直到它全部在一个文件中?

(2) - 我有一个带有 5 个母版页的 asp.net 网站。5 个中有 4 个有 6 个 javascript 文件(全部缩小,有些是我自己使用的一些有效的压缩工具)。但是我在 SE 帖子和在线阅读到您应该在页面加载后加载脚本。您如何在 asp.net 网站上执行此操作?我所有的 .js 文件都在

@bluesmoon 给出了一个很好、彻底的答案,但我无法理解:

To load a script after page load, do something like this:

// This function should be attached to your onload handler
// it assumes a variable named script_url exists.  You could easily
// extend it to use an array of scripts or figure it out some other
// way (see note late)
function lazy_load() {
    setTimeout(function() {
            var s = document.createElement("script");
            s.src=script_url;
            document.body.appendChild(s);
        }, 50);
}

This is called from onload, and sets a timeout for 50ms later at which point it will 
add a new script node to the document's body. The script will start downloading after 
that. Now since javascript is single threaded, the timeout will only fire after onload 
has completed even if onload takes more than 50ms to complete.

我真的不明白他在说什么或如何去实施它。就是这样......我只需要帮助组合javascript并在页面加载后加载它们(或它);我在我的母版页或 .aspx 页面中没有看到任何“onload”字样。感谢您为我提供的任何帮助或指导!

4

1 回答 1

1

看看Cassette并在您的结束 body 标签之前渲染您的包

于 2012-11-02T06:47:22.403 回答