最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to show the popup message on Exit button - Stack Overflow

programmeradmin1浏览0评论

I have a button something like this what should I do?

<input type="button" value="Exit" onclick="JavaScript:window.location.href='../index'"/>

On Exit I am going to Other view..

On click on Exit I need to display popup window saying Are you sure you want to Exit? with Ok and Cancel buttons

anybody tell me out to do this?

thanks

I have a button something like this what should I do?

<input type="button" value="Exit" onclick="JavaScript:window.location.href='../index'"/>

On Exit I am going to Other view..

On click on Exit I need to display popup window saying Are you sure you want to Exit? with Ok and Cancel buttons

anybody tell me out to do this?

thanks

Share Improve this question edited Sep 20, 2010 at 17:37 kumar asked Sep 20, 2010 at 17:20 kumarkumar 2,94418 gold badges62 silver badges90 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

JavaScript:

<input type="button" value="Exit" onclick="confirmBox()" />

function confirmBox(){
  if (confirm('Are you sure you want to Exit?')){
    window.location.href = '../index'
  }
}

jQuery:

<input type="button" value="Exit" id="exit" />

$('#exit').click(function(){
   if (confirm('Are you sure you want to Exit?')){
     window.location.href = '../index'
   }
});

For an input:

<input type="submit" id="exitApplication" value="Exit"/>

Using jquery you can:

$("#exitApplication").click(function(){
  var answer = confirm("Are you sure you want to exit?")
  if(!answer){
      //false when they click cancel 
      return false;
  }
});

If you've this...

<input type="button" value="Exit" onclick="JavaScript:window.location.href='../index'"/>

... there are a couple of ways to achieve this.

Both Chris & Sarfraz solutions work. What you can do as well as to add a confirm(...) dialog in the page's onbeforeunload event.

<html>
  <body> 
    Click this button to go to Google's homepage.
    <input type="button" value="Exit" onclick="window.location.href='http://www.google.'">

    <script type="text/javascript"> 
      window.onbeforeunload = function() {
        return "Are you sure?";
      }
    </script>  

  </body>
</html>

This will guarantee that, not only when the user clicks the Exit button, but also when s/he tries to navigate away from the page (For example, clicking the "Back" button in the browser.), onbeforeunload event will be triggered, and prompt the user to make sure s/he really wants to leave the page.

confirm(...) returns true if the user says "Yes, I want to leave the page."

发布评论

评论列表(0)

  1. 暂无评论