动态加载外部CSS与JS文件

所属分类: 网页制作 / CSS 阅读数: 1595
收藏 0 赞 0 分享

  动态加载外部CSS与JS文件使用dom创建<script>或者<link>标签,并给他们附加属性,如type等。然后使用appendChild方法把标签绑定到另一个标签,一般是绑到<head>。
  应用:
  1、提高代码的复用,减少代码量;
  2、添加一个javascript控制器和 session可以实现动态改变页面样式;
  3、由于是页面是从上到下依次加载文件的,并且边加载边解释,所以可以添加javascript控制器控制页面文件的加载顺序,如先加载css布局文件,再显示有图片的css美化文件,之后再加载大的falsh文件,或者安内容的重要性来加载。
  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:

  接下来的工作是绑定到<head>标签。绑定的时候有一个问题就是同一个文件有可能被我们绑定两次,绑定两次浏览器也不会出现异常,但是效率就低了。为了避免
  这种情况我们可以新增一个全局数组变量,把绑定的文件名字保存在里面,每次绑定前先检查一下是否已经存在,如果存在就提示已经存在,如果不存在就绑定。
document.getElementsByTagName("head")[0].appendChild(fileref)
  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.
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 文件
更多精彩内容其他人还在看

css实现div内图片上下左右居中

DIV容器内设置 text-align:center 即可让图片居中,但是DIV内默认的图片是上对齐,不会上下居中,如果想要实现这样的效果,JS判断是比较麻烦的,因为DIV容器内的图片高度是不一定的,那么只有用table了,我想大多数人事不愿意这么做的,那么怎么办呢,还是用CSS
收藏 0 赞 0 分享

css调用服务器端字体示例代码

本文主要介绍了使用@font-face调用服务器端字体的代码,大家参考使用吧
收藏 0 赞 0 分享

css控制div鼠标放上去变色

本文主要介绍了css控制div鼠标放上去变色代码实例,大家参考使用吧
收藏 0 赞 0 分享

CSS实现连续数字和英文的自动换行的方法

在网页布局过程中档遇到连续数字和英文时,它们是不自动换行的,使用CSS即可实现自动换行,下面有个不错的教程,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

CSS中针对IE6、7和FF等浏览器的特殊样式写法

针对IE6、7和FF的css样式兼容问题,一直都是前端开发者的头疼问题,下面为大家介绍下CSS中针对IE6、7和FF的特殊写法,希望对大家有所帮助
收藏 0 赞 0 分享

关于IE7 z-index的浏览器兼容性问题完美解决方案

在了IE7的z-index问题上真是很头疼。可又不能因为浏览器版本低而不去解决,毕竟要从用户的角度着想,下面为大家分享下完美的解决方法,有类似情况的朋友可以参考下
收藏 0 赞 0 分享

用纯CSS实现容器内图片上下左右居中

DIV容器内设置 text-align:center 即可让图片居中,但是DIV内默认的图片是上对齐,不会上下居中,下面为大家介绍下只用CSS实现容器内图片上下左右居中,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

CSS行高line-height的个人理解

“行高“顾名思义指一行文子的高度,大家对CSS行高line-height都有自己的理解,下面为大家介绍下本人自己的理解
收藏 0 赞 0 分享

让网页图片变灰色将彩色图像变成灰度的三种方法

灰度图像看起来更有艺术感,Photoshop很容易把你的彩色图像变成灰度,下面为大家介绍下让网页图片变灰色的三种方法,希望对大家有所帮助
收藏 0 赞 0 分享

借助CSS定位来实现把一个DIV放到另一个div右下角

借助CSS定位来实现,将右下角的那个DIV放在另一个DIV里面,具体的实现如下,希望对大家有所帮助
收藏 0 赞 0 分享
查看更多