Javascript 在同一窗口和同一选项卡中打开 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8454510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open URL in same window and in same tab
提问by user1065055
I want to open a link in the same window and in the same tab that contains the page with the link.
我想在同一个窗口和包含带有链接的页面的同一个选项卡中打开一个链接。
When I try to open a link by using window.open
, then it opens in new tab—not in the same tab in the same window.
当我尝试使用打开链接时window.open
,它会在新选项卡中打开,而不是在同一窗口的同一选项卡中。
回答by vdbuilder
You need to use the name attribute:
您需要使用 name 属性:
window.open("https://www.youraddress.com","_self")
Edit: Url should be prepended with protocol. Without it tries to open relative url. Tested in Chrome 59, Firefox 54 and IE 11.
编辑:Url 应该加上协议。没有它会尝试打开相对网址。在 Chrome 59、Firefox 54 和 IE 11 中测试。
回答by Dave L.
Use this:
用这个:
location.href = "http://example.com";
回答by andromeda
In order to ensure that the link is opened in the same tab, you should use window.location.replace()
为了确保链接在同一个选项卡中打开,您应该使用 window.location.replace()
See the example below:
请参阅下面的示例:
window.location.replace("http://www.w3schools.com");
回答by Tom Berthold
You can have it go to the same page without specifying the url:
您可以在不指定 url 的情况下将其转到同一页面:
window.open('?','_self');
回答by Magesh
If you have your pages inside "frame" then "Window.open('logout.aspx','_self')"
如果您的页面位于“框架”内,则“Window.open('logout.aspx','_self')”
will be redirected inside same frame. So by using
将在同一帧内重定向。所以通过使用
"Window.open('logout.aspx','_top')"
we can load the page as new request.
我们可以将页面作为新请求加载。
回答by Muaz Khan
One of the most prominent javascript features is to fire onclick handlers on the fly. I found following mechanism more reliable than using location.href=''
or location.reload()
or window.open
:
最突出的 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);
}
Above code is also helpful to open new tab/window and bypassing all pop-up blockers!!! E.g.
上面的代码也有助于打开新标签/窗口并绕过所有弹出窗口阻止程序!!!例如
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);
}
回答by DarckBlezzer
Open another url like a click in link
打开另一个网址,如点击链接
window.location.href = "http://example.com";
回答by Jeffrey Zhao
Do you have to use window.open
? What about using window.location="http://example.com"
?
你必须使用window.open
吗?怎么用window.location="http://example.com"
?
回答by ijse
window.open(url, wndname, params)
, it has three arguments. if you don't want it open in the same window, just set a different wndname. such as :
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).
Here is the details about window.open()
, you can trust it!
https://developer.mozilla.org/en/DOM/window.open
这是关于的详细信息window.open()
,你可以相信它!
https://developer.mozilla.org/en/DOM/window.open
have a try ~~
试试~~
回答by Adi Prasetyo
With html 5 you can use history API.
使用 html 5,您可以使用history API。
history.pushState({
prevUrl: window.location.href
}, 'Next page', 'http://localhost/x/next_page');
history.go();
Then on the next page you can access state object like so
然后在下一页上,您可以像这样访问状态对象
let url = history.state.prevUrl;
if (url) {
console.log('user come from: '+ url)
}