简体   繁体   中英

How to open a link in new window?

I need to open a new link automatically inside

if(isset($_POST['download'])) {
...
}

after user clicks on submit button download.

I tried document.location, but it opens a link in the same tab, and window.open requires that the browser should have pop-ups enabled, which can be annoying to users, plus in Chrome (may be also in some other browsers), target="_blank" opens a link in new window, not in a new tab.

Is there anything I can use to open a window normally in a new tab, like with <a href=""></a> ?

All modern browser now contain tab functionality.

Try window.open(), browser will automatically will do task as per its configuration.Different browser treat target="_blank" differently.Mozilla open in new tab while chrome not.It is not up to you.

You cannot ensure they will open it in a new window, but you can make an effort at it.

For example:

<a href="go/Where-ever" target="_blank">Link Text</a>

Like you said, you can do this with JavaScript

var anchors = document.getElementsByTagName('a'); // or another selector

for ( var i in anchors ) 
    anchors[i].onclick = function () { return !window.open(this); };

Inline JavaScript

<a href="go/Where-ever" onclick="window.open(this.href);return false;">Link Text</a>

And jQuery

$(".linkSelector a").prop("target","_blank");

But remember, browser settings, Right-Click->Open in New Tab, and Right-Click->Open in New Window can override all of this. But that's not really a bad thing generally - most people like windows opening as tabs/windows and they get irritated at, or don't pay attention to anything that ignores their wishes.

window.open()用于完成任务, 您可以在此处查看示例

以下代码将帮助您在新窗口中打开页面。

 window.open("https://stackoverflow.com", "_blank","toolbar=yes,top=200,left=200,width=800,height=800");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM