Cancel the onbeforeunload event in a webpage
As a web developer i a manage some websites. I want to cancel the onunload or onbeforeunload event. If the user press the refresh button the web page should cancel and perform other function. Like filling out a form if the user try to leave or press F5 a window must popup and prompt the user to confirm leave. I want to add code which make a webpage to cancel the onbeforeload event and start a new event. Same if filling out a form if the use tries to exit the webpage a pop-up window should come up and ask for confirm leave. If the user agrees if clears out the draft data. I tried adding some code by still the page is not responding.
Re: Cancel the onbeforeunload event in a webpage
In my web page i add the below codes to cancel the onbeforeunload event. Try the codes in your web page too to get the result.
PHP Code:
var needToConfirm = false;
function setDirtyFlag()
{ needToConfirm = true; //Function to made changes // add or change webpage }
function releaseDirtyFlag()
{needToConfirm = false; //Function when no alert is required//Called when Save button is clicked}
window.onbeforeunload = confirmExit;
function confirmExit()
{if (needToConfirm) return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; }
Re: Cancel the onbeforeunload event in a webpage
I would suggest you the below html tags for your problem. In your case the unload event is working everytime you try to change page rather than close the window. It means you are terminating a process and logging people out.
The code is as follows :
HTML Code:
<body onUnload="popKill()">
</body>
</html>
<script language="Javascript">
function popKill()
{
window.open('kill.asp','popup','toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,copyhistory=no,scrollbars=no,width=10,height=10');
}
</script>
Then the page opened is call kill.asp which looks like:
<html>
<body>
<% Session.Abandon %>
<script language="Javascript">window.close()</script>
</body>
</html>
This code will cancel the onload or onbeforeunload event.
Re: Cancel the onbeforeunload event in a webpage
By javascript you can prevent a user to close a webpage or redirect him to an another page. You will need to script your html tag with the unload code as specified below.
PHP Code:
function winUnload(){
alert("Unload Window");
}
window.onunload = function(){
winUnload();
}
By this the user will either get a pop-up warning while existing or the user will be redirected to the web page.
The below code i use to cancel the onBeforeUnload event. It works great for me. Review you will understand where is your mistake.
PHP Code:
<html>
<script language="JavaScript">
function checksave(){
if(confirm("Are you sure to leave without saving data?")){
self.close();
}
}
</script>
<body onBeforeUnload="return false; checksave()">
</body>
</html>
Re: Cancel the onbeforeunload event in a webpage
The result you need is that when the user exit or refreshes a page, either a warning window pop-up or his page would be redirected to an another web page. You need to stop your page from getting refreshed by canceling the onbeforeunload event in a web page. You can do this by the below posted codes. I tried them and they giving a good result in my website.
PHP Code:
Page.ClientScript.RegisterStartupScript(typeof(String), "ConfirmClose", @"
<script>
window.onbeforeunload = confirmExit;
function confirmExit()
{
if(postback == false)
return ""Do you want to save changes or discard changes buttons."";
}
</script>");
the save changes button contains the following aspx markup:
OnClientClick="postback=true;return true;"
This sets to cancel onbeforeunload event in a webpage