I am trying to run the following JavaScript program,
function myFunction() {
var x;
if (confirm("Press a button!") == true) {
x = "You pressed Ok!";
} else {
x = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = x;
}
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
But I really need is not just saying "You pressed Ok" instead of that it need to open a link. Help me please.
I hope
openWindow("http://fb.com")
this line will help, but don't know where to place it and make a working program.
Heretic Monkey
12.2k7 gold badges63 silver badges133 bronze badges
3 Answers 3
if (confirm("Press a button!")) {
window.open('www.google.com')
}
answered Mar 29, 2017 at 14:08
Joshua J Wilborn
5463 silver badges13 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
John Weisz
FYI,
if (confirm(...)) is identical to if (confirm(...) == true), because the condition statement inside an if is evaluated as a boolean expression, and checking whether any given boolean expression e for e == true is essentially the same as the expression itself.You just need to change your function and place your openWindow() call inside it. Because I don't know what your openWindow() function does, I would use window.open():
function myFunction() {
window.open("http://fb.com");
}
answered Mar 29, 2017 at 14:09
hering
1,9524 gold badges29 silver badges43 bronze badges
Comments
function myFunction() {
var x;
if (confirm("Press a button!") == true) {
location.href = "http://www.google.com";
} else {
x = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = x;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
answered Mar 29, 2017 at 14:10
RAJNIK PATEL
1,0491 gold badge17 silver badges30 bronze badges
Comments
default
window.open("http://www.google.com")and just place it where yourx = "You pressed Ok!";is.