<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <script src="js/jquery-1.12.3.min.js"></script> <script> var currentKeyCode = -1, altkey = false, rightKey = false, pageY = 0, listernType = null; $(window).keydown(function () { top.altkey = event.altKey; top.currentKeyCode = event.keyCode; }); $(document).on("contextmenu", function (e) { rightKey = true; }); function isReloadOrClose(event) { var msg = null; if (currentKeyCode == 116 || rightKey) { //116:F5 if (currentKeyCode == 116) { msg = "刷新窗口!(F5)"; } if (rightKey) { msg = "右键刷新窗口!"; } top.listernType = "reload"; } else { msg = "点击关闭窗口"; top.listernType = "close"; if ((altkey || currentKeyCode == 18) || (currentKeyCode == 115)) { msg = "关闭窗口!(alt+F4)"; } if (currentKeyCode == 91) { msg = "Mac关闭窗口!(commad)"; } } currentKeyCode = -1, altkey = false, rightKey = false, pageY = 0, listernType = null; console.log(msg) return msg; } window.onbeforeunload = function (event) { return isReloadOrClose(event) } </script> <body> </body> </html>