好久没有发布关于技术的文章了,我最近可能会陆续发布一些,我现在先发布一个简单的,供大家参考一下,该内容是Google Ajax之中的loader,用来动态加载Google Maps API,Google Search API等API文件,不过说实在话,在我对代码进行捷思完成之后,我觉得相当的失望,因为这段代码虽然在Google API之中承担着比较重要的内容,可是实际上我认为设计的不太合理,起码我觉得这个文件从代码上来看好像不支持在onload之后执行,这就使loader这个类的作用大打折扣,用户不直接在页面上调用maps api可能最大的原因是希望能够根据用户的需求选择性的调用,也就是说在需要的时候再调用,也就是类似51ditu maps api的这个功能:(
异步加载地图文件),不过我看loader之中并没有实现该内容。
以上的想法都是个人意见,未必正确,希望大家指正,下面我贴出对该JavaScript文件的解释内容,供大家参考:
1
//定义window.google命名空间
2
if (!window['google'])
3
{
4
window['google'] =
{};
5
}
6
//定义window.google.loader命名空间
7
if (!window['google']['loader'])
8
{
9
window['google']['loader'] =
{};
10
google.loader.ServiceBase = 'http://www.google.com/uds';//默认文件加载路径
11
google.loader.ApiKey = 'internal';//用户的key值,该值对所有的API产品都应该是一致的
12
google.loader.KeyVerified = true;//该值在其他的API产品之中通过这个值来判断是否需要验证key的有效性
13
google.loader.LoadFailure = false;//该值没有被调用,应该是提供给用户来调用的接口,判断loader是否失败
14
google.loader.AdditionalParams = '';//请求文件的附加参数
15
google.loader.OriginalAppPath = 'http://code.google.com/apis/ajax/documentation/example.html';//原始HTML文件地址
16
(
17
function()
18
{
19
//判断用户使用的浏览器是否是指定的浏览器
20
//参数a是浏览器名称,例如"msie"
21
function isBroswer(a)
22
{
23
if(a in isBroswerArray)
24
{
25
return isBroswerArray[a]
26
}
27
return isBroswerArray[a]=navigator.userAgent.toLowerCase().indexOf(a)!=-1
28
}
29
//isBroswerArray:本对象记录浏览器判断信息,可以认为是一个缓存
30
var isBroswerArray=
{};
31
function isIE()
32
{
33
return isBroswer("msie")
34
}
35
function isSafari()
36
{
37
return isBroswer("safari")||isBroswer("konqueror")
38
};
39
//继承,让b作为a的基类
40
function inherit(a,b)
41
{
42
var c=function()
{};
43
c.prototype=b.prototype;
44
a.o=b.prototype;
45
a.prototype=new c
46
}
47
//预先定义好的API文件,如Maps API等
48
var preDefinedApis=
{};
49
//类文件加载完成后执行的指定函数句柄
50
var callbacks=
{};
51
//用户的key值,用来发送给服务器验证
52
var userKey=null;
53
//用来记录是否已经向服务器发送了key值如果发送过了,则不再发送
54
var userKeySent=false;
55
//使用该类来定义一个API文件,例如maps api的类文件
56
function ApiFile(fileName)
57
{
58
this.fileName=fileName
59
}
60
//获得API文件的路径URL,参数为版本号和版本选项
61
ApiFile.prototype.getLoadUrl=function(version,config)
62
{
63
var c="";
64
if(config!=undefined)
65
{
66
if(config["locale"]!=undefined)
67
{
68
c+="&hl="+encodeURIComponent(config["locale"])
69
}
70
if(config["nocss"]!=undefined)
71
{
72
c+="&output="+encodeURIComponent("nocss="+config["nocss"])
73
}
74
if(config["callback"]!=undefined)
75
{//注册在API文件加载完成之后执行的函数,只是我不太明白为什么这个字段要发送给服务端,莫非服务器返回的内容会自动添加相应的语句?
76
var d=setApiCallback(config["callback"],this.fileName);
77
c+="&callback="+encodeURIComponent(d)
78
}
79
if(config["other_params"]!=undefined)
80
{
81
c+="&"+config["other_params"]
82
}
83
}
84
if(userKey!=null&&!userKeySent)
85
{
86
c+="&key="+encodeURIComponent(userKey);
87
userKeySent=true
88
}
89
return google.loader.ServiceBase+"/?file="+this.fileName+"&v="+version+google.loader.AdditionalParams+c
90
};
91
ApiFile.prototype.canCallback=function()
{return true};
92
function CustomApiFile(a,b,c,d,e,f,config)
93
{
94
this.fileName=a;
95
this.j=b;
96
this.i=c;
97
this.keyName=d;
98
this.versionName=e;
99
this._canCallback=f;
100
this.fileConfig=config||
{}
101
}
102
inherit(CustomApiFile,ApiFile);
103
//获得API文件的路径URL,参数为版本号和版本选项
104
CustomApiFile.prototype.getLoadUrl=function(version,config)
105
{
106
var c="";
107
if(this.keyName!=undefined)//如果提供了发送用户key的参数名称,则使用该参数名称发送key值
108
{
109
c+="&"+this.keyName+"="+encodeURIComponent(userKey?userKey:google.loader.ApiKey)
110
}
111
if(this.versionName!=undefined)//如果提供了发送版本号的参数名称,则使用该参数名称发送版本号
112
{
113
c+="&"+this.versionName+"="+encodeURIComponent(version)
114
}
115
if(config!=undefined&&this.fileConfig!=undefined)
116
{
117
for(var d in config)
118
{
119
if(this.fileConfig[":"+d]!=undefined)
120
{
121
var e;
122
if(d=="callback")
123
{
124
e=setApiCallback(config[d],this.fileName)
125
}
126
else
127
{
128
e=config[d]
129
}
130
var f=this.fileConfig[":"+d];
131
if(typeof f=="string")
132
{
133
c+="&"+f+"="+encodeURIComponent(e)
134
}
135
else
136
{
137
c+="&"+f(e)
138
}
139
}
140
else if(d=="other_params")
141
{
142
c+="&"+config[d]
143
}
144
}
145
}
146
google[this.fileName]=
{};//设置命名空间
147
if(!this.i&&c!="")
148
{
149
c="?"+c.substring(1)
150
}
151
recordStat("el",this.fileName);
152
return this.j+c
153
};
154
CustomApiFile.prototype.canCallback=function()
155
{
156
return this._canCallback
157
};
158
//提供的loader方法
159