Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Cancel the onbeforeunload event in a webpage

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 21-05-2009
Member
 
Join Date: May 2009
Posts: 54
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.

Reply With Quote
  #2  
Old 21-05-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
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?"; } 
Reply With Quote
  #3  
Old 21-05-2009
Member
 
Join Date: May 2008
Posts: 2,008
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.
Reply With Quote
  #4  
Old 21-05-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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> 
Reply With Quote
  #5  
Old 21-05-2009
Member
 
Join Date: May 2008
Posts: 2,293
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Cancel the onbeforeunload event in a webpage"
Thread Thread Starter Forum Replies Last Post
Cancel reoccurring event in the future without losing per-event changes Chin-Mae Windows Software 5 16-10-2010 06:57 PM
Why does onclick event not cancel href navigation? Cordell Software Development 5 14-05-2010 12:03 AM
Cancel SelectedIndexChanged event Hayward Software Development 2 11-06-2009 09:13 AM
To display webpage inside another webpage Faakhir Technology & Internet 3 24-03-2009 10:59 AM
Event Log Error: Event Source:WinMgmt Event ID:10 BlackSunReyes Small Business Server 2 01-03-2007 03:27 AM


All times are GMT +5.5. The time now is 09:18 AM.