// 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.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).
13 回答
为了确保在同一选项卡中打开链接,您应该使用
window.location.replace()
请参阅以下示例:
资料来源:http://www.w3schools.com/jsref/met_loc_replace.asp
您可以在不指定网址的情况下将其转到同一页面:
如果你的页面在“frame”中,那么“Window.open('logout.aspx','_ self')”
将被重定向到同一帧内 . 所以通过使用
我们可以将页面加载为新请求 .
您需要使用name属性:
Edit :Url应该附加协议 . 没有它试图打开相对网址 . 在Chrome 59,Firefox 54和IE 11中测试过 .
用这个:
其中一个最突出的JavaScript功能是即时启动onclick处理程序 . 我发现以下机制比使用
location.href=''
或location.reload()
或window.open
更可靠:上面的代码也有助于打开新的选项卡/窗口并绕过所有弹出窗口拦截器!例如 .
打开另一个网址,例如点击链接
你必须使用
window.open
吗?怎么用window.location="http://example.com"
?window.open(url, wndname, params)
,它有三个参数 . 如果你不想在同一个窗口中打开它,只需设置一个不同的wndname . 如 :这是关于
window.open()
的详细信息,您可以信赖它!https://developer.mozilla.org/en/DOM/window.open
试试~~
使用html 5,您可以使用history API .
然后在下一页上,您可以像这样访问状态对象
这很容易 . 打开第一个窗口
window.open(url, <tabNmae>)
示例:
window.open("abc.com",'myTab')
并为下一个window.open,使用 same tab name 而不是
_self
,_parent
等 .正是这样
window.open("www.youraddress.com","_self")