Poor JavaScript quality
2006-09-15 07:31:43 sobes
First off, thanks very much for your feedback on my article. This is the first time I've written for XML.com, so all responses are much appreciated.
The JavaScript quality wasn't the best I've seen. Not the HTML either, but since that isn't really the topic, I'll resist the urge to comment upon it any further. But the JavaScript doesn't do much error recovery, doesn't declare all variables, doesn't check object existance prior to their usage and does some really ugly and nasty HTML concatenation with innerHTML.
I tried to keep the JavaScript as simple and short as possible, while conveying the main point of the article - how to translate an RSS feed received through AJAX into a series of simple JavaScript objects. Although I didn't declare all the variables, I deliberately listed all object properties (variables) at the top of the object constructors so that custom declarations for them can be added easily by someone who'd wish to extend the code. Object detection is used when necessary (eg: "if (catElement == null)..." in the RSS2Category constructor).
I acknowledge that using DOM functions to create the HTML elements would be a more scalable way of displaying the RSS on the webpage. I used innerHTML for two reasons: (1) cross-browser compatibility and (2) simplicity. As mentioned, showRSS(RSS) is not meant to be the focus of the article. Using DOM functions would mean having a much larger showRSS(RSS) function, and there'd be a few browser workarounds that I would've had to explain.
Plus, everything is just loose functions without any protection from other JavaScripts one might want to use on the same page. Instead, all of the
functions should be kept inside a wrapper function or object to avoid conflicts with other scripts.
This is a good suggestion and an example of how the code could be extended to work within a larger JavaScript application. The code for the project is included as a separate ".js" library file precisely to make this relatively simple to do.
I would advice the author to, instead of trying to invent his own set of wheels in this area, use existing libraries for basic stuff like this. Both Yahoo's Ajax library and Prototype are excellent and hides all this mess from the author and on top of that does all of this much neater and better than most others (including me. And the author of this article).
Before even planning out this article, I had done research on AJAX libraries, as well as other examples of RSS parsers or readers that have been implemented through AJAX. Despite my research, I was unable to find anything that clearly and comprehensively explains the process of implementing an RSS reader with AJAX in a simple way. Although both of the sources you suggest are excellent examples of AJAX libraries, neither of them actually implement a reader for RSS 2.0. As the AJAX that is used in the article is quite simple (it's pretty much encased in the getRSS() function), I chose not to use an AJAX library.
Again, thank you for your feedback, and I hope that you've found my response helpful.
Paul Sobocinski. (author)