一个简单的动态加载js和css的jquery代码_jquery

一个简单的动态加载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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索动态加载
jquery 动态加载css、jquery 加载css、jquery动态修改css、jquery动态添加css、jquery动态设置css,以便于您获取更多的相关知识。

时间: 2024-09-03 07:10:08

一个简单的动态加载js和css的jquery代码_jquery的相关文章

动态加载js、css的实例代码_javascript技巧

一.原生js: /** * 加载js和css文件 * @param jsonData.path 前缀路径 * @param jsonData.url 需要加载的js路径或css路径 * @param jsonData.type 需要加载的类型 js或css */ function loadWriteFiles(jsonData) { jsonData.path = jsonData.path != undefined ? jsonData.path : ""; if(jsonData.

非常简洁的动态加载js和css的jquery plugin

1// plugin author : chenjinfa@gmail.com 2// plugin name : $.include 3// $.include('file/ajaxa.js');$.include('file/ajaxa.css'); 4// or $.includePath = 'file/';$.include(['ajaxa.js','ajaxa.css']); 5 6$.extend({ 7 includePath: '', 8 include: function(f

动态加载js、css的简单实现代码_javascript技巧

一.原生js: /** * 加载js和css文件 * @param jsonData.path 前缀路径 * @param jsonData.url 需要加载的js路径或css路径 * @param jsonData.type 需要加载的类型 js或css */ function loadWriteFiles(jsonData) { jsonData.path = jsonData.path != undefined ? jsonData.path : ""; if(jsonData.

动态加载js、css等文件跨iframe实现

 这篇文章主要介绍了动态加载js.css等文件跨iframe实现的方法,需要的朋友可以参考下 1.动态加载js,css文件(用原生js和jquery)    iframe结构:  frame0(父)  frame2(子)  frame3(子)    frame2中触发事件,动态的向frame3中 加载js.css文件和 dom元素?    *同级之间可以调用,可以 通过 子-父-子 的方式调用同级  parent.parentFram("这个方法在调用其他子farme");    1.

动态加载js、css等文件跨iframe实现_javascript技巧

1.动态加载js,css文件(用原生js和jquery) iframe结构: frame0(父) frame2(子) frame3(子) frame2中触发事件,动态的向frame3中 加载js.css文件和 dom元素? *同级之间可以调用,可以 通过 子-父-子 的方式调用同级 parent.parentFram("这个方法在调用其他子farme"); 1.jquery的append() 复制代码 代码如下: 速度快,同步(需要引入jquery) var oBody = docum

js异步动态加载js与css文件代码

 jquery动态加载css,js文件方法简单很, 例 方法1: 代码如下 $.getscript("test.js"); 方法2: 代码如下 function loadjs(file){  var head = $('head').remove('#loadscript');  $("<scri"+"pt>"+"</scr"+"ipt>").attr({src:file,type:

动态加载js和css(外部文件)_javascript技巧

复制代码 代码如下: // 动态加载外部js文件 var flag = true; if( flag ){ loadScript( "js/index.js" ); }; function loadScript( url ){ var script = document.createElement( "script" ); script.type = "type/javascipt"; script.src = url; document.get

jquery动态加载select下拉框示例代码_jquery

如题,直接上代码,实战学习. 复制代码 代码如下: <head><title>jquery实现动态加载select下拉选项</title> <script type="text/javascript"> function init(){ makemoduleSelect(); } //加载模板下拉框选项 function makemoduleSelect(){ $.ajax({ url : 'indexStatisticsAction_g

如何使用jquery动态加载js,css文件实现代码_jquery

使用jquery动态加载js,css文件 复制代码 代码如下: $.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 =