skip to Main Content

When I click the link button it opens the page in the same tab. I have the target attribute = "_blank" but it doesn’t work.

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>AMZNindex.html</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-
EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

</head>
<body>

<h1>AMZN/index.html</h1>


<button onclick="document.location='https://finance.yahoo.com/quote/AMZN/'" target="_blank">stock price</button>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-
    MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>

3

Answers


  1. target="_blank" should be used on an anchor element (<a></a>).

    <a href='https://google.com' target='_blank'>Stock price</a>
    

    If you really need a button, use window.open which has a second parameter for opening new tabs:

    <button onclick="window.open('https://google.com', '_blank');">Stock price</button>
    
    Login or Signup to reply.
  2. The target="_blank" attribute should be placed inside the tag, not the button tag. To open a link in a new tab when clicking a button, you can use JavaScript to achieve this.

    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <title>AMZNindex.html</title>
            <link
                href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
                rel="stylesheet"
                integrity="sha384-
    EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
                crossorigin="anonymous"
            />
        </head>
        <body>
            <h1>AMZN/index.html</h1>
    
            <button
                onclick="window.open('https://finance.yahoo.com/quote/AMZN/', '_blank')"
            >
                stock price
            </button>
    
            <script
                src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
                integrity="sha384-
        MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
                crossorigin="anonymous"
            ></script>
        </body>
    </html>
    

    The onclick attribute of the button now uses window.open(‘URL’, ‘_blank’) to open the link in a new tab.

    This should ensure that the link opens in a new tab when the button is clicked.

    Login or Signup to reply.
  3.     window.open('https://finance.yahoo.com/quote/AMZN/','_blank')
    

    How to add target="_blank" to JavaScript window.location?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search