Generic AJAX Library
Technorati Tags : JavaScript, XML, AJAX
[bad irish accent–for some reason] Ah was gonna keep this to meself and see where it took me, but Ah just get too excited and have to share with ye lads.
[/bad irish accent]
Ever since a discussion at Christian Web Masters on Apr 5 2005, I have been trying to write what I consider a really genericized (is that a word?) version of an AJAX library.
One thing I always hated about AJAX is that, due to the use of the onreadystatechange event handler, things often needed to be hardcoded to the methods involved, making code re-use very difficult when using AJAX in more than one spot within any particular page.
I think I have now done it, though. I have an object whose methods I can call to get XML documents and pass the data for handling. I create external functions for data handling and error handling, and simply pass their names, along with arguments, and the URL to the XML to the AJAX object.
If, later down the same page, I have another document I want and need to do different things with it, I simply call the method with different URL and data-handling function names.
As you can see at my test page, I have a page that calls the AJAX library file, defines 2 functions for handling the returned data–one for error and one for success–and a function that initiates the whole thing when you click the link. The latter of the functions is told what URL to use in getting the file, what method to evok on success, what arguments to pass to the success function, and what function to invoke on failure.
When you click the link, you should see an alert. The first lin of the alert should tell you the sum of the srguments passed to it–to show that the arguments are working–and the rest of the alert shows the responseText of the XMLHttpRequest to show that it’s working as well.
Hope you (web) guys find this helpful, I know I do!
Powered By Qumana
5 Comments
RSS feed for comments on this post. TrackBack URI
Sorry, the comment form is closed at this time.
November 2nd, 2005 @ 12:41 pm
[…] JavaScript Cookie Library Filed under: Technology — by Jim on November 2, 2005 @ 12:39 pm Close on the heels of my Generic AJAX Library I have written a cookie handling library. For those not aware, a cookie is a small bit of data that a developer can leave on your PC to ‘rememember’ things. This technology was enabled because of the stateless nature of HTTP–the protocol that web pages are transmitted in. It allows programmers to leave information, such as your login preferences, on your computer to remember what your preferences were next time you come back to the site. Most all web development languages and environments have methods of accessing and manipulating these bits of data, but JavaScript’s is very limited. you can read from and write to the document.cookie string, but all of the actual data formatting or interpreting is left to you to do manually. This being the case, I have been wanting to write an OOP based version of a library that provides this functionalty to programmers in the JavaScript environment. others have done the same in the past, and I am not claiming mine is much better. But I didn’t find everything I was looking for in the ones I saw out there, and I love writing things myself. My friend Dustin has entioned it before, and I agree with him–sometimes it’s better, even more fun, to re-invent the wheel. So, I’ll leave you to my demonstration page for the Cookie Library, better known as cookieLib. Enjoy! –Jim Powered By Qumana […]
November 10th, 2005 @ 3:29 pm
[…] Just making notice that the Generic AJAX Library I wrote has now been documented more properly. Maybe it will make some more sense to a few folks now. […]
May 9th, 2006 @ 10:09 pm
[…] For the client side of AJAX, I will be using Jim Auldridge’s excellent generic AJAX library. You will need to download it (right-click and Save Link/Target as..) to continue on with the examples. Documentation is also available for the generic AJAX library. HTML document: ajax_weather.html […]
October 24th, 2006 @ 4:22 pm
Hi Jim. Great article on the use of AJAX and XMLHttpRequest! When I tried out your weather program on my (Windows XP) WAMP stack I got an “Error on page.” message, so I opened your “ajax_weatherFINAL.html” up my Web design tool and it says the error is on line 20: zipcode = document.getElementById(’zipcode’).value; which returns the following error: “Object does not support this proprty or method”.
BTW, I used proxy.php and modified the main program since I’m using a WAMP stack. XMLHttpRequest.js is in the WAMP project folder (WWW)with your ajax_weatherFINAL.html code.
Any help would be greatly appreciated.
Dennis
October 24th, 2006 @ 5:15 pm
Hello Dennis,
Thanks for reading the page and leaving a comment.
The script you are referencing, ajax_weather, is not mine though. That script was written by a fellow who wrote an AJAX article using my library as a base for it. That article can be found at FocusedTutorials and it is the author of that article that you should contact in regards to it.
Thanks again,
Jim