简体   繁体   中英

Alert message in Javascript

I am using simple JavaScript code in my PHP code to display an alert message:

<script type="text/javascript">
    window.alert("Can not Send Messages ")
</script>

When the alert message is displayed, the original page disappears, when I press "OK" button in the message box, the original page will appear again. Is there any way to display the error message and to keep the original page in the back? My other question, is there any way to decorate the message box ^_^ ?

Thanks and Regards for all.

alert() blocks until it has been closed. Simple execute it after the document itself has been loaded:

window.onload = function() {
    alert("Cannot send messages");
}

In case you are using something like jQuery you could also use its domready event (it's available without jQuery, too, btw):

$(document).ready(function() {
    alert("Cannot send messages");
});

Move that script to the end, after your tag. That way the browser has received enough HTML to construct the page, giving it something to render behind the popup.

If you want to decorate system "alert" message you can create your own function and replace default. But you should be sure that there is no libs that uses default alert, or do the follow:

// Preserve link to default alert
var defaultAlert= window.alert;

function myAlert(msg, decorate) {
    if (!decorate)  return defaultAlert(msg);

    // Display decorated alert
    // ... draw beauty div
}

window.alert= myAlert;

And that your alert would not pause execution flow like alert do.

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