function loadJs(file) {
var frm = document.getElementById("frm");
var doc = frm.contentWindow.document;
var scriptTag = doc.getElementById('loadScript');
var head = doc.getElementsByTagName("head")[0];
if(scriptTag)head.removeChild(scriptTag);
script=document.createElement('script');
script.src=file;
script.type='text/javascript';
script.id='loadScript';
head.appendChild(script);
}
function loadCss(file) {
var frm = document.getElementById("frm");
var doc = frm.contentWindow.document;
var cssTag=doc.getElementById('loadCss');
var head=doc.getElementsByTagName('head').item(0);
if(cssTag)head.removeChild(cssTag);
css=document.createElement('link');
css.href=file;
css.rel='stylesheet';
css.type='text/css';
css.id='loadCss';
head.appendChild(css);
}
var frm = document.getElementById("frm");
var doc = frm.contentWindow.document;
var scriptTag = doc.getElementById('loadScript');
var head = doc.getElementsByTagName("head")[0];
if(scriptTag)head.removeChild(scriptTag);
script=document.createElement('script');
script.src=file;
script.type='text/javascript';
script.id='loadScript';
head.appendChild(script);
}
function loadCss(file) {
var frm = document.getElementById("frm");
var doc = frm.contentWindow.document;
var cssTag=doc.getElementById('loadCss');
var head=doc.getElementsByTagName('head').item(0);
if(cssTag)head.removeChild(cssTag);
css=document.createElement('link');
css.href=file;
css.rel='stylesheet';
css.type='text/css';
css.id='loadCss';
head.appendChild(css);
}
本文介绍了一种在网页中动态加载JavaScript和CSS文件的方法。通过创建新的`<script>`和`<link>`元素,并设置其`src`和`href`属性来实现文件的加载。该方法有助于按需加载资源,提高网页性能。

被折叠的 条评论
为什么被折叠?



