你能帮我解决这个问题。我在Linux中使用Mozilla Firefox。 当我关闭窗口或重新加载页面时,onload事件不起作用
<html>
<head>
<title>onunload test</title>
<script type="text/javascript">
window.onunload = unloadPage;
function unloadPage()
{
alert("unload event detected!");
}
</script>
</head>
<body>
<p>Reload a new page into the browser<br />
to fire the unload event for this page.</p>
<p>You can also use the back or forward buttons<br />
to load a new page and fire this event.</p>
</body>
答案 0 :(得分:1)
试试这段代码:
window.onbeforeunload = function(){
return 'Are you sure you want to leave?'; // or anything you want to do
};
答案 1 :(得分:0)
尝试使用jQuery库,因为我认为jquery会考虑不同类型的浏览器:)
$( window ).unload(function() {
alert( "unload event detected!" );
});