一个简单的动态加载js和css的jquery代码
作者:bea
一个简单的动态加载js和css的jquery代码,用于在生成页面时通过js函数加载一些共通的js和css文件。 //how to use the function below: //$.include('file/ajaxa.js');$.include('file/ajaxa.css'); //or $.includePath = 'file/';$.include(['ajaxa.js','ajaxa.css']);(only if .js and .css file
一个简单的动态加载js和css的jquery代码,用于在生成页面时通过js函数加载一些共通的js和css文件。
//how to use the function below:
//$.include('file/ajaxa.js');$.include('file/ajaxa.css');
//or $.includePath = 'file/';$.include(['ajaxa.js','ajaxa.css']);(only if .js and .css files are in the same directory)
$.extend({
includePath: '',
include: function(file)
{
var files = typeof file == "string" ? [file] : file;
for (var i = 0; i < files.length; i++)
{
var name = files[i].replace(/^s|s$/g, "");
var att = name.split('.');
var ext = att[att.length - 1].toLowerCase();
var isCSS = ext == "css";
var tag = isCSS ? "link" : "script";
var attr = isCSS ? " type='text/css' rel='stylesheet' " : " type='text/javascript' ";
var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
if ($(tag + "[" + link + "]").length == 0) $("head").prepend("<" + tag + attr + link + "></" + tag + ">");
}
}
});
$.include('../js/jquery-ui-1.8.21.custom.min.js');
$.include('../css/black-tie/jquery-ui-1.8.21.custom.css');
将该函数写入一个common.js文件中,在html中加载该common.js文件,就可以达到目的。 注意: 1.在html5中,<script>标签已经不支持language属性了,所以我删除了:
var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
中的language='javascript' 2.原作者在写入js和css标签时,用的是:
document.write("<" + tag + attr + link + "></" + tag + ">");
但是经过实践,发现document.write()方法会在写入前清除原页面的所有内容,也就相当于覆盖的意思,这样明显达不到我的需要,我需要在加载页面时动态的向页面导入共通的js和css,而不能清除我原页面的其他任何内容,所以查了下api,我改用了:
$("head").prepend("<" + tag + attr + link + "></" + tag + ">");
这个方法,$("head").prepend()方法的作用是在<head>标签的最前端追加写入内容。
最后,再补充一个方法,也是通过共通js来实现,应该比上面这个方法更容易理解:
Dynamically loading external JavaScript and CSS files
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"){ //if filename is a external JavaScript file
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){ //if filename is an external CSS file
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") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file
有用 | 无用
//how to use the function below:
//$.include('file/ajaxa.js');$.include('file/ajaxa.css');
//or $.includePath = 'file/';$.include(['ajaxa.js','ajaxa.css']);(only if .js and .css files are in the same directory)
$.extend({
includePath: '',
include: function(file)
{
var files = typeof file == "string" ? [file] : file;
for (var i = 0; i < files.length; i++)
{
var name = files[i].replace(/^s|s$/g, "");
var att = name.split('.');
var ext = att[att.length - 1].toLowerCase();
var isCSS = ext == "css";
var tag = isCSS ? "link" : "script";
var attr = isCSS ? " type='text/css' rel='stylesheet' " : " type='text/javascript' ";
var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
if ($(tag + "[" + link + "]").length == 0) $("head").prepend("<" + tag + attr + link + "></" + tag + ">");
}
}
});
$.include('../js/jquery-ui-1.8.21.custom.min.js');
$.include('../css/black-tie/jquery-ui-1.8.21.custom.css');
将该函数写入一个common.js文件中,在html中加载该common.js文件,就可以达到目的。 注意: 1.在html5中,<script>标签已经不支持language属性了,所以我删除了:
var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
中的language='javascript' 2.原作者在写入js和css标签时,用的是:
document.write("<" + tag + attr + link + "></" + tag + ">");
但是经过实践,发现document.write()方法会在写入前清除原页面的所有内容,也就相当于覆盖的意思,这样明显达不到我的需要,我需要在加载页面时动态的向页面导入共通的js和css,而不能清除我原页面的其他任何内容,所以查了下api,我改用了:
$("head").prepend("<" + tag + attr + link + "></" + tag + ">");
这个方法,$("head").prepend()方法的作用是在<head>标签的最前端追加写入内容。
最后,再补充一个方法,也是通过共通js来实现,应该比上面这个方法更容易理解:
Dynamically loading external JavaScript and CSS files
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"){ //if filename is a external JavaScript file
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){ //if filename is an external CSS file
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") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- ajaxFileUpload.js插件支持多文件上传的方法
- 引用其它js时如何同时处理多个window.onload事件
- JS中自定义定时器让它在某一时刻执行
- 理解javascript中的回调函数(callback)
- 详解js闭包
- jquery delay()介绍及使用指南
- 使用jquery实现放大镜效果
- javascript初学者常用技巧
- js/jquery判断浏览器的方法小结
- Iframe实现跨浏览器自适应高度解决方法
- jQuery级联操作绑定事件实例
- jquery和css3实现的炫酷时尚的菜单导航
- Enter回车切换输入焦点实现思路与代码兼容各大浏览器
- jQuery淡入淡出元素让其效果更为生动
- JQuery表格拖动调整列宽效果(自己动手写的)
- JQuery中的html()、text()、val()区别示例介绍
- 关闭页面window.location事件未执行的原因及解决方法
- 文本框倒叙输入让输入框的焦点始终在最开始的位置
- JavaScript中的单引号和双引号报错的解决方法