Opening link in new tab without clicking on link

22.8k Views Asked by At

I'm currently (in HTML) trying to load a link in a new tab or window right when the website is opened, without anyone clicking on a link on a page. I've so far managed to open a link automatically and open a link in a new tab and window, but not at the same time. Can someone help me with this? I also don't mind using another language if this is not possible in HTML.

3

There are 3 best solutions below

0
On

If you want to open a new page in new window once the main page of the website is loaded, try this by calling the onload javascript function in the body:

<body onload="myfunction()">

And then in myfunction() you can call try this !

window.open(url, '_blank');
0
On

First question why do you want to do that? Secondly You can use javascript for that.

function OpenInNewTab(url) {
  var win = window.open(url, '_blank');
  win.focus();
}

And in your HTML put

<body onload=OpenInNewtab('http.....')>
.......
</body>
0
On

Here is the code to open a link in new tab on page load using jquery.

<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
jQuery(document).ready(function () {
newTab();
});

function newTab() {
 var form = document.createElement("form");
 form.method = "GET";
 form.action = "http://www.google.com";
 form.target = "_blank";
 document.body.appendChild(form);
 form.submit();
}
</script>
</head>
<body>
<a class="my-link">link</a>
</body>
</html>