【发布时间】:2021-05-11 10:47:23
【问题描述】:
我想在同一个窗口和包含该链接页面的同一选项卡中打开一个链接。
当我尝试使用 window.open 打开链接时,它会在新标签中打开,而不是在同一窗口的同一标签中。
【问题讨论】:
-
您可能想查看此post 以了解以下建议方法之间的差异,例如“相似”的
_self和_top。
标签: javascript html hyperlink href
我想在同一个窗口和包含该链接页面的同一选项卡中打开一个链接。
当我尝试使用 window.open 打开链接时,它会在新标签中打开,而不是在同一窗口的同一标签中。
【问题讨论】:
_self 和_top。
标签: javascript html hyperlink href
你需要使用name属性:
window.open("https://www.youraddress.com","_self")
编辑:Url 前应带有协议。没有它会尝试打开相对 url。在 Chrome 59、Firefox 54 和 IE 11 中测试。
【讨论】:
a的属性target=一样。事实上,您可以随意命名您的窗口。您所需要的只是将其设置为不同的值,这样它就不会在同一个窗口或选项卡中打开。
_self 在 HTML5 W3C 建议 2014 年 10 月 28 日 的 5.1.6 浏览上下文名称 部分中提到:w3.org/TR/html/browsers.html#browsing-context-names(但window.location 仍然更干净)。
使用这个:
location.href = "http://example.com";
【讨论】:
为了确保链接在同一个标签页中打开,你应该使用window.location.replace()
请看下面的例子:
window.location.replace("http://www.w3schools.com");
【讨论】:
你可以在不指定 url 的情况下让它进入同一个页面:
window.open('?','_self');
【讨论】:
如果您的页面位于“框架”内,那么 "Window.open('logout.aspx','_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);
}
【讨论】:
像点击链接一样打开另一个网址
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()的详细信息,你可以相信!
https://developer.mozilla.org/en/DOM/window.open
试试看~~
【讨论】:
使用 html 5,您可以使用 history API。
history.pushState({
prevUrl: window.location.href
}, 'Next page', 'http://localhost/x/next_page');
history.go();
然后在下一页你可以像这样访问状态对象
let url = history.state.prevUrl;
if (url) {
console.log('user come from: '+ url)
}
【讨论】:
就是这样
window.open("www.youraddress.com","_self")
【讨论】:
这很容易。以window.open(url, <tabNmae>) 身份打开第一个窗口
示例:window.open("abc.com",'myTab')
对于接下来的所有 window.open,使用 相同的选项卡名称 而不是 _self、_parent 等。
【讨论】:
Just Try in button.
<button onclick="location.reload();location.href='url_name'"
id="myButton" class="btn request-callback" >Explore More</button>
Using href
<a href="#" class="know_how" onclick="location.reload();location.href='url_name'">Know More</a>
【讨论】:
window/tab 命名。https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Syntax
_self在当前标签页中打开
<a
href="url"
target="_self">
open
</a>
const autoOpenAlink = (url = ``) => {
window.open(url, "open testing page in the same tab page");
}
_blank在新标签页中打开
vue 演示
<div style="margin: 5px;">
<a
:href="url"
@click="autoOpenAlink"
target="_blank"
>
{{url}}
</a>
</div>
vue
autoOpenAlink(e) {
e.preventDefault();
let url = this.url;
window.open(url, "iframe testing page");
},
【讨论】:
你可以的
window.close();
window.open("index.html");
它在我的网站上成功运行。
【讨论】: