0

I'm trying to open a window by passing in a variable url from the "demo" paragraph. I am probably doing something very obviously wrong but am very new to javascript. Any help much appreciated as this doesnt work!

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Click me</button>
<p id="demo">www.google.com</p>

<script>
function myFunction() {
var name = document.getElementById("demo").innerHTML;
var name2 = encodeURIComponent(name);
window.open(name2, "_blank");
}
</script>
</body>
</html>
1

1 Answer 1

2

Just add "http://" string

window.open("http://"+name2, "_blank");
Sign up to request clarification or add additional context in comments.

2 Comments

Better to insert correct url (with http/https) in demo div don't you think ?
Maybe... it depends on what he want to do with it

Your Answer

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.