I have a button something like thi开发者_StackOverflow社区s 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
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.com'">
<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."
精彩评论