新选项卡或窗口中的跨浏览器客户端重定向
我们需要一种客户端重定向,我们唯一的选择是:
window.location
window.open
- HTTP 301, 302, 303 响应
window.location 没有支持在新选项卡或窗口中打开目标位置。 (我认为这与 浏览上下文有关)
window.open 在 Chrome 中不起作用,并且似乎依赖于对浏览器进行的某些客户端配置,这使其成为不太有利的选择,因为我们无法控制它。
HTTP 重定向响应不会打开新选项卡或窗口,就像 window.location
一样。
有什么想法吗?
we need a kind of client-side redirection and the only options we have are:
window.location
window.open
- HTTP 301, 302, 303 responses
window.location doesn't support opening the target location in a new tab or window. (I think this is related to browsing context)
window.open doesn't work in Chrome and seems to be dependent upon some client-side configurations made to the browser, which makes it a less favorable option, since we don't have control on it.
HTTP redirect response doesn't open a new tab or window, just like window.location
.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
昨晚玩了几个小时后,今天早上我灵光一现,我刚刚在 FF3、Chrome、IE7 和 IE8 中测试了这个解决方案 - 它有点 hacky,但它在所有情况下都有效。
它是如何工作的
你需要在页面上的某处隐藏一个
,你可以在 JS 中引用它(没什么大不了的 - 只需使用
display: none
创建一个即可)。当您想要进行重定向时,请使用
document.createElement ()
创建一个新的嘿,很快,您的重定向将在新窗口/选项卡中打开!
不幸的是,您仍然受到用户如何配置浏览器来处理
target="_blank"
的影响,但它将是一个新窗口或一个新选项卡,并且它应该在任何地方都可以工作......After playing around with this for hours last night, I had a flash of inspiration this morning, and I have just tested this solution in FF3, Chrome, IE7 and IE8 - it's a bit hacky but it works in all.
How It Works
You need a hidden
<div>
on your page somewhere that you can get a reference to in JS (no big deal - just create one withdisplay: none
).When you want to do the redirect, use
document.createElement()
to create a new<form>
element, assign it'saction
attribute with the address of the page where you want to take the user, and give it atarget
attribute as appropriate (I have used_blank
in my example above). Then simply append it to your hidden<div>
and call it'ssubmit()
method.Hey presto, your redirect opens in a new window/tab!
Unfortunately you are still at the mercy of how the user configures their browser to handle
target="_blank"
but it will be either a new window or a new tab, and it should work everywhere...