Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Check whether a user clicked 'Ok' or 'Cancel' while using Confirm() function?

Tags:

javascript

I am using a confirm() function for logging out. How can i check whether the user clicked Ok or Cancel. My confirm function is

confirm('Are You Sure')

But now, while clicking both Ok and Cancel the page redirects. How can I resolve it?

like image 615
arunrc Avatar asked Mar 29 '12 05:03

arunrc


2 Answers

if (confirm('Are You Sure?')){
   window.location = "http://www.google.com/";
}else{
   alert("You are not redirected.")
}

DOCUMENTATION

like image 60
dku.rajkumar Avatar answered Oct 10 '22 00:10

dku.rajkumar


You need to get the return value of confirm, and then decide what to do next.

var answer = confirm('Are You Sure');
if (answer) {
   //...
} else {
   //...
}
like image 34
xdazz Avatar answered Oct 10 '22 01:10

xdazz