How can I make a JavaScript alert before a page closes?
I want to write JavaScript code that would, when开发者_如何学Go I close the current HTML page, display an alert message like "Are you sure?"
I want to take the value of the button from the alert message, whatever the user pressed. How can I do this?
You can use window.onbeforeunload
As far as I know, beforeunload
is never fired in Opera.
For supporting that browser, you probably have to build some page-leaving-detection engine yourself (observing links etc).
You could do:
function close_window() {
if (confirm("Close Window?")) {
close();
}
}
Check out this post: How to close current tab in a browser window?
$(document).ready(function(){
$(window).bind("beforeunload", function(e) {
return "alert message";
});
});
精彩评论