关闭浏览器窗口之前的javascript确认对话框

use*_*851 6 javascript

我需要在使用javascript或PHP关闭浏览器窗口之前显示确认对话框.单击浏览器的关闭按钮时,应出现确认框; 否则,不应显示该对话框.

谢谢

小智 12

这将在关闭浏览器时显示:

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;
}
Run Code Online (Sandbox Code Playgroud)


Gov*_*iya 4

使用这段代码,我之前用过,我在这里

<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>
Run Code Online (Sandbox Code Playgroud)