开发者

Prevent browser from closing in asp.net

开发者 https://www.devze.com 2023-01-07 21:19 出处:网络
I am using asp.net 2.0 with c#. i want a pop up to be displayed when user tries to close the browser and if user click on \"no\" [i.e. he don\'t want browser to be closed] then it prevent browser to

I am using asp.net 2.0 with c#.

i want a pop up to be displayed when user tries to close the browser and if user click on "no" [i.e. he don't want browser to be closed] then it prevent browser to get closed. Please help. 开发者_运维技巧Thanks


the code they use is

window.onbeforeunload=function() {
  if (somereasonorother) return "You did not save your stuff"
}


Pointy, this is entirely possible, and it's done by many web pages for perfectly reasonable reasons.

Try something like this:

function areYouSure() {
    return "Are you sure you want to leave this page?";
}
window.onbeforeunload = areYouSure;


You can try to attach yourself to the onbeforeunload event:

<body onbeforeunload="ConfirmClose();">

But I have to mention that it won't work on all browsers. The only ones that prompted something after I closed a window were Chrome, Firefox and Internet Explorer; Opera ignored the code in the JavaScript method.

This is mostly because some browsers trigger the onbeforeunload event only when you're trying to leave the current page by visiting another one, and not when you close the current window / tab.

0

精彩评论

暂无评论...
验证码 换一张
取 消