javascript window.open 从回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19026162/
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
javascript window.open from callback
提问by zardoz
window.open()
called from main thread opens new tab by default.
window.open()
默认情况下,从主线程调用会打开新选项卡。
But, here open new window every time (Opera 16 and Google Chrome 29)
但是,这里每次都打开新窗口(Opera 16 和 Google Chrome 29)
<input type="button" value="Open" onclick="cb1()">
<script type="text/javascript">
function cb1() {
setTimeout(wo, 1000); //simple async
}
function wo()
{
var a = window.open("http://google.com", "w2");
a.focus();
}
</script>
(lol, this is my answer for Open a URL in a new tab (and not a new window) using JavaScript).
(大声笑,这是我使用 JavaScript 在新选项卡(而不是新窗口)中打开 URL 的答案 )。
How I can open in the tab (by browser default) here?
我如何在选项卡中打开(默认浏览器)?
回答by Yogh
We ran across this same problem and hunted around SO for an answer. What we found works in our circumstances and the distilled wisdom is as follows:
我们遇到了同样的问题并四处寻找答案。我们发现在我们的情况下有效的方法和提炼的智慧如下:
The problem is related to browser pop-up blockers preventing programmatic window opens. Browsers allow window opens from actual user clicks which occur on the main thread. Similarly, if you call window.open on the main thread it will work, as noted above. According to this answer on Open a URL in a new tab (and not a new window) using JavaScriptif you are using an Ajax call and want to open the window on success you need to set async: false
which works because that will keep everything on the main thread.
该问题与阻止程序化窗口打开的浏览器弹出窗口阻止程序有关。浏览器允许从主线程上发生的实际用户点击打开窗口。同样,如果您在主线程上调用 window.open 它将起作用,如上所述。如果您使用 Ajax 调用并希望在成功时打开窗口,则根据使用 JavaScript 在新选项卡(而不是新窗口)中打开 URL上的这个答案,您需要设置async: false
哪个有效,因为这将使所有内容都保留在主页面上线。
We couldn't control our Ajax call like that, but found another solution that works because of the same reasons. Warning, it is a bit hacky and may not be appropriate for you given your constraints. Courtesy of a comment on a different answer on Open a URL in a new tab (and not a new window) using JavaScriptyou open the window before calling setTimeout
and then update it in the delayed function. There are a couple of ways of doing this. Either keep a reference to the window when you open it, w = window.open...
and set w.location
or open with a target, window.open('', 'target_name')
, in the delayed function open in that target, window.open('your-url', 'target_name')
, and rely on the browser keeping the reference.
我们无法像那样控制我们的 Ajax 调用,但由于相同的原因找到了另一个有效的解决方案。警告,它有点老套,鉴于您的限制,它可能不适合您。感谢对使用 JavaScript 在新选项卡(而不是新窗口)中打开 URL的不同答案的评论,您在调用之前打开窗口setTimeout
,然后在延迟函数中更新它。有几种方法可以做到这一点。在打开窗口时保留对窗口的引用,w = window.open...
并在该目标中打开的延迟函数中设置w.location
或使用目标window.open('', 'target_name')
打开window.open('your-url', 'target_name')
,并依赖浏览器保持引用。
Of course, if the user has their settings to open links in a new window this isn't going to change that, but that wasn't a problem for the OP.
当然,如果用户有在新窗口中打开链接的设置,这不会改变这一点,但这对 OP 来说不是问题。
回答by Daxxy
Like the other posts mentions the best way to do this is to first open the window and then set its location after the callback or asynchronous function
就像其他帖子提到的那样,最好的方法是先打开窗口,然后在回调或异步函数之后设置其位置
<input type="button" value="Open" onclick="cb1()">
<script type="text/javascript">
function cb1() {
var w = window.open('', 'w2');
setTimeout(function () {
wo(w);
}, 1000); //simple async
}
function wo(w)
{
w.location = "http://google.com";
w.focus();
}
</script>
Alternatively if you are using async await you will also have the same problem. The same solution still applies.
或者,如果您使用 async await,您也会遇到同样的问题。相同的解决方案仍然适用。
public async openWindow(): Promise<void> {
const w = window.open('', '_blank');
const url = await getUrlAsync();
w.location = url;
}
A further enhancement is to open the window on an initial page that provides some quick feedback either by loading a url or writing some html to that page
进一步的增强是在初始页面上打开窗口,通过加载 url 或向该页面写入一些 html 来提供一些快速反馈
public async openWindow(): Promise<void> {
const w = window.open('', '_blank');
w.document.write("<html><head></head><body>Please wait while we redirect you</body></html>");
w.document.close();
const url = await getUrlAsync();
w.location = url;
}
This will prevent a user looking at a blank tab/window for however long it takes to resolve your URL.
这将防止用户在解析您的 URL 所需的时间里查看空白选项卡/窗口。
回答by burktelefon
If a new window is opened as a new tab, or a new instance, depends on the user-settings.
如果新窗口作为新选项卡或新实例打开,则取决于用户设置。