1

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
asked Mar 29, 2017 at 14:05
2
  • You should use window.open("http://www.google.com") and just place it where your x = "You pressed Ok!"; is. Commented Mar 29, 2017 at 14:09
  • A simple search of the internet for "how to open a window in javascript" would have found the answer to this. Commented Mar 29, 2017 at 14:18

3 Answers 3

2
if (confirm("Press a button!")) {
 window.open('www.google.com')
}
answered Mar 29, 2017 at 14:08
Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.