Skip to main content
Inspiring
July 1, 2024
Question

html button won't open link in a new window/tab when using window.location

  • July 1, 2024
  • 3 replies
  • 641 views

When applying a link action to a button, I cannot get it to open in a new window/tab:

 

<button onclick="window.location.href='https://google.com';" target="_blank" type="button" class="btn btn-outline-primary btn-lg ctaBtn" style="margin-top: 1em;">Request Sample</button>

 

Can you please advise what must be applied to get the button link to open in a new window/tab?

 

Thanks.

    This topic has been closed for replies.

    3 replies

    Nancy OShea
    Community Expert
    Community Expert
    July 1, 2024

    FYI:  JavaScript uses different terms & syntax. For details, see below.

    https://www.geeksforgeeks.org/how-to-open-url-in-new-tab-using-javascript/#

     

    Nancy O'Shea— Product User & Community Expert
    BenPleysier
    Community Expert
    Community Expert
    July 1, 2024

    Adding to @r_tist's reply, the code would look like

     

    <a href="https://google.com" class="btn btn-outline-primary btn-lg mt-4" target="_blank">Request Sample</a>
    

     

     

    This code is semantically more correct than using a button element because of the following statements:

    • Links take the user to a new location, such as a new web page or new section of the current page. 
    • Buttons trigger some action, such as showing content on the page that was previously hidden, playing a video, or submitting a form.
    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    r_tistAuthor
    Inspiring
    July 1, 2024

    Thank you for this clear explanation!

    Very good.

    Community Expert
    July 1, 2024

    Are you using Bootstrap? With all of those button classes, why not just change the button --> a and just have a stylized link that looks like a button? Then you can remove the javascript and use a href value.