gpt4 book ai didi

javascript - 延迟加载javascript

转载 作者:数据小太阳 更新时间:2023-10-29 04:32:49 27 4
gpt4 key购买 nike

<分区>

这3种延迟加载js和按需加载的基本区别是什么,为什么?

脚本 1:

$.getScript = function(url, callback, cache){
$.ajax({
type: "GET",
url: url,
success: callback,
dataType: "script",
cache: cache
});
};

脚本2:

function require(file, callback) {
var script = document.getElementsByTagName('script')[0],
newjs = document.createElement('script');

// IE
newjs.onreadystatechange = function () {
if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
callback();
}
};

// others
newjs.onload = function () {
callback();
};

newjs.src = file;
script.parentNode.insertBefore(newjs, script);
}

document.getElementById('id').onclick = function () {
require('ondemand.js', function () {
extraFunction('loaded from the parent page');
document.body.appendChild(document.createTextNode('done!'));
});
};

脚本 3:

$L = function (c, d) {
for (var b = c.length, e = b, f = function () {
if (!(this.readyState
&& this.readyState !== "complete"
&& this.readyState !== "loaded")) {
this.onload = this.onreadystatechange = null;
--e || d()
}
}, g = document.getElementsByTagName("head")[0], i = function (h) {
var a = document.createElement("script");
a.async = true;
a.src = h;
a.onload = a.onreadystatechange = f;
g.appendChild(a)
}; b;) i(c[--b])
};

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com