Link to home
Start Free TrialLog in
Avatar of ldbkutty
ldbkuttyFlag for India

asked on

jQuery: "async: false" with "beforeSend"

Hi All,

The following code works good in Firefox, and displays the "Loading" image till there's a response. But the same does not show the "Loading" image in other browsers (IE, Safari).

I found somewhere that having both

async: false

and

beforeSend

doesn't go hand-in-hand. According to http://api.jquery.com/jQuery.ajax/#options : Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

Is there any workaround? Please advise.
var resp = $.ajax({
    url: myUrl,
    async: false,
    beforeSend: function () {
        jQuery('loading').show();
    },
    success: function(data, result) {
        jQuery('loading').hide();
    }
}).responseText;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ldbkutty

ASKER

Zvonko, Simple & powerful, as always :)

Thanks!
Why not


jQuery('loading').show();
setTimeout(function(){
  var resp = $.ajax({
    url: myUrl,
    async: false,
    success: function(data, result) {
        jQuery('loading').hide();
    }
  }).responseText;
},10);

Open in new window

Good question, I dono know. Perhaps to hide it double :)