]>
Commit | Line | Data |
---|---|---|
a5eae6b7 MR |
1 | var url = "http://localhost/~mir/test.php?name="; |
2 | var output = new Array(); | |
3 | var httpRequest; | |
4 | var fields = new Object(); | |
5 | ||
6 | /** | |
7 | * Get the XMLHttpRequest Object | |
8 | * \return XMLHttpRequst, or <i>false</i> if fail | |
9 | */ | |
10 | function getXMLHttpRequest() { | |
11 | if (window.XMLHttpRequest) { // Mozilla, Safari, ... | |
12 | httpRequest = new XMLHttpRequest(); | |
13 | if (httpRequest.overrideMimeType) { | |
14 | httpRequest.overrideMimeType('text/xml'); | |
15 | } | |
16 | } | |
17 | else if (window.ActiveXObject) { // IE | |
18 | try { | |
19 | httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); | |
20 | } | |
21 | catch (e) { | |
22 | try { | |
23 | httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); | |
24 | } | |
25 | catch (e) {} | |
26 | } | |
27 | } | |
28 | if (!httpRequest) { | |
29 | alert('Giving up :( Cannot create an XMLHTTP instance'); | |
30 | return false; | |
31 | } | |
32 | return true; | |
33 | } | |
34 | ||
35 | function handleHttpResponse(response, xml) { | |
36 | if (xml == true) { | |
37 | var text = response.getElementsByTagName('param')[0].firstChild.data | |
38 | text = text + " : " + response.getElementsByTagName('greeting')[0].firstChild.data | |
39 | document.getElementById("textarea").value = text | |
40 | } | |
41 | else { | |
42 | document.getElementById("textarea").value = response.value | |
43 | } | |
44 | } | |
45 | ||
46 | function setField(field, value) { | |
47 | fields[field] = value | |
48 | } | |
49 | ||
50 | function getContactByName(name) { | |
51 | if (httpRequest == null) { | |
52 | alert("Could not initiate AJAX Session") | |
53 | return null | |
54 | } | |
55 | httpRequest.onreadystatechange = function() { | |
56 | if (httpRequest.readyState == 4) { | |
57 | if (httpRequest.status == 200) | |
58 | handleHttpResponse(httpRequest.responseXML, true) | |
59 | } | |
60 | } | |
61 | httpRequest.open("GET", url + name, true) | |
62 | httpRequest.send(null) | |
63 | } | |
64 | ||
65 | function handleEvent(field) { | |
66 | try { | |
67 | var f = fields[field] | |
68 | if (f == "search_name") { | |
69 | var name = document.getElementById(f).value | |
70 | getContactByName(name) | |
71 | } | |
72 | } | |
73 | catch (e) { | |
74 | return false | |
75 | } | |
76 | } | |
77 | ||
78 | if (!getXMLHttpRequest()) { | |
79 | document.location = 'error.html'; | |
80 | } | |
81 | window.onload = setField("name", "search_name"); | |
82 | ||
83 |