How to link a window.confirm in JS? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

19th Jan 2020, 1:45 PM
Philipp Makarov
Philipp Makarov - avatar
6 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
19th Jan 2020, 2:18 PM
Sandeep Chatterjee
+ 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
19th Jan 2020, 1:56 PM
Ipang
+ 2
But how do u make a link?
19th Jan 2020, 2:06 PM
Philipp Makarov
Philipp Makarov - avatar
+ 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
19th Jan 2020, 2:09 PM
Sandeep Chatterjee
+ 1
Thank you
19th Jan 2020, 2:51 PM
Philipp Makarov
Philipp Makarov - avatar
0
Okay. But it doesn't answered to my question: how do you make a link in JS?
19th Jan 2020, 2:12 PM
Philipp Makarov
Philipp Makarov - avatar