动态加载外部css或js文件
网站建设 2023-01-28 21:38www.1681989.com免费网站
原理解析第一步使用dom创建<script>或者<lk>标签,并给他们附加属性,如type等第二步使用appendChild方法把标签绑定到另一个标签,一般是绑到<head>.
应用1、提高代码的复用,减少代码量;2、添加一个javascript控制器和 session可以实现动态改变页面样式;3、由于是页面是从上到下依次加载文件的,并且边加载边解释,所以可以添加javascript控制器控制页面文件的加载顺序,如先加载css布局文件,再显示有图片的css美化文件,之后再加载大的falsh文件,或者安内容的重要性来加载。\r
阅读提示e文不好的初学者可以直接看中文,然后拷贝代码试验下。\r
To load a .js or .css file dynamically, a nutshell, it means usg DOM methods to first create a swanky new "script" or "LINK" element, assign it the appropriate attributes, and fally, use element.appendChild() to add the element to the desired location with the document tree. It sounds a lot more fancy than it really is. Lets see how it all es together:
以下为引用的内容
function loadjscssfile(filename, filetype){
if (filetype=="js"){ //判断文件类型
var fileref=document.createElement('script')//创建标签
fileref.setAttribute("type","text/javascript")//定义属性type的值为text/javascript
fileref.setAttribute("src", filename)//文件的地址
}
else if (filetype=="css"){ //判断文件类型
var fileref=document.createElement("lk")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefed")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
loadjscssfile("myscript.js", "js") //打开页面时浏览器动态的加载文件
loadjscssfile("javascript.php", "js") // 打开页面时浏览器动态的加载"javascript.php" ,
loadjscssfile("mystyle.css", "css") //打开页面时浏览器动态的加载.css 文件
接下来的工作是绑定到<head>标签。绑定的时候有一个问题就是同一个文件有可能被我们绑定两次,绑定两次浏览器也不会出现异常,效率就低了。为了避免\r
这种情况我们可以新增一个全局数组变量,把绑定的文件名字保存在里面,每次绑定前先检查一下是否已经存在,如果存在就提示已经存在,如果不存在就绑定。\r
document.getElementsByTagName("head")[0].appendChild(fileref)
By referencg the HEAD element of the page first and then callg appendChild(), this means the newly created element is added to the very end of the HEAD tag. Furthermore, you should be aware that no existg element is harmed the addg of the new element- that is to say, if you call loadjscssfile("myscript.js", "js") twice, you now end up with two new "script" elements both potg to the same Javascript file. This is problematic only from an efficiency standpot, as you'll be addg redundant elements to the page and usg unnecessary browser memory the process. A simple way to prevent the same file from beg added more than once is to keep track of the files added by loadjscssfile(), and only load a file if it's new:
var filesadded="" //保存已经绑定文件名字的数组变量
function checkloadjscssfile(filename, filetype){
if (filesadded.dexOf("[" filename "]")==-1){// dexOf判断数组里是否有某一项
loadjscssfile(filename, filetype)
filesadded ="[" filename "]" //把文件名字添加到filesadded
}
else
alert("file already added!")//如果已经存在就提示
}
checkloadjscssfile("myscript.js", "js") //suess
checkloadjscssfile("myscript.js", "js") //redundant file, so file not added
Here I'm just crudely detectg to see if a file that's set to be added already exists with a list of added files' names stored variable filesadded before decidg whether to proceed or not.
Ok, movg on, sometimes the situation may require that you actually remove or replace an added .js or .css file. Lets see how that's done next.
网站设计
- 静宁会SEO的网站建设公司:全面提升您的网络影
- 提升在线业务的关键:选择最佳的丽水网站建设
- 浙江网站优化发展潜力如何
- 井研专业的网站建设公司:打造您的在线品牌
- 灵山SEO网站建设公司:提升您的在线业务表现
- 蒙城网站建设优化公司:提升您网站表现的理想
- 阳谷企业网站优化:提升线上业务力的关键
- 樟树专业的网站建设公司:打造您在线业务的坚
- 通河百度SEO排名的策略与技巧
- 重庆百度快照排名如何进行精准的客户引流
- 重庆百度快照排名
- 常宁便宜的建站公司:助您轻松打造在线业务
- 巫溪百度网站优化:提升网站曝光率与流量的关
- 湖北整站优化怎么做才能放大客户需求
- 闸北网站建设多少钱?全面解析与预算规划
- 辽宁企业网站优化怎么做电话营销