var win=window,doc=document;
var calss2Type={
'[object Array]':'array',
'[object Boolean]':'boolean',
'[object Date]':'date',
'[object Function]':'function',
'[object Number]':'number',
'[object Object]':'object',
'[object RegExp]':'regexp',
'[object String]':'string'
}
function getType(args){
return args&&calss2Type[Object.prototype.toString.call(args)]
}
function loadJs(arr,async){
/*
*params arr [] [{url:'***.js',fn:'mess.init',params:{type:'in'}},{url:'***.js',fn:'mess.init',params:{type:'in'}}]
*params arr {} {url:'***.js',fn:'mess.init',params:{type:'in'}}
*params arr string '***.js'
*params arr fun arguments.callee.call(this,fun())
*fn window['mess']['init']()
*/
var async=async,arr=arr;
if(getType(arr)==='string'){
this.createJs(arr);
return this;
}
if(getType(arr)==='object'){
var obj=arr;
this.createJs(obj.url||'',obj.fn||'',obj.params||'');
return this;
}
if(getType(arr)==='function'){
arguments.callee.call(this,arr());
}
if(getType(arr)==='array'&&arr.length){
for (var i = 0 ; i < arr.length; i++) {
var obj=getType(arr[i])==='object'&&arr[i];
if(obj){
if(!obj.url){
continue
}
this.createJs(obj.url,obj.fn,obj.params);
}
};
}
return this;
}
function createJs(url,fn,params){
var _this=this,
elm=doc.createElement('script');
elm.type='text/javascript';
elm.async=false;
if('onload' in elm){
elm.οnlοad=function(){
if(fn) {
var method=getType(params)==='array'?'apply':'call';
if(getType(fn)==='string'){
fn= _this.getFn(fn);
}
fn[method](fn,params);
}
elm.οnlοad=null;
}
}else{
elm.onreadystatechange=function() {
if (this.readyState==='loaded' || this.readyState==='complete') {
if(fn) {
var method=getType(params)==='array'?'apply':'call';
if(getType(fn)==='string'){
fn= _this.getFn(fn);
}
fn[method](fn,params);
}
elm.onreadystatechange=null;
}
}
}
elm.src=url;
doc.getElementsByTagName('body')[0].appendChild(elm);
},
function getFn(str){
var arr=str.split('.'),fns=win;
for(var i=0; i
fns=fns[arr[i]];
}
return fns;
}
//loadJs({url:'/index/js/index.js',fn:'obj.say',params:['hello','me']});
(function(win,doc){
alert(1);
var obj={
say:function(mess,who){
alert(mess+' '+who);
}
}
//全局调用必须往外开方法
win.obj=obj;
})(window,document);