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