use*_*055 323 html javascript href hyperlink
我想在同一窗口和包含带链接的页面的同一选项卡中打开一个链接.
当我尝试使用打开链接时window.open
,它会在新选项卡中打开 - 而不是在同一窗口的同一选项卡中.
vdb*_*der 547
您需要使用name属性:
window.open("https://www.youraddress.com","_self")
Run Code Online (Sandbox Code Playgroud)
编辑:网址应该附加协议.没有它试图打开相对网址.在Chrome 59,Firefox 54和IE 11中测试过.
Dav*_* L. 155
用这个:
location.href = "http://example.com";
Run Code Online (Sandbox Code Playgroud)
and*_*eda 46
为了确保在同一选项卡中打开链接,您应该使用 window.location.replace()
请参阅以下示例:
window.location.replace("http://www.w3schools.com");
Run Code Online (Sandbox Code Playgroud)
资料来源:http://www.w3schools.com/jsref/met_loc_replace.asp
小智 24
您可以在不指定网址的情况下将其转到同一页面:
window.open('?','_self');
Run Code Online (Sandbox Code Playgroud)
Mag*_*esh 19
如果你的页面在"frame"中,那么"Window.open('logout.aspx','_ self')"
将被重定向到同一帧内.所以通过使用
"Window.open('logout.aspx','_top')"
Run Code Online (Sandbox Code Playgroud)
我们可以将页面作为新请求加载.
Mua*_*han 11
其中一个最突出的JavaScript功能是即时启动onclick处理程序.我发现以下机制比使用location.href=''
or 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);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码也有助于打开新的选项卡/窗口并绕过所有弹出窗口阻止程序!例如
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);
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*zer 10
打开另一个URL,如点击链接
window.location.href = "http://example.com";
Run Code Online (Sandbox Code Playgroud)
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).
Run Code Online (Sandbox Code Playgroud)
这里有详细介绍window.open()
,值得信赖!
https://developer.mozilla.org/en/DOM/window.open
试试~~
_self
const autoOpenAlink = (url = ``) => {\n window.open(url, "open testing page in a same tab page");\n}
Run Code Online (Sandbox Code Playgroud)\r\n<a\n href="https://cdn.xgqfrms.xyz/index.html"\n target="_self"\n onclick="autoOpenAlink(\'https://cdn.xgqfrms.xyz/index.html\')">\n open url in the current tab page using `_self`\n</a>
Run Code Online (Sandbox Code Playgroud)\r\n_blank
const autoOpenAlink = (url = ``) => {\n window.open(url, "open testing page in a new tab page");\n}\n\n// \xe2\x9d\x8c The error is caused by a `StackOverflow` limitation\n// 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.
Run Code Online (Sandbox Code Playgroud)\r\n<a\n href="https://cdn.xgqfrms.xyz/index.html"\n target="_blank"\n onclick="autoOpenAlink(\'https://cdn.xgqfrms.xyz/index.html\')">\n open url in a new tab page using `_blank`\n</a>
Run Code Online (Sandbox Code Playgroud)\r\n\n\n根据 MDN 的文档,您只需给出新的
\nwindow
/的一个名称tab
。
https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Syntax
\n 归档时间: |
|
查看次数: |
824534 次 |
最近记录: |