javascript中延遲加載的7種方法實(shí)現(xiàn)
延遲加載javascript,也就是頁面加載完成之后再加載javascript,也叫on demand(按需)加載,一般有一下幾個(gè)方法:
1. DOM
head append script tag
window.onload = function() {
setTimeout(function(){
// reference to <head>
var head = document.getElementsByTagName('head')[0];
// a new CSS
var css = document.createElement('link');
css.type = "text/css";
css.rel = "stylesheet";
css.href = "new.css";
// a new JS
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "new.js";
// preload JS and CSS
head.appendChild(css);
head.appendChild(js);
// preload image
new Image().src = "new.png";
}, 1000);
};2. document.write
<script language="javascript" type="text/javascript">
function include(script_filename) {
document.write('<' + 'script');
document.write(' language="javascript"');
document.write(' type="text/javascript"');
document.write(' src="' + script_filename + '">');
document.write('</' + 'script' + '>');
}
var which_script = '1.js';
include(which_script);
</script>3. Iframe
和第一種類似,但是script tag是放到iframe的document里面。
window.onload = function() {
setTimeout(function(){
// create new iframe
var iframe = document.createElement('iframe');
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("name", "preload");
iframe.id = "preload";
iframe.src = "about:blank";
document.body.appendChild(iframe);
// gymnastics to get reference to the iframe document
iframe = document.all ? document.all.preload.contentWindow : window.frames.preload;
var doc = iframe.document;
doc.open(); doc.writeln("<html><body></body></html>"); doc.close();
// create CSS
var css = doc.createElement('link');
css.type = "text/css";
css.rel = "stylesheet";
css.href = "new.css";
// create JS
var js = doc.createElement("script");
js.type = "text/javascript";
js.src = "new.js";
// preload CSS and JS
doc.body.appendChild(css);
doc.body.appendChild(js);
// preload IMG
new Image().src = "new.png";
}, 1000);
};4. Iframe static page
直接把需要加載東西放到另一個(gè)頁面中
window.onload = function() {
setTimeout(function(){
// create a new frame and point to the URL of the static
// page that has all components to preload
var iframe = document.createElement('iframe');
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("frameborder", "0");
iframe.src = "preloader.html";
document.body.appendChild(iframe);
}, 1000);
};5. Ajax eval
用ajax下載代碼,然后用eval執(zhí)行
6. Ajax Injection
用ajax下載代碼,建立一個(gè)空的script tag,設(shè)置text屬性為下載的代碼
7. async 屬性(缺點(diǎn)是不能控制加載的順序)
<script src="" async="true"/>
到此這篇關(guān)于javascript中延遲加載的7種方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)javascript 延遲加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js實(shí)現(xiàn)QQ面板拖拽效果(慕課網(wǎng)DOM事件探秘)(全)
這篇文章主要為大家詳細(xì)介紹了QQ面板拖拽效果,探秘慕課網(wǎng)DOM事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
基于Electron實(shí)現(xiàn)桌面應(yīng)用開發(fā)代碼實(shí)例
這篇文章主要介紹了基于Electron實(shí)現(xiàn)桌面應(yīng)用開發(fā)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
JavaScript實(shí)現(xiàn)PC端橫向輪播圖
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)PC端橫向輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
在knockoutjs 上自己實(shí)現(xiàn)的flux(實(shí)例講解)
下面小編就為大家分享一篇在knockoutjs 上自己實(shí)現(xiàn)的flux方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
js打造數(shù)組轉(zhuǎn)json函數(shù)
這里給大家分享的是一段使用js實(shí)現(xiàn)數(shù)組轉(zhuǎn)換成json的函數(shù)代碼,代碼簡潔易懂,并附上了使用方法,小伙伴們拿去試試。2015-01-01

