动态加载外部css或js文件

2016-02-19 23:21 3 1 收藏

生活已是百般艰难,为何不努力一点。下面图老师就给大家分享动态加载外部css或js文件,希望可以让热爱学习的朋友们体会到设计的小小的乐趣。

【 tulaoshi.com - Web开发 】

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

原理解析:第一步:使用dom创建script或者link标签,并给他们附加属性,如type等第二步:使用appendChild方法把标签绑定到另一个标签,一般是绑到head.

应用:1、提高代码的复用,减少代码量;2、添加一个javascript控制器和 session可以实现动态改变页面样式;3、由于是页面是从上到下依次加载文件的,并且边加载边解释,所以可以添加javascript控制器控制页面文件的加载顺序,如先加载css布局文件,再显示有图片的css美化文件,之后再加载大的falsh文件,或者安内容的重要性来加载。r

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

阅读提示:e文不好的初学者可以直接看中文,然后拷贝代码试验下。r

To load a .js or .css file dynamically, in a nutshell, it means using DOM methods to first create a swanky new "script" or "LINK" element, assign it the appropriate attributes, and finally, use element.appendChild() to add the element to the desired location within the document tree. It sounds a lot more fancy than it really is. Lets see how it all comes 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("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

loadjscssfile("myscript.js", "js") //打开页面时浏览器动态的加载文件
loadjscssfile("javascript.php", "js") // 打开页面时浏览器动态的加载"javascript.php" ,
loadjscssfile("mystyle.css", "css") //打开页面时浏览器动态的加载.css 文件

接下来的工作是绑定到head标签。绑定的时候有一个问题就是同一个文件有可能被我们绑定两次,绑定两次浏览器也不会出现异常,但是效率就低了。为了避免r

这种情况我们可以新增一个全局数组变量,把绑定的文件名字保存在里面,每次绑定前先检查一下是否已经存在,如果存在就提示已经存在,如果不存在就绑定。r

By referencing the HEAD element of the page first and then calling appendChild(), this means the newly created element is added to the very end of the HEAD tag. Furthermore, you should be aware that no existing element is harmed in the adding 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 pointing to the same Javascript file. This is problematic only from an efficiency standpoint, as you'll be adding redundant elements to the page and using unnecessary browser memory in the process. A simple way to prevent the same file from being 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.indexOf("["+filename+"]")==-1){// indexOf判断数组里是否有某一项
  loadjscssfile(filename, filetype)
  filesadded+="["+filename+"]" //把文件名字添加到filesadded
}
else
  alert("file already added!")//如果已经存在就提示
}
checkloadjscssfile("myscript.js", "js") //success
checkloadjscssfile("myscript.js", "js") //redundant file, so file not added

Here I'm just crudely detecting to see if a file that's set to be added already exists within a list of added files' names stored in variable filesadded before deciding whether to proceed or not.
Ok, moving 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.

来源:https://www.tulaoshi.com/n/20160219/1629726.html

延伸阅读
标签: Web开发
1.直接document.write script language=javascript document.write(script src='test.js'\/script); /script 2.动态改变已有script的src属性 script src='' id=s1/script script language=javascript s1.src=test.js /script 3.动态创建script元素 script var s2 = document.createElement(...
标签: Web开发
我们在使用offline cache的时候,有时候可能会更新资源,如js,css或者图片的 更新。但是在没有更新以前用户已经缓存了旧版本的资源,当在次访问的时候 他使用的还是旧版本的资源,如何才能让用户及时的更新缓存资源呢? 更新缓存资源主要有两种方法,通过修改配置文件的版本号或者调用js完成更新 js更新方法 代码如下: if (window.ap...
标签: Web开发
先给出函数。 01. var addSheet = function (){ 02.   var doc,cssCode; 03.   if (arguments.length == 1){ 04.    doc = document; 05.    cssCode = arguments[0] 06.  } else if (arguments.length == 2){ 07.    doc = argu...
标签: Web开发
服务器端生成%if ..... thenresponse.write "iframe name=frame1 src=xxx.html width=0 height=0"end ifif ..... thenresponse.write "iframe name=frame2 src=xxx.html width=0 height=0"end if%客户端生成div id=div1/divscript language=vbscriptif .... then str="iframe name=frame1 src=xxx.html width=0 height=0"end ifif ... thenstr=...
标签: Web开发
相信你阅读过大量关于编写css时应该注意的一些细节来控制css文件的大小,比如:全局样式、继承样式、缩写样式,空格、注释等;同时你也可能用过对你的css文件进行压缩,来减少css文件的大小,但压缩后的css文件在可维护性、识别性方面变的特别弱!更复杂的情况是,如果你的页面里面引用了多个css、多个js文件你又会怎样处理? 以下给我们介...

经验教程

691

收藏

96
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部