Thursday, February 7, 2008

Slow Javascript

I have a J2EE AJAX web application with 1000's of DOM elements. As a result, simply running a document.getElementById() in javascript can take IE7 over a minute to return back the object, also since their is so much dojo javascript loaded, even calling a method for an onClick, takes seconds for the browser to find the method and start executing the code. What I have been spending days on is trying to figure out a way to give the user some sort of clue that their click was received and is processing, some kind of instantaneous feedback. The AJAX application has loading objects that are shown for ansynchronous (slow) operations but even the loading window can take several seconds to finally get displayed. After trying several approaches I found something that I think works really well and would like to share.
In the onClick of a link that needs to do some heavy processing the webapplication would call a method like this:

onClick="setCurrentTaskOutcome('name','favwebcomic')"


Problem is, it takes the browser too long to even find the 'setCurrentTaskOutcome' method, the browser appears hung while the click event is slowly processed by IE. So putting more code in the setCurrentTaskOutcome function to alert the user would not help with the user feedback issue because they would still have to wait until the function was located and executed by the browser before any feedback would get presented.
My solution is:

onClick="confirm('Set User Params ?') != 0 ? setCurrentTaskOutcome('name','favwebcomic') : void(0);"


Now the browser will instantaneously prompt the user to confirm the operation, if they cancel the operation is not performed, if they confirm, then they at least now know the browser is processing their request instead of it just appearing hung.

To break it down, the confirm javascript function will return non-zero if they click ok, so the

confirm('Set User Params ?') != 0

Is a confirm window dialog and test to see if they click ok wrapped into one.
I use the tertiary operator '?' to perform the task if they clicked ok, or nothing if they clicked cancel.

This works in both IE and Firefox. However, IE7 was the only browser that seems to suffer from performance and responsiveness issues for this web application. IE7 is the required browser for the employees of the company that will use this, so I can't just tell them to use Firefox. What needs to start happening is if you work for a company that requires you to use IE, or maybe even makes using Firefox, Safari or Opera difficult for you (because they have IE7 specific web apps), find a new job. Life is too short to live with IE7.

No comments:

About Me

My photo
Lead Java Developer Husband and Father

Tags