javascript window.open从回调 [英] javascript window.open from callback

查看:859
本文介绍了javascript window.open从回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

window.open()默认情况下会打开新标签页.

window.open() called from main thread opens new tab by default.

但是,每次都在这里打开新窗口(Opera 16和Google Chrome 29)

But, here open new window every time (Opera 16 and 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>

(大声笑,这是我对在使用JavaScript的新标签(而不是新窗口)).

(lol, this is my answer for Open a URL in a new tab (and not a new window) using JavaScript).

如何在此处打开标签页(默认为浏览器)?

How I can open in the tab (by browser default) here?

推荐答案

我们遇到了同样的问题,并四处寻找答案.我们发现,在我们的情况下有效,并且提炼出的智慧如下:

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:

该问题与浏览器弹出窗口阻止程序阻止打开编程窗口有关.浏览器允许在主线程上发生的实际用户单击打开窗口.同样,如果您在主线程上调用window.open,则它将起作用,如上所述.根据在新标签中打开URL的答案(而不是新窗口)(如果您使用的是Ajax调用,并且想要成功打开该窗口,则需要设置async: false,因为它会将所有内容保留在主线程中).

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 JavaScript if 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.

我们无法像这样控制我们的Ajax调用,但是由于相同的原因,找到了另一个可行的解决方案.警告,这有点hacky,考虑到您的限制,它可能不适合您.感谢对打开的其他答案的评论使用JavaScript在新标签页(而不是新窗口)中输入URL ,您可以在调用setTimeout之前打开窗口,然后在延迟功能中对其进行更新.有两种方法可以做到这一点.在打开窗口w = window.open...并设置w.location时保留对窗口的引用,或者在该目标window.open('your-url', 'target_name')中打开的延迟功能中使用目标window.open('', 'target_name')打开该窗口,并依靠浏览器保持参考.

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 JavaScript you 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.

当然,如果用户具有在新窗口中打开链接的设置,则不会更改该设置,但这对OP来说不是问题.

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.

这篇关于javascript window.open从回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆