Open specific link in new tab on pageload

二次信任 提交于 2021-02-16 14:15:15

问题


Previously, I got a solution from here on how to open another link automatically on page load. I got this code to do that

window.setTimeout("autoClick()", 2000); // 2 seconds delay

function autoClick() {
var linkPage = document.getElementById('dynLink').href;
window.location.href = linkPage;
}
</script> 

dynLink is used in the body as target="_blank" within link tag. But this is loading the desired page within the same tab. Not in a New Tab.

I want when this auto page load clicks the link with id=dynLink, the page opens in a new tab then to load in the same tab.

And I really means New TAB - NOT NEW WINDOW.

Looking forward to some working solution. Thanks!


回答1:


You can try this:

 var newTab = window.open('http://google.at', '_blank');
 newTab.location;

Here is the fiddle: http://jsfiddle.net/70kdacL4/310/

For your specific case:

HTML:

<a id="link" href="http://www.google.at" target="_blank">TestLink</a>

JavaScript:

$(document).ready(function(){
    window.setTimeout(function(){
        var link = document.getElementById("link").href;
        var newTab = window.open(link, '_blank');
    }, 5000);
});

Be careful if you use a popup blocker. A popup blocker can prevent the tab from beeing opened.

Here is the fiddle: http://jsfiddle.net/qm9ss6s4/4/




回答2:


Following are some of the solutions using plain HTML and JavaScript. You may use based on your requirement and share if you have any other solution.

Open Link Immediately on Page Load.

<body onload="window.open('https://www.google.com/');">

Open link in New Tab on page load.

<body onload="window.open('https://www.google.com/', '_blank');">

Refresh the same URL Using meta tags.

<meta http-equiv="refresh" content="5" />

Refresh a different URL after 5 seconds Using meta tags.

<meta http-equiv="refresh" content="5;URL='https://www.google.com/'" />

Open different URL in New Tab using meta tags.

<meta http-equiv="refresh" content="5;URL=javascript:window.open('https://www.google.com/', '_blank');" />


来源:https://stackoverflow.com/questions/32423689/open-specific-link-in-new-tab-on-pageload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!