另一種創(chuàng)建方式。似乎更加簡潔明了。用2個function完美的創(chuàng)建和調(diào)用xmlhttp Object。
?1
????//xmlhttp.js
?2
????
?3
????//Function?to?create?an?XMLHttp?Object.
?4
????function?getxmlhttp?()
{
?5
????????//Create?a?boolean?variable?to?check?for?a?valid?microsoft?active?X?instance.
?6
????????var?xmlhttp?=?false;
?7
????????
?8
????????//Check?if?we?are?using?internet?explorer.
?9
????????try?
{
10
????????????//If?the?javascript?version?is?greater?than?5.
11
????????????xmlhttp?=?new?ActiveXObject("Msxml2.XMLHTTP");
12
????????}?catch?(e)?
{
13
????????????//If?not,?then?use?the?older?active?x?object.
14
????????????try?
{
15
????????????????//If?we?are?using?internet?explorer.
16
????????????????xmlhttp?=?new?ActiveXObject("Microsoft.XMLHTTP");
17
????????????}?catch?(E)?
{
18
????????????????//Else?we?must?be?using?a?non-internet?explorer?browser.
19
????????????????xmlhttp?=?false;
20
????????????}
21
????????}
22
????????
23
????????//If?we?are?using?a?non-internet?explorer?browser,?create?a?javascript?instance?of?the?object.
24
????????if?(!xmlhttp?&&?typeof?XMLHttpRequest?!=?'undefined')?
{
25
????????????xmlhttp?=?new?XMLHttpRequest();
26
????????}
27
????????
28
????????return?xmlhttp;
29
????}
30
????
31
????//Function?to?process?an?XMLHttpRequest.
32
????function?processajax?(obj,?serverPage)
{
33
????????//Get?an?XMLHttpRequest?object?for?use.
34
????????var?theimg;
35
????????xmlhttp?=?getxmlhttp?();
36
????????xmlhttp.open("GET",?serverPage);
37
????????xmlhttp.onreadystatechange?=?function()?
{
38
????????????if?(xmlhttp.readyState?==?4?&&?xmlhttp.status?==?200)?
{
39
????????????????document.getElementById(obj).innerHTML?=?xmlhttp.responseText;
40
????????????}
41
????????}
42
????????xmlhttp.send(null);
43
????}

?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
