异步加载js两例
<!DOCTYPE HTML>
<html lang="">
<head>
<meta charset="gbk">
<title></title>
</head>
<body>
http://stackoverflow.com/questions/610995/jquery-cant-append-script-element
<script type="text/javascript">
;(function(w){
var loader = function(){
var dc = document;
function createScript(url, callback){
var urls = url, scripts = [], completed = 0;
for( var i = 0, len = urls.length; i < len; i++ ){
scripts[i] = dc.createElement('script');
scripts[i].src = urls[i];
dc.getElementsByTagName('head')[0].appendChild(scripts[i]);
if( scripts[i].readyState ){
//ie
scripts[i].onreadystatechange = function(){
if( this.readyState == 'complete' || this.readyState == 'loaded' ){
this.onreadystatechange = null; //确保事件不被处理2次
completed++; completed >= urls.length ? callback() : '';
}
}
}else{
//not ie
scripts[i].onload = function(){
completed++; completed >= urls.length ? callback() : '';
}
}
}
}
function createLink(url, callback){
var urls = url, links = [];
for( var i = 0, len = urls.length; i < len; i++ ){
links[i] = dc.createElement('link');
links[i].rel = 'stylesheet';
links[i].href = urls[i];
dc.getElementsByTagName('head')[0].appendChild(links[i]);
}
callback();
}
return { load: function(option, callback){
var _type = option.type || 'js', _url = option.url, _callback = callback || function(){};
switch( _type ){
case 'js':
case 'javascript':
createScript(_url, _callback);
break;
case 'css':
createLink(_url, _callback);
break;
}
return this;
}
}
}();
w.Cme ? '' : w.Cme = {};
w.Cme.loader = loader; })(window);
Cme.loader.load({ url: [ 'http://hq.sinajs.cn/list=sz000009' ] }, function(){ alert( hq_str_sz000009 ); })
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>loadJS</title>
<script type="text/javascript">
var loadJS = function (params) {
var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.onload = script.onreadystatechange = script.onerror = function () {
if (script && script.readyState && /^(?!(?:loaded|complete)$)/.test(script.readyState)) return;
script.onload = script.onreadystatechange = script.onerror = null;
script.src = '';
script.parentNode.removeChild(script);
script = null;
params.callback();
};
script.charset = params.charset || document.charset || document.characterSet || 'gb2312';
script.src = params.src;
try {
head.appendChild(script);
} catch (exp) {}
};
var callback = function () {
var ms = ('undefined' != typeof CarInfo ? '<br />' + CarInfo[0].name : '');
document.body.innerHTML += ms;
};
window.onload = function () {
loadJS({ src : 'http://price.bitauto.com/InputDealHandle.ashx?keyWord=1&temp=' + new Date().getTime(), charset : 'utf-8', callback : callback });
loadJS({ src : 'http://price.bitauto.com/InputDealHandle.ashx?keyWord=2&temp=' + new Date().getTime(), charset : 'utf-8', callback : callback });
loadJS({ src : 'http://price.bitauto.com/InputDealHandle.ashx?keyWord=3&temp=' + new Date().getTime(), charset : 'utf-8', callback : callback });
};
</script>
</head>
<body>
loadJS
</body>
</html>
<html lang="">
<head>
<meta charset="gbk">
<title></title>
</head>
<body>
http://stackoverflow.com/questions/610995/jquery-cant-append-script-element
<script type="text/javascript">
;(function(w){
var loader = function(){
var dc = document;
function createScript(url, callback){
var urls = url, scripts = [], completed = 0;
for( var i = 0, len = urls.length; i < len; i++ ){
scripts[i] = dc.createElement('script');
scripts[i].src = urls[i];
dc.getElementsByTagName('head')[0].appendChild(scripts[i]);
if( scripts[i].readyState ){
//ie
scripts[i].onreadystatechange = function(){
if( this.readyState == 'complete' || this.readyState == 'loaded' ){
this.onreadystatechange = null; //确保事件不被处理2次
completed++; completed >= urls.length ? callback() : '';
}
}
}else{
//not ie
scripts[i].onload = function(){
completed++; completed >= urls.length ? callback() : '';
}
}
}
}
function createLink(url, callback){
var urls = url, links = [];
for( var i = 0, len = urls.length; i < len; i++ ){
links[i] = dc.createElement('link');
links[i].rel = 'stylesheet';
links[i].href = urls[i];
dc.getElementsByTagName('head')[0].appendChild(links[i]);
}
callback();
}
return { load: function(option, callback){
var _type = option.type || 'js', _url = option.url, _callback = callback || function(){};
switch( _type ){
case 'js':
case 'javascript':
createScript(_url, _callback);
break;
case 'css':
createLink(_url, _callback);
break;
}
return this;
}
}
}();
w.Cme ? '' : w.Cme = {};
w.Cme.loader = loader; })(window);
Cme.loader.load({ url: [ 'http://hq.sinajs.cn/list=sz000009' ] }, function(){ alert( hq_str_sz000009 ); })
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>loadJS</title>
<script type="text/javascript">
var loadJS = function (params) {
var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.onload = script.onreadystatechange = script.onerror = function () {
if (script && script.readyState && /^(?!(?:loaded|complete)$)/.test(script.readyState)) return;
script.onload = script.onreadystatechange = script.onerror = null;
script.src = '';
script.parentNode.removeChild(script);
script = null;
params.callback();
};
script.charset = params.charset || document.charset || document.characterSet || 'gb2312';
script.src = params.src;
try {
head.appendChild(script);
} catch (exp) {}
};
var callback = function () {
var ms = ('undefined' != typeof CarInfo ? '<br />' + CarInfo[0].name : '');
document.body.innerHTML += ms;
};
window.onload = function () {
loadJS({ src : 'http://price.bitauto.com/InputDealHandle.ashx?keyWord=1&temp=' + new Date().getTime(), charset : 'utf-8', callback : callback });
loadJS({ src : 'http://price.bitauto.com/InputDealHandle.ashx?keyWord=2&temp=' + new Date().getTime(), charset : 'utf-8', callback : callback });
loadJS({ src : 'http://price.bitauto.com/InputDealHandle.ashx?keyWord=3&temp=' + new Date().getTime(), charset : 'utf-8', callback : callback });
};
</script>
</head>
<body>
loadJS
</body>
</html>