Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript confirm dialog box before close browser window

Tags:

javascript

I need to display a confirm dialog box before closing the browser window using javascript or PHP. The confirm box should come up when I click the close button of browser; otherwise, the dialog should not be displayed.

Thanks

like image 458
user881851 Avatar asked Aug 06 '11 10:08

user881851


3 Answers

This will display it when closing the browser:

window.onbeforeunload = function (event) {
  var message = 'Sure you want to leave?';
  if (typeof event == 'undefined') {
    event = window.event;
  }
  if (event) {
    event.returnValue = message;
  }
  return message;
}
like image 199
Marcus Granström Avatar answered Nov 15 '22 02:11

Marcus Granström


Use this code , I used that earlier, I here

<html>
<head>
<title>.:I 0wn U:.</title>
<script language="JavaScript">
<!--
window.onbeforeunload = bunload;

function bunload(){
dontleave="Are you sure you want to leave?";
return dontleave;
}
//-->
</script>
</head>
<body>
Please stay on this page!
</body>
</html>
like image 33
Govind Malviya Avatar answered Nov 15 '22 00:11

Govind Malviya


With Jquery:

$(window).bind('beforeunload', function(e) {
    // Your code and validation
    if (confirm) {
        return "Are you sure?";
    }
});
like image 38
Justo Avatar answered Nov 15 '22 01:11

Justo