Mercurial > hg > chronicle
comparison themes/blog.mail-scanning.com/ajax.js @ 269:53c79b52862a
Added
author | Steve Kemp <steve@steve.org.uk> |
---|---|
date | Mon, 04 Aug 2008 20:04:15 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
268:dbee53530497 | 269:53c79b52862a |
---|---|
1 // -*-mode: C++; style: K&R; c-basic-offset: 4 ; -*- */ | |
2 // | |
3 // Simple collection of Javascript for Ajax form submission. | |
4 // | |
5 | |
6 | |
7 | |
8 | |
9 // | |
10 // Get an XMLHTTPRequest object. | |
11 // | |
12 function getXMLHTTPRequest() | |
13 { | |
14 req = false; | |
15 if(window.XMLHttpRequest) | |
16 { | |
17 try | |
18 { | |
19 req = new XMLHttpRequest(); | |
20 } | |
21 catch(e) | |
22 { | |
23 req = false; | |
24 } | |
25 } | |
26 else if(window.ActiveXObject) | |
27 { | |
28 try | |
29 { | |
30 req = new ActiveXObject("Msxml2.XMLHTTP"); | |
31 } | |
32 catch(e) | |
33 { | |
34 try | |
35 { | |
36 req = new ActiveXObject("Microsoft.XMLHTTP"); | |
37 } | |
38 catch(e) | |
39 { | |
40 req = false; | |
41 } | |
42 } | |
43 } | |
44 | |
45 return( req ); | |
46 } | |
47 | |
48 | |
49 // | |
50 // Submit the comment. | |
51 // | |
52 function submitComment() | |
53 { | |
54 showProgress(); | |
55 | |
56 var xhr = getXMLHTTPRequest(); | |
57 if(! xhr ) | |
58 { | |
59 hideProgress(); | |
60 return; | |
61 } | |
62 | |
63 xhr.onreadystatechange = function() | |
64 { | |
65 if(xhr.readyState == 4) | |
66 { | |
67 if(xhr.status == 200) | |
68 { | |
69 var o = document.getElementById( "output" ); | |
70 o.innerHTML = xhr.responseText; | |
71 } | |
72 else | |
73 { | |
74 var o = document.getElementById( "output" ); | |
75 o.innerHTML = "Failed HTTP code " + xhr.status + " " + xhr.responseText; | |
76 } | |
77 | |
78 hideProgress(); | |
79 } | |
80 }; | |
81 | |
82 data = 'ajax=1'; | |
83 data = data + '&id=' + escape(document.forms[0].id.value ); | |
84 data = data + '&captcha=' + escape( document.forms[0].captcha.value ); | |
85 data = data + '&id=' + escape(document.forms[0].id.value ); | |
86 data = data + '&captcha=' + escape( document.forms[0].captcha.value ); | |
87 data = data + '&name=' + escape( document.forms[0].name.value ); | |
88 data = data + '&mail=' + escape( document.forms[0].mail.value ); | |
89 data = data + '&body=' + escape( document.forms[0].body.value ); | |
90 | |
91 // | |
92 // Make the request | |
93 // | |
94 xhr.open("POST", "/cgi-bin/comments.cgi", true); | |
95 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | |
96 xhr.send(data); | |
97 } | |
98 | |
99 | |
100 // | |
101 // Show our progress marker. | |
102 // | |
103 function showProgress() | |
104 { | |
105 var i = document.getElementById( "progress" ); | |
106 if ( i ) | |
107 { | |
108 i.style.display = 'block'; | |
109 } | |
110 } | |
111 | |
112 // | |
113 // Hide our progress marker. | |
114 // | |
115 function hideProgress() | |
116 { | |
117 var i = document.getElementById( "progress" ); | |
118 if ( i ) | |
119 { | |
120 i.style.display = 'none'; | |
121 } | |
122 } |