3

I want to create a <button> that when clicked will open a URL in a new tab.

This is easy to achieve in pure HTML, but the catch is that the <button> is dynamically generated, thus this has to be done in JavaScript.

This is the code I used to create a <button> so far using JavaScript and HTML DOM:

<body>
  <div id = 'only_div'>
  </div>
  <script>
    var btn=document.createElement("button");
    btn.innerText = "Click me";
    document.getElementById("only_div").appendChild(btn);
  </script>
</body>

In this existing code, I just want to add that when clicked, a URL (for example http://www.stackoverflow.com) will open in a new tab.

5 Answers 5

5

You can add a click event listener to the button with addEventListener() and then when it's trigger use window.location.replace() to redirect.

<body>
    <div id = 'only_div'>
    </div>

    <script>
      var btn=document.createElement("button");
      btn.innerText = "Click me";
      btn.addEventListener("click", ()=>{
         window.location.replace(URL);
      });
      document.getElementById("only_div").appendChild(btn);
    </script>

</body>

for a new tab use:

window.open(URL, '_blank');

Docs:

Sign up to request clarification or add additional context in comments.

1 Comment

This works but unfortunately, it doesn't open the URL in a new tab.
1

You could do something like the following, which will open the tab in a new tab and go to this tab:

function openInNewTab(url) {
  const win = window.open(url, '_blank');
  win.focus();
}

const btn=document.createElement("button");
btn.innerText = "Click me";
btn.addEventListener('click', function () {
    openInNewTab('http://www.test.com')
});
document.getElementById("only_div").appendChild(btn);

See js fiddle here.

2 Comments

It worked! But many people provided with may correct answers. What is the policy on SO, which answer to accept as the correct one?
this answer was the first one, and it does what you asked so this should probably be the accepted one, but who knows 🤷‍♂️up to you
1

I think you can simply add

btn.onclick = function() { // Note this is a function window.open('http://www.stackoverflow.com/', '_blank'); };

and that should do it.

Comments

1

You have to attach the event (click) to the button. You can do so by using EventTarget.addEventListener(). You can use Window.open() to load the specified resource into the browsing context.

Your code should be like the following:

var btn=document.createElement("button");
btn.innerText = "Click me";
// attach the event
btn.addEventListener('click', function(){
  window.open('http://www.stackoverflow.com', '_blank'); // _blank will open the site in a new tab
});
document.getElementById("only_div").appendChild(btn);

Comments

1

Use setAttribute():

btn.setAttribute("onclick","window.open('https://stackoverflow.com','_blank')");

Comments

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.