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
target="_blank"
should be used on an anchor element (<a></a>
).If you really need a button, use
window.open
which has a second parameter for opening new tabs: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.
The onclick attribute of the button now uses window.open(‘URL’, ‘_blank’) to open the link in a new tab.
How to add target="_blank" to JavaScript window.location?