Results 1 to 5 of 5

Thread: Cancel the onbeforeunload event in a webpage

  1. #1
    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.

  2. #2
    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?"; } 

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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

Similar Threads

  1. Replies: 5
    Last Post: 16-10-2010, 06:57 PM
  2. Why does onclick event not cancel href navigation?
    By Cordell in forum Software Development
    Replies: 5
    Last Post: 14-05-2010, 12:03 AM
  3. Cancel SelectedIndexChanged event
    By Hayward in forum Software Development
    Replies: 2
    Last Post: 11-06-2009, 09:13 AM
  4. To display webpage inside another webpage
    By Faakhir in forum Technology & Internet
    Replies: 3
    Last Post: 24-03-2009, 10:59 AM
  5. Event Log Error: Event Source:WinMgmt Event ID:10
    By BlackSunReyes in forum Small Business Server
    Replies: 2
    Last Post: 01-03-2007, 03:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,650,980.71315 seconds with 17 queries