alert with "YES" and "NO" button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • senthilkumarb
    New Member
    • Sep 2008
    • 7

    alert with "YES" and "NO" button

    Hi every body,

    i need a snippet in JavaScript to perform close window(browser)
    when we close window it should give alert with "YES" & "NO" button if yes the window should stay still otherwise window should close
  • senthilkumarb
    New Member
    • Sep 2008
    • 7

    #2
    alert with "YES&qu ot; and "NO&quo t; button

    Hi every body,

    i need a snippet in JavaScript to perform close window(browser)
    when we close window it should give alert with "YES" & "NO" button if yes the window should stay still otherwise window should close

    Comment

    • Rsmastermind
      New Member
      • Sep 2008
      • 93

      #3
      Hi this is your solution the internet Explorer itself provides the confirm() functionality where you can find the two buttons ok and cancel and the corresponding code on the basis of input is shown below with the example

      You should call this function on the event onunload

      like this
      Code:
      ///////////////////
      <body onunload="windowClose();"
      ////////////////////////////////////////////////
      
      
      <script type="text/javascript">
      function windowClose()
      {
      var r=confirm("Window will be closed do U want to Proceed");
      if (r==true)
        {
                  top.close();  
        }
      else
        {
               (Do whatever you like);
        }
      }
      Last edited by acoder; Sep 20 '08, 03:59 PM. Reason: Added [code] tags

      Comment

      • bnashenas1984
        Contributor
        • Sep 2007
        • 257

        #4
        Hi
        I think you have posted your question in the wrong forum BUT anyway , here is your answer:

        Code:
        <head>
        <script type="text/javascript">
        	var myclose = false;
        	function ConfirmClose()
        	{
        		if (event.clientY < 0)
        		{
        			event.returnValue = 'Are you sure you want to close this window!';
        			setTimeout('myclose=false',100);
        			myclose=true;
        		}
        	}
        </script>
        </head>
        <body onbeforeunload="ConfirmClose()">
        </body>
        Please note that this code only works for IE.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Not just Internet Explorer - every browser (or at least every major one) has a confirm() method.

          Instead of onunload, you'd have to use onbeforeunload though because once it gets to the unload event, you can't prevent it. Note that onbeforeunload is not supported in all browsers.

          One final point to bear in mind: it is rarely a good idea to have a confirmation when the user clearly wants to leave the page/close the browser.

          Comment

          Working...