+ 1
How to link a window.confirm in JS?
I made a window.confirm in a js function. function xy() { window.confirm("Want to go to that site?") } How to link that with if?
7 Answers
+ 3
That's how you link , the function with if.
If you mean , moving to another site.
Use window.location;
Like it
function xy(){
if(window.confirm("want to go ?")){
window.location="http://google.com");
}else{
alert("no problem");
}
}
call xy() now, it will ask with dialog box, in case someone select "yes", he will be redirected to site
+ 2
Assign the return value from `confirm` to a variable, then test the variable with `if`
let agree = confirm("Want to go to that site?");
if(agree) // OK button (true)
// code here
else // Cancel button (false)
// code here
+ 2
But how do u make a link?
+ 2
Yes, Ipang is right.
window.confirm("prompt") returns a boolean value
You can use it to either store it in a variable or directly pass the boolean value to condition checking, for example
let flag = window.confirm("want to go to that site?");
if(flag){
doThis();
}
else{
doThat();
}
Or use it like
if(window.confirm("...") ) {
doThis();
}
else{
doThat();
}
Here you are checking if the value is true or false
+ 1
Thank you
0
Okay. But it doesn't answered to my question: how do you make a link in JS?