Wednesday, March 28, 2012

How do I know the AJAX load is complete?

I have several webservice proxies set up and some event wiring that is occurring in the Application.Load client side Ajax event. Is there a property somewhere that I can query to determine if the loading of all Ajax controls and initialization is setup and complete.

I have a client side event that displays a modal popup using a WebService proxy. But when the users click on the button before the Ajax initialization is complete there is a script error. Right now I am checking typeof( WebService.Proxy) to "undefined" to know whether I should execute the method, but a variable I could query would be much better.

Would it be preferable to disable the button by default -- then in app load, enable it?


That is a good suggestion. I could use that method in some situations. However, some of my events are mouseover events and link click events. There will be several on the page and it would be beneficial to have a single interface to consistently determine if I could execute my event script. From the user experience side, most users won't click a button until Ajax has finished loading anyway and I'd hate to distract them with UI elements flickering when they are activated.

In some cases, I would like set a timeout that could poll the app load complete bool and continue firing the event.


I just noticed in Beta 2 the external script references are loaded asychronously. This presses my need for a "done loading javascript file" message, or do you suggest putting this "check" in each javascript object file?


The way the asychronous loading feature is designed, you can be assured that all controls and instantiated and initialized by the time you get the Sys.Application object's "load" event. It is during the processing of the "init" event that all the asynchronous loading work is completed. To ensure that a piece of your code runs AFTER this, you can do the following:

Sys.Application.add_load(function() { anotherFunctionCall(); });

The code above ensures that you can run something with the confidence that all the controls, scripts, etc. have been loaded and initialized.


Preferably you would hookup your event handlers from pageLoad (aka app load). That way, if the user does something like mouse over before the page is ready, nothing happens. No need to check for something being loaded or not.


Thanks, that makes sense. I'm trying to use the extender style javascript object to wire up the event handlers and I think it is going to work out well.

No comments:

Post a Comment