在同一窗口和标签页中打开链接

548

我想在同一窗口和同一标签页中打开包含链接的页面。

当我尝试使用window.open打开链接时,它会在新标签页中打开,而不是在同一窗口的同一标签页中打开。


你可能想查看这个帖子,以了解下面提出的方法之间的区别,比如“_self”和“_top”看起来很相似。 - My Work
15个回答

921
你需要使用name 属性:
window.open("https://www.youraddress.com","_self")

编辑:URL 应该以协议开头。如果没有协议,它会尝试打开相对 URL。在 Chrome 59、Firefox 54 和 IE 11 中进行了测试。


2
第二个参数只是新窗口的名称,就像标签a的属性target=一样。实际上,您可以随意命名窗口。您所需要做的就是设置不同的值,以便它不会在同一个窗口或选项卡中打开。 - ijse
15
实际上,有一些特殊的名称,其中之一是"_self",它指的是代码正在运行的窗口/选项卡。 ;) - vdbuilder
5
在MDN [https://developer.mozilla.org/en-US/docs/Web/API/Window/open] 的 window.open() 文档中没有指定 '_self'。更跨浏览器的解决方案是使用 location.replace()。 - Bryan Rayner
1
上面评论中的 MDN 链接自动链接到 404。链接是 https://developer.mozilla.org/en-US/docs/Web/API/Window/open。 - groovecoder
它不起作用,好吧不知道原因。我尝试了window.location.href="....." - Qazi
显示剩余4条评论

201

使用这个:

location.href = "http://example.com";

22
根据 https://dev59.com/y2445IYBdhLWcg3wiayV,这种方式比使用 window.open 更受欢迎。 - Harry
Airbnb的代码检查工具不喜欢location.herf。必须在开头提到window。 - Kick Buttowski

110

14
它不会保存浏览记录,我们不应该使用它。相反,尝试使用window.open("http://www.google.com","_top")。 参考链接:https://www.geeksforgeeks.org/open-a-link-without-clicking-on-it-using-javascript/ - shyam_
2
我正在将其作为内部页面插件的一部分使用,效果非常完美。浏览器历史记录并不是真正的问题,因为它是用于自动化的,使用它的人没有手动访问任何页面的业务。感谢您提供的代码片段! - kevin walker

48

您可以在不指定URL的情况下使其跳转到同一页面:

window.open('?','_self');

4
不是同一页。它将从现有的URL中删除任何查询字符串。 - Quentin

22
如果您的页面在“frame”内,那么“Window.open('logout.aspx','_self')”将被重定向到同一框架中。因此,通过使用
"Window.open('logout.aspx','_top')"
我们可以将页面作为新请求加载。

12

最突出的JavaScript特性之一是能够随时触发onclick处理程序。我发现以下机制比使用location.href=''location.reload()window.open更加可靠:

// this function can fire onclick handler for any DOM-Element
function fireClickEvent(element) {
    var evt = new window.MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true
    });

    element.dispatchEvent(evt);
}

// this function will setup a virtual anchor element
// and fire click handler to open new URL in the same room
// it works better than location.href=something or location.reload()
function openNewURLInTheSameWindow(targetURL) {
    var a = document.createElement('a');
    a.href = targetURL;
    fireClickEvent(a);
}

上述代码还可帮助打开新标签页/窗口并绕过所有弹出窗口拦截器!例如:

function openNewTabOrNewWindow(targetURL) {
    var a = document.createElement('a');
    a.href = targetURL;

    a.target = '_blank'; // now it will open new tab/window and bypass any popup blocker!

    fireClickEvent(a);
}

或者你可以直接执行 a.click(); 而不是整个触发点击事件。 - Tankki3

12

像点击链接一样打开另一个URL

window.location.href = "http://example.com";

11

您必须使用window.open吗?使用window.location="http://example.com"呢?


6

window.open(url, wndname, params),它有三个参数。如果您不想在同一个窗口中打开它,只需设置不同的wndname(窗口名称)。例如:

window.open(url1, "name1", params); // this open one window or tab
window.open(url1, "name2", params); // though url is same, but it'll open in another window(tab).

以下是关于window.open() 的详细信息,您可以放心使用!
https://developer.mozilla.org/en/DOM/window.open
试试看吧~~

8
这个问题非常明确,是关于想要在同一窗口同一标签页中打开的! - SNag

5

使用_self在当前标签页打开URL

const autoOpenAlink = (url = ``) => {
  window.open(url, "open testing page in a same tab page");
}
<a
 href="https://cdn.xgqfrms.xyz/index.html"
 target="_self"
 onclick="autoOpenAlink('https://cdn.xgqfrms.xyz/index.html')">
   open url in the current tab page using `_self`
</a>

使用_blank在新标签页打开链接

const autoOpenAlink = (url = ``) => {
  window.open(url, "open testing page in a new tab page");
}

// ❌  The error is caused by a `StackOverflow` limitation
// js:18 Blocked opening 'https://cdn.xgqfrms.xyz/index.html' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.
<a
 href="https://cdn.xgqfrms.xyz/index.html"
 target="_blank"
 onclick="autoOpenAlink('https://cdn.xgqfrms.xyz/index.html')">
   open url in a new tab page using `_blank`
</a>

参考文献

根据MDN的文档,您只需要给出新窗口/标签页的一个名称。

https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Syntax


你无法可靠地知道当前窗口的名称。最好遵循此2011年答案中的建议,使用特殊的_self名称。 - Quentin
你的第一个例子是错误的。"_blank" 明确表示一个未命名的窗口或选项卡。 - Quentin
如果我设置为_self,它会在当前页面打开,这对我来说很糟糕,我只需要一个新页面。 - xgqfrms
4
问题不是关于你需要什么!如果你想回答一个关于你需要什么的问题,那么请找到一个询问那个的问题。 - Quentin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接