Skip to main content
Inspiring
Jul 1, 2024 12:02 PM UTC
Question

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

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 is closed to new replies. Start a new post to keep the conversation going.

    4 replies

    Nancy OShea
    Community Expert
    Community Expert
    Jul 1, 2024 7:14 PM UTC

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

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

     

    Translate
    Nancy O'Shea— Product User & Community Expert
    BenPleysier
    Community Expert
    Community Expert
    Jul 1, 2024 12:26 PM UTC

    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.
    Translate
    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    r_tistAuthor
    Inspiring
    Jul 1, 2024 1:08 PM UTC

    Thank you for this clear explanation!

    Very good.

    Translate
    Community Expert
    Jul 1, 2024 12:09 PM UTC

    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.

    Translate