views:

45

answers:

3

Hello Friends,

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

+2  A: 

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;
  }
});
Chris
i am confused why you have chosen the id as `delete`.
Rakesh Juyal
An id is just a unique identifier for an html element. You can call the id anything you want that is valid within the html specification. I changed it to exitApplication, but it makes no difference what this id is. I had this code handy from an application I am working on so I pasted it in and delete was the ID I was using but you are welcome to change it. Just if you change id to something else make sure you change the jquery selector as well to reflect as I did in my example.
Chris
+1  A: 

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'
   }
});
Sarfraz
Thanks If I have somethign like this what should I do..?
kumar
@kumar: I have updated the code for that :)
Sarfraz
Yes Sarfraz i got it. thanks
kumar
@kumar: Welcome...
Sarfraz
+1  A: 

If you've this...

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

... there are couple ways to achieve this.

Both Chris & Sarfraz solutions work. What you can do as well is 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.com'"&gt; 
<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."

DashK
Just keeping this Body code will work for any button? in the view? or only for navigating? please can you provide me comeplete code Ken thanks i am confuse..
kumar
My mistake. It's window's onbeforeunload, not body's onbeforeunload. I've edited my post to reflect it. (Comment won't let me post code nicely.)
DashK