简体   繁体   中英

window.alert close handler?

Is there an easy way to execute some code AFTER the closing of a window.alert popup? I have a substantially large library to load and my plan was to throw up a welcome message like

window.alert("Welcome to my site!")

just to give the browser a second or two to make sure everything loads. I'm using window.confirm right now, as I can catch the confirm event after the button has been clicked, but there's really no need for me to have two options. Any suggestions on how to catch the "confirm" on the "Ok" message for window.alert()? Also, will your solution work cross browser (or at least on the later editions of FF, IE, and Safari)?

window.alert() stops the execution flow of the script, so your it's enough to put your code after the call to alert() .

Example:

alert("I am waiting for you to click OK");
/* Your code here */

However, if you want to check if "everything" has been loaded, this method will likely not work. Consider using, for example, the body's onload event handler.

<body onload="mycode();">
  ....
</body>

For advanced methods, consider using the jQuery library, which lets you achieve your purpose by means of the $(document).ready() handler.

alert is not an asynchronous method. No javascript runs until alert closes.

You should really be using jQuery's $(documnet).ready() or an equivalent from another library to wait for the page to load before javascript execution.

The code line next to

window.alert("Welcome to my site!")

will execute immediately after the alert window closes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM