我有以下代码,如果用户刷新了运行良好的页面,它将警告用户。
但是,当用户单击按钮或可能只是 href='#' 甚至是我的网站更改的 url 时,他们也会收到警告。我怎样才能只在他们尝试刷新浏览器时显示此警报?
谢谢
window.onbeforeunload = function (e) {
e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'WARNING: You are about to reload this page, doing so will loose all your open tabs and loose any unsaved data.';
}
// For Safari
return 'WARNING: You are about to reload this page, doing so will loose all your open tabs and loose any unsaved data.';
};