My client wishes to show an alert when entering a web page.
However, an alert blocks further loading of the page until "Ok" is clicked, and the page needs to load in the background while the alert is displayed.
One solution would be to use a custom alert using HTML and CSS, but this is not answering the client's needs.
Is there a solution using 'core javascript' or jQuery to allow the page to load while the alert is displayed? Thanks.
My client wishes to show an alert when entering a web page.
However, an alert blocks further loading of the page until "Ok" is clicked, and the page needs to load in the background while the alert is displayed.
One solution would be to use a custom alert using HTML and CSS, but this is not answering the client's needs.
Is there a solution using 'core javascript' or jQuery to allow the page to load while the alert is displayed? Thanks.
Share Improve this question edited Jun 4, 2013 at 9:35 Nick Andriopoulos 10.6k6 gold badges34 silver badges56 bronze badges asked Jun 4, 2013 at 8:56 Dipesh ParmarDipesh Parmar 27.4k8 gold badges64 silver badges92 bronze badges 2- Why not just wait that the page is loaded to display the alert? – FLX Commented Jun 4, 2013 at 8:59
- I understand... So you could make a custom alert that looks exactly the same as the native alert :) – FLX Commented Jun 4, 2013 at 9:04
3 Answers
Reset to default 8You should run it through a setTimeout:
setTimeout(function() { alert('hello world'); }, 1);
This is shown here: Is there a JavaScript alert that doesn't pause the script?
EDIT
see @FunKy's answer for a workaround in Firefox. (really interesting!)
Simple answer is no, it's not possible!
Reason is native alert()
dialog is UI blocking and javascript is a single thread language.
You have to use a customized dialog message.
But, if some ajax request are done just before blocking UI, when alert()
dialog is closed, the ajax response will/should be available. So, you still can make some ajax request just before showing alert.
You can use a custom dialog box instead of an alert box (which block page load). For exemple, Jquery UI Dialog is easy to use (but there are many plugins which do exactly the same thing).