This question already has an answer here:
这个问题已经有了答案:
- Open a URL in a new tab (and not a new window) using JavaScript 26 answers
- 使用JavaScript 26答案在新选项卡(而不是新窗口)中打开URL
How to open a URL in new tab instead of new window programatically?
如何在new选项卡中打开URL而不是按程序打开新窗口?
6 个解决方案
#1
339
Use window.open()
:
使用window.open():
var win = window.open('http://*.com/', '_blank');
if (win) {
//Browser has allowed it to be opened
win.focus();
} else {
//Browser has blocked it
alert('Please allow popups for this website');
}
Depending on the browsers implementation this will work
这取决于浏览器的实现
There is nothing you can do to make it open in a window rather than a tab.
在窗口而不是标签页中,你无法让它打开。
#2
109
This is as simple as this.
这很简单。
window.open('_link is here_', 'name');
Function description:
功能描述:
name
is a name of the window. Following names are supported:
名称是窗口的名称。支持以下名称:
-
_blank
- URL is loaded into a new tab. This is default. - _blank - URL被加载到一个新选项卡中。这是默认的。
-
_parent
- URL is loaded into the parent frame - _parent - URL被加载到父框架中
-
_self
- URL replaces the current page - _self - URL替换当前页面
-
_top
- URL replaces any framesets that may be loaded - _top - URL替换可能加载的任何框架集。
#3
48
if you mean to opening all links on new tab, try to use this jquery
如果您想打开new选项卡上的所有链接,请尝试使用此jquery
$(document).on('click', 'a', function(e){
e.preventDefault();
var url = $(this).attr('href');
window.open(url, '_blank');
});
#4
31
var url = "http://www.example.com";
window.open(url, '_blank');
#5
23
You can easily create a new tab; do like the following:
您可以轻松创建一个新的选项卡;做如下:
function newTab() {
var form = document.createElement("form");
form.method = "GET";
form.action = "http://www.example.com";
form.target = "_blank";
document.body.appendChild(form);
form.submit();
}
#6
8
I know your question does not specify if you are trying to open all a tags in a new window or only the external links.
我知道你的问题没有明确说明你是想在一个新窗口中打开所有的标签还是只打开外部链接。
But in case you only want external links to open in a new tab you can do this:
但如果你只是想要外部链接打开一个新的标签,你可以这样做:
$( 'a[href^="http://"]' ).attr( 'target','_blank' )
$( 'a[href^="https://"]' ).attr( 'target','_blank' )
#1
339
Use window.open()
:
使用window.open():
var win = window.open('http://*.com/', '_blank');
if (win) {
//Browser has allowed it to be opened
win.focus();
} else {
//Browser has blocked it
alert('Please allow popups for this website');
}
Depending on the browsers implementation this will work
这取决于浏览器的实现
There is nothing you can do to make it open in a window rather than a tab.
在窗口而不是标签页中,你无法让它打开。
#2
109
This is as simple as this.
这很简单。
window.open('_link is here_', 'name');
Function description:
功能描述:
name
is a name of the window. Following names are supported:
名称是窗口的名称。支持以下名称:
-
_blank
- URL is loaded into a new tab. This is default. - _blank - URL被加载到一个新选项卡中。这是默认的。
-
_parent
- URL is loaded into the parent frame - _parent - URL被加载到父框架中
-
_self
- URL replaces the current page - _self - URL替换当前页面
-
_top
- URL replaces any framesets that may be loaded - _top - URL替换可能加载的任何框架集。
#3
48
if you mean to opening all links on new tab, try to use this jquery
如果您想打开new选项卡上的所有链接,请尝试使用此jquery
$(document).on('click', 'a', function(e){
e.preventDefault();
var url = $(this).attr('href');
window.open(url, '_blank');
});
#4
31
var url = "http://www.example.com";
window.open(url, '_blank');
#5
23
You can easily create a new tab; do like the following:
您可以轻松创建一个新的选项卡;做如下:
function newTab() {
var form = document.createElement("form");
form.method = "GET";
form.action = "http://www.example.com";
form.target = "_blank";
document.body.appendChild(form);
form.submit();
}
#6
8
I know your question does not specify if you are trying to open all a tags in a new window or only the external links.
我知道你的问题没有明确说明你是想在一个新窗口中打开所有的标签还是只打开外部链接。
But in case you only want external links to open in a new tab you can do this:
但如果你只是想要外部链接打开一个新的标签,你可以这样做:
$( 'a[href^="http://"]' ).attr( 'target','_blank' )
$( 'a[href^="https://"]' ).attr( 'target','_blank' )