I would like to capture href of anchor tag<a> on click, and open that link in new tab using javascript or jquery, how to do this?
Try opening pages using Window.open
with a different window name will open in a new browser window like a popup, OR open in a new tab, if the user has allowed the browser to do so.
Your code should be
var win = window.open('https://qawithexperts.com/', '_blank');
if (win) {
//Browser allows the new tab to open
win.focus();
} else {
//Browser has blocked it
alert('Please allow popups for this website');
}
Or try this code to
<a href="url" target="_newtab">Anchor Text</a>
In javascript you can use
window.open('page.html','_newtab');
For HTML check Open link in new tab using HTML
window.open("url","_blank")
, may be blocked by chrome or any other browsers by pop-up so you may like to try this JS code
document.querySelector('#myButton').onclick = function() {
var redirectWindow = window.open('http://google.com', '_blank');
redirectWindow.location;
};
OR
You can also give a try to below Javascript code
<a class="link">Link</a>
<script type="text/javascript">
$("a.link").on("click",function(){
window.open('www.yourdomain.com','_blank');
});
</script>
Using jQuery, you can open new tab in browser with code below
$('#btnId').click(function () {
var redirectWindow = window.open('http://google.com', '_blank');
redirectWindow.location;
});
Using Pure Javascript
function openInNewTab(url) {
window.open(url, '_blank').focus();
}
and you can use it as below in HTML
<div onclick="openInNewTab('www.google.com');">Open in New Tab</div>
That's it.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly