文章分类 | 推荐文章 | 最新文章 | 热点文章 | 最新软件 | 精品软件 | 下载排行 | 推荐下载 | 免费看大片 | WPS | 杀毒软件
清风网络
首 页 软件下载 网络学院 数码学院
QQ 电脑入门 游戏 操作系统 图形处理 办公软件 媒体动画 精文荟萃 工具软件 网络编程 程序开发 网络技术 认证考试 网站建设 文章专栏
当前位置:清风网络学院网站建设Div+Css动态加载外部css或js文件
精品推荐
特别推荐
·初学者来看:学习网页技术CSS怎么入门?
·CSS网页设计非常有用技巧
·DIV+CSS的命名规则有利于网站优化
·多个CSS样式表争夺特定选择符的控制权
·学习CSS制作网页总结的一些经验技巧
·CSS初学者应该保持的一种心态
·CSS常见问题和技巧总结
·DIV+CSS常见错误汇总
·设计中常用的十个CSS技巧
·网站配色,CSS主色调配色方案
热点TOP10
·背景图片居中显示(HTML,CSS技巧)
·CSS实例:让页脚保持在未满屏页面的底部
·CSS大师Eric采访实录
·CSS隐藏网页文字的几种常用方法
·国外译文:网页设计如何使用CSS的选择器模式?
·CSS注释、命名、继承性、样式排序等CSS技巧的小结
·CSS条状图表形式的实现方法
·CSS实现带背景图片的文字链接的方法
·CSS和XHTML经验:类目之间的竖线问题引发的讨论
·LI标签中内容超过长度后以省略号显示的方法

动态加载外部css或js文件

日期:2007年12月20日 作者: 查看:[大字体 中字体 小字体]


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

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

阅读提示: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

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.

(出处:清风网络学院






上一篇:CSS初学者应该保持的一种心态

下一篇:Windows XP下PHP+MySQL环境搭建

相关文章:
·dll文件下载
·局域网文件共享详解
·利用word将PDF转换DOC文件的方法
·java做的比较完善的FTP连接上传下载文件
·MPEG、RM、WMV电影文件格式转换指南
·从视频文件中提取声音的方法
·Coreldraw图像文件导入PS三种方法
·邮箱能否传输影音文件
·如何打开下载文件 文件后缀名大全
·QQ表情下载及QQ自定义动态表情下载
相关软件:
·最强文件加密锁2007
·Google Earth 动态卫星地图 汉化版 带我们进入间谍时代 能看见你家院子
·Ultra RM Converter(Real文件转换分割,合并)V3.3.0916 汉化版
·Google Earth 动态卫星地图 汉化版 能看见你家院子的车 电信做种
·西门子plc软件及教程 文件类型 其它介绍
·文件夹加密器V9.6
·动态网页制作视频教程_方成32讲 asf
·文件夹隐藏大师V3.2
·csf文件播放器 V5.0.0 1118
·2005监理执业考试文件汇编

特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
[打印本页] [关闭窗口] 转载请注明来源:http://www.viphot.com
| 帮助(?) | 版权声明 | 友情连接 | 关于我们 | 信息发布
Copyright 2007 www.viphot.com All Rights Reserved. 鄂ICP备05000083号Powered by:viphot