GoogleIt Mail IT Print IT PermaLinkSample Code For My Ajax Article In Lotus Advisor
02:47:24 PM
Written By : Richard SchwartzCategory : AJAX With IBM Lotus Domino
Location : Nashua, NH

I've been meaning to publish the source code from my recent article in Lotus Advisor, Develop Domino Web Applications The AJAX Way. I've been meaning to for a long time, but shortly after the article was published several people told me that they had typed in the code and it was throwing JavaScript errors. I didn't want to post the code until I understood and fixed whatever the issue was.


I initially couldn't see the problem. The results of my code's operations were still being displayed properly in my demo app, but then I realized that my browser settings in Firefox were supressing JavaScript erorr messages, and there was indeed a small little warning icon showing up in the status line of IE. Ooops! So even though the results appeared to be correct, something was wrong in the code.


The code that I used in my article was based on an version that I had published in this blog earlier this year. Although I know I had been meticulous in checking the JavaScript console in Firefox early on in the process of developing the code, something must have crept into the code somewhere along the line, and since the results in applications that used the code consistently looked good, I never bothered to check the Console again. And I never noticed that little yellow icon in IE at all.


Mark Barton went so far as to dig into my code, and he made an excellent start at diagnosing the problem. He also came up with a fix, but the way he did it left him feeling a little bit un-satisfied, and the same was true for me. I finally had time to get into it myself last night, and following up on Mark's work, I found out a little bit more about the problem, and came up with a fix that I like a little bit better. Here is the JavaScript source, and here is a downloadable NSF with my sample application.


There were actually two problems in the code, and the interaction between the two -- combined with some browser behavior that I confess I don't completely understand -- allowed the two errors to mask each other so that the code actually did seem to work. One of the errors was an inconsistency in the quoting in constructs like this within anonymous function definitions:


     "window[\"" + reqString + "\"] = null;"

Without proper quoting, this turns into a reference to non-existent property of the window object. That's the first part of the error. The second part of the error is the context in which I was trying to make the assignment of null to this object. I was doing it immediately after the return from the processReadyStateChange() function -- unconditionally. That was just plain dumb, because processReadyStateChange is called repeatedly by the XMLHttpRequest object, and the object should only be cleaned up after the final call.


The net effect of the two bugs seems to be that although I had code that was trying to delete an XMLHttpRequest object that it shouldn't have been trying to delete, it didn't actually delete it because the reference to that object was through an invalid property of the window object, so the runtime errors that did occur were irrelevant. The XMLHttpRequest object did it's job, it invoked the callback function to process the results, and my page was updated despite the errors. Nothing, however, was being cleaned up properly. I'm not sure that it really matters, but Mark was uneasy about the fix he implemented because he knew that it broke the cleanup that I was doing, and now that I knew that my cleanup wasn't really working either I knew that I wanted to fix it.


I have no evidence that the lack of cleanup was causing a memory leak in either IE or Firefox, but this code isn't easy to work with. It just makes sense to figure out how to do the object cleanup now, while I still have a good understanding of the code, rather than waiting a couple of years for a memory leak to show up in some application running on some future browser version.


I fixed the cleanup logic by making sure that processReadyStateChange() always returned an empty string, except when it detects the 200 response from the server that indicates that data has been returned, and by making the code in my anonymous functions conditional on the type and value of the result returned by processReadyStateChange(), like this:


     "if ( ! (typeof(xmlOutput) == \"string\" && xmlOutput == \"\") ) {" 
          + targetObjectString + "= xmlOutput; window[\"" + reqString + "\"] = null;}"

The last change that I made to the code that was published in the article was to cleanup references to document.all, which Vinod Seraphin had actually suggested I do before publication. Although it does work in both Firefox and current versions of IE, it's a deprecated property. A reference like document.all.statusMsg should be converted to the following


     document.getElementById("statusMsg")

Anyhow, that's the code -- all fixed up. I'll follow this up with an article that explains it. I've promised the folks at Advisor that I won't duplicate the contents of the article in blog postings, but I can do some excerpts and elaboration, and that should suffice to explain things that went above and beyond what I described in my posts about the earlier versions of the code.


For more about AJAX, see here

This page has been accessed 1236 times. .
Comments :v

1. Mark10/06/2005 05:11:32 AM
Homepage: http://www.markbarton.com


Thanks for this Richard.

One reason I do not like anonymous functions in Javascript is all of the escaping you have to deal with.

Its bad enough in Notes




2. Richard Schwartz10/06/2005 08:45:09 AM
Homepage: http://www.rhs.com/poweroftheschwartz


And then there's the joy of figuring out how to escape an anonymous functions that call eval() to execute a string that contains escaped strings!




3. Alain H Romedenne10/20/2005 04:32:47 AM


Rich,

In indispensable AJAX Useful links page, the 3rd link is dead when I use it..

Cordially
Alain




4. Richard Schwartz10/20/2005 09:05:11 AM
Homepage: http://www.rhs.com/poweroftheschwartz


@Alain: Thanks. I've fixed the link.




5. Haran11/04/2005 11:33:23 AM
Homepage: http://Error : HTTP Web Server: Item Not Found Exception


Rich,

I'm getting above error when I try to download the sample database "AjaxVoting.zip" from the above link.

Pl. fix it.

Thanks,
haran




Enter Comments^


Email addresses provided are not made available on this site.





You can use UUB Code in your posts.

[b]bold[/b]  [i]italic[/i]  [u]underline[/u]  [s]strikethrough[/s]

URL's will be automatically converted to Links


:-x :cry: :laugh: :-( :cool: :huh: :-) :angry: :-D ;-) :-p :grin: :rolleyes: :-\ :emb: :lips: :-o
bold italic underline Strikethrough





Remember me    

Monthly Archive
Responses Elsewhere



About The Schwartz

rss.jpg


All opinions expressed here are my own, and do not represent positions of my employer.