简体   繁体   中英

Prevent browser from closing in asp.net

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" [ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM