Just had a small task to do last weekend. The useful part is how to use XMLHttpRequest which is the essential part of AJAX. I have uploaded some code snippets to CodeKeep. Here are something I have leant just for future references:

 

1.       The brief overview on XMLHttpRequest can be found here. It also gives some solutions on how to solve cache issues, for example, by adding a random string on the end of the url in the query.

2.       The replace string function in Javascript takes a regular expression as the first parameter (not a string).

3.       It is possible for XMLHttpRequest’s onreadystatechange method to take parameters. Please check out this.

4.       Hiding/displaying the controls can use:

a)       document.getElementById(“”).style.display=“none”

document.getElementById(“”).style.display=“inline”

or

b)      document.getElementById(“”).style.visibility=“visible”

document.getElementById(“”).style.visibility=“hidden”

The difference between the two is by using the 1st option, it hides the controls and the space occupied by the controls also gets removed, i.e. if a control has a certain height and is contained in a table row, the row gets collapsed and when displayed, the control will again take it’s place and the row will again retain its height by displaying the control in it.

On the other hand if 2nd option is used, the control will get hide but the space occupied by the control will remain as it is and the row will not collapse, i.e. the row will still be displayed as blank containing the control in hidden mode.

5.       How to debug javascript is talked at here.

6.       If want to intercept the key before anything happens, use onkeydown, and if you want to do something after the key has done whatever to the field, use onkeyup. So if want to capture keys like “backspace” or “delete”, you have to use onkeyup event. If you want to capture those keys in keypress event, I guess you need to write your own keypress handler (testing if (e.keyCode == 8 ) for backspace and if (e.keyCode == 46 ) for delete, etc.). And when calling it, you have to pass ‘event’ parameter, like this: onkeypress=’x(event)’, otherwise it won’t work in Firefox while it doesn’t affect IE.

7.       w3school and w3school Chinese are excellent websites for learning all the web-related stuff.

8.       When using XMLHttpRequest to post data to server, the parameter in send(data) method should be a XMLDOM object or xml text. And on the server side, load() method should be used instead of loadXML() which will give you a mistype error.

9.       I found IIS 5 on Windows XP doesn’t support writing XML file using XMLHttpRequest send method (you may get a error message telling that uploading size exceeds the maximum allowed, which is misleading), but IIS 6 on Windows Server 2003 has no problems at all if you set permission correctly.