I am attempting to display a dialog to user while performing an AJAX call using JQuery. I NEED the call to be "async: false" because other parts of the page are dependant on the data and I cannot include all of the code needed in the "success" field.
Using Firefox and Opera, the dialog box displays properly, however in IE and Chrome it waits until the page is completely rendered and does not display the dialog box at all. Here is a sample this is pretty close to the actual implementation, this is a much larger project I have tried to strip it down to the minimums involved.
http://jsfiddle.net/mcraig_brs/eNMna/
I would like to find a way to display the dialog BEFORE starting the ajax call and close the dialog when it is complete. The AJAX call MUST remain "async: false". Is this possible and How can I do this?
Any help would be appreciated. The final answer needs to work in IE8/9, Firefox, and Chrome. Opera and Safari would be bonus :)
In response to one of the responses, I have modified the fiddle to show what I mean:
http://jsfiddle.net/mcraig_brs/BBhdS/
Try running all your code after the document is ready:
$(function(){ var page = new pageManager(); page.LoadData(); });
If the code is executed before the document is completely ready, some of jQuery's selectors (for example, $("#dialog")
) may have no effect because the elements are not ready yet. It depends on how the browser constructs the page vs. how the elements are ordered, etc.
The issue here is that jQuery UI's dialog open function itself runs asynchronously (to handle for animations that may need to occur when opening the dialog). Therefore for this to run correctly you'll need your code to run in a dialogopen
event handler.
Live Example - http://jsfiddle.net/tj_vantoll/YR7RC/
That being said I would strongly discourage the use of synchronous XHR requests. In the long run they'll make your code significantly less maintainable. Using jQuery's deferreds you can queue up multiple functions to run after an AJAX call completes. There's an excellent write up on the technique here - http://rmurphey.com/blog/2010/12/25/deferreds-coming-to-jquery/.