我想在同一窗口和同一标签页中打开包含链接的页面。
当我尝试使用window.open
打开链接时,它会在新标签页中打开,而不是在同一窗口的同一标签页中打开。
我想在同一窗口和同一标签页中打开包含链接的页面。
当我尝试使用window.open
打开链接时,它会在新标签页中打开,而不是在同一窗口的同一标签页中打开。
window.open("https://www.youraddress.com","_self")
编辑:URL 应该以协议开头。如果没有协议,它会尝试打开相对 URL。在 Chrome 59、Firefox 54 和 IE 11 中进行了测试。
a
的属性target=
一样。实际上,您可以随意命名窗口。您所需要做的就是设置不同的值,以便它不会在同一个窗口或选项卡中打开。 - ijsewindow.location.href="....."
。 - Qazi使用这个:
location.href = "http://example.com";
window.location.replace()
。window.location.replace("http://www.w3schools.com");
您可以在不指定URL的情况下使其跳转到同一页面:
window.open('?','_self');
"Window.open('logout.aspx','_top')"
我们可以将页面作为新请求加载。最突出的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);
}
像点击链接一样打开另一个URL
window.location.href = "http://example.com";
您必须使用window.open
吗?使用window.location="http://example.com"
呢?
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()
的详细信息,您可以放心使用!_self
在当前标签页打开URLconst 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