我的表单包含多个按钮和超链接,我想在浏览器的后退按钮上显示警报。我使用了以下代码,它在 Chrome 和 Safari 上运行良好,但在 IE 和 Firefox 中无法运行:
window.onload = function() {
var btnClicks = document.getElementsByClassName('noPopup');
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
links[i].onclick = setGlobal;
}
for (var i = 0; i < btnClicks.length; i++) {
btnClicks[i].onclick = setGlobal;
}
function setGlobal() {
window.btn_clicked = true;
window.linkClicked = true;
}
window.onbeforeunload = function() {
if (!window.btn_clicked || !window.linkClicked) {
return 'Would you like to save first.';
}
};
};