文章分类 | 推荐文章 | 最新文章 | 热点文章 | 最新软件 | 国产软件 | 国外软件 | 汉化补丁 | 设为首页 | 加入收藏
网站首页 软件下载
数码学院 网络学院
业界资讯 | 图形图像 | 操作系统 | 网络冲浪 | 工具软件 | 办公软件 | 媒体动画 | 精文荟萃 | 认证考试 | 网站建设 | 技术开发 | 专栏
当前位置:abcdown网络学院网站建设Html/Css动态加载外部css或js文件
精品广告
推荐TOP10
·DIV与Table布局在大型网站的可用性比较
·CSS样式表设计的十条技巧
·你知道这些css网页设计技巧吗
·用CSS层叠样式表完成可以控制的闪烁效果
·正则表达式过滤HTML危险脚本
·调整CSS类型的顺序改变链接状态
·学习用Xhtml+CSS2构建网页
·网站重构:利用CSS改善网站可访问性
·解密.htm.html.shtm.shtml的区别与联系
·CSS的十八般技巧
·超强,用CSS构建iframe效果
·锦囊妙计 CSS实现样式布局22招
·网页风格化 用CSS实现皮肤适时切换
·搞定CSS SPAN和DIV的区别
·与HTML相比XHTML有什么特点?
·根据分辨率不同调用不同的css文件
·怎样在Web开发中完美控制IE标题栏
·危险无处不在 Html标签带来的安全隐患
·HTML小技巧的一些小技巧
·网页制作中的小秘诀
阅读TOP10
·导航上用CSS标志当前页效果的实现
·动态加载外部css或js文件
·CSS初学者应该保持的一种心态
·为便于简化和维护CSS,该如何组织CSS?
·DIV CSS网页布局需要掌握的技巧汇总
·针对各种版本的浏览器隐藏CSS的九大技巧
·css的一些基础的东西
·利用css和js实现firefox和IE都支持的页面局部打印
·内联CSS的background-image属性图片相对路径设置失败
·用CSS设计高体验的表单显示效果示例
·Html 书签的使用
·HTML:关闭(离开)浏览器窗口前的 系统提示
·xml文件调用css
·HTML:网页刷新方法集合
·滚动字幕的制作 marquee
·完全CSS写的鼠标悬停tip效果
·frameset 使用心得
·XHTML+CSS兼容性解决方案小集
·对css滑动门技术的一些总结和归纳
·DIV与Table布局在大型网站的可用性比较

动态加载外部css或js文件

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


原理解析:第一步:使用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.

(出处:ABC软件下载学院



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

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


动态加载外部css或js文件 相关软件: 动态加载外部css或js文件 相关文章:
·文件夹保密专家 V5.03 Build 2003.07.19
·万象 2003网吧管理软件 15.324破解版
·赢证股市分析软件 V3.0破解版
·2002年国产软件776个注册机(码)
·闪电ASP V1.53 +破解文件
·金科邮件快车iQMailS
·QQ2000 0825显IP版本数据文件升级 数据版本:03/01/05
·磁卡消费管理软件 V1.5 Build 2003.07.27
·百万富翁彩票统计分析预测软件(超级自助版) Build 20050619
·金钥匙文件保密系统 赠送版
·动画文件制作利器
·PhotoStudio--五脏俱全的图像编辑软件
·操作简单的三维图像制作软件-COOL 3D
·解决在DW中不支持中文文件名的方法
·无组件上传图片到数据库中,最完整解决方案
·用Flash MX组件打造全新留言板
·试用简谱字库软件 轻松输入音乐简谱
·拒绝垃圾邮件:三种“武器”断黑手
·用FlashGet检测并修复受损的ZIP文件
·谁共享了文件?超级网上邻居简介

特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
[打印本页] [关闭窗口] 转载请注明来源:http://www.abcdown.net
首页 | 本站声明 | 下载帮助 | 发布软件 |
中文版权所有:ABC学院 浙ICP备05000717号