-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bridge.html
47 lines (45 loc) · 1.26 KB
/
Bridge.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<html><head><title>Bridge-桥接模式</title><meta charset="utf-8"></head>
<body>
<script type="text/javascript">
var MyNamespace = window.MyNamespace || {};
// 定义在request模块
MyNamespace.request = (function(){
function getXHR(){
methods = [
function(){ return new XMLHttpRequest(); },
function(){ return new ActiveXObject('Microsoft.XMLHTTP'); },
function(){ return new ActiveXObject('Msxml2.XMLHTTP'); }
]
for(var i = 0; i < methods.length; i++){
try{
methods[i]();
}catch(e){
continue;
}
this.getXHR = methods[i]();
return methods[i]();
}
}
function handleReadystate(xhr, callback){
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
if(callback){
callback(xhr.responseText);
}
}
}
}
return function(method, uri, postData, callback){
//此处运用了桥接模式
var xhr = getXHR();
xhr.open(method, uri, true);
handleReadystate(xhr, callback);
xhr.send(postData || null);
}
})();
MyNamespace.request('post', 'test.php', "name=test&pwd=test", function(data){
alert(data);
})
</script>
</body>
</html>