Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I noticed (on Safari 6.0.4 on MacOSX) the alert box pops up and the ajax request is only sent after I click "OK". (On Chrome the request is sent while the alert is displayed).</p> <p>So the code following my ajax request actually delays the request being sent.</p> </blockquote> <p>I find that very surprising, but I can't test it as I don't have Mac OS X. Although <code>alert</code> blocks the main JavaScript thread, it shouldn't prevent the browser from <strong>sending</strong> the request, as the browser is multi-threaded. But I suppose it could, in some implementations.</p> <p>Note that regardless, <code>alert</code> will hold up the call to any "success" or "error" handlers you have associated with the request, which is a completely different thing. This is because unless you use web workers, JavaScript is single-threaded on browsers, and (again) <code>alert</code> holds up the one JavaScript thread.</p> <p>If the <em>sending</em> is really getting held up by the <code>alert</code>, you can fix that with <code>setTimeout</code>:</p> <pre><code>$(function() { url = "http://...../myscript.php"; $.post(url, { action: 'my action', param: 'my parameter'}, {cache: false}); setTimeout(function() { alert("Hello"); }, 0); // Defers it }); </code></pre> <p>If it's the callbacks that are getting held up, the best way to deal with that is to do something more modern than an <code>alert</code>, such as a nicely-styled absolutely-positioned <code>div</code> containing the message. If you want the message to be modal, you can do that yourself although there are some edge conditions around disabling the rest of the page; but there are also about a million "modal" plug-ins for jQuery that handle those edge conditions for you. (Both jQuery UI and Bootstrap provide them, and there are lots of others.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload