我有一个gridview,我使用页面索引。如果用户导航到下一页,我需要发出警告,例如“确保在移动之前更新页面”,并显示弹出窗口。如果单击是,则应转到另一个页面,或者如果单击取消,则必须保留在同一页面中。
我正在使用此js,但如果点击页面中的任何按钮,则会经常发出警报。如何仅在单击分页时显示消息。
<script type="text/javascript">
var isPaging;
$(".myPagingButton").on("click", function () {
isPaging = true;
});
function goodbye(e) {
if (!isPaging) {
return;
}
isPaging = false;
if (!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'Make sure you have updated the page before navigation?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
window.onbeforeunload = goodbye;
</script>
答案 0 :(得分:0)
如果您只希望它出现在分页按钮上,请在单击按钮时设置标记。
HTML:
<a class="myPagingButon" href="example.html">Paging</a>
JavaScript的:
var isPaging;
$(function(){
$(".myPagingButon").on("click", function() { console.log("click");
isPaging = true;
});
});
function goodbye(e) {
if (!isPaging) {
return;
}
isPaging = false;
return 'Dialog text here.';
}
window.onbeforeunload = goodbye;
答案 1 :(得分:-1)
document.getElementById('yourButton').addEventListener('click', function(){
window.addEventListener('beforeunload', function(){
goodbye();
});
});