如何防止Google Chrome阻止我的弹出窗口? [英] How do I prevent Google Chrome from blocking my popup?
问题描述
在我的网站上有一个按钮,它只是用来调用一个调用 window.open
的函数,但是最近需要进行调整来执行服务器端检查在弹出窗口打开之前。
自从AJAX调用添加代码以来,浏览器会阻止弹出窗口,该窗口在成功
AJAX调用的回调。我读过浏览器可能会阻止弹出窗口,如果它没有被用户点击事件调用,所以我尝试将AJAX请求设置为 async:false
,这解决了Firefox中的问题,但谷歌浏览器仍然阻止我的弹出窗口。有什么方法可以解决这个问题吗?
我可以将服务器端检查移到在弹出窗口中打开的页面,但是我想这样做
代码:
< / p> ; a id =attackButtonhref =#> Attack Base!< / a>
< script type =text / javascript>
$(function(){
$('#attackButton')。click(function(){
$ .ajax({
url:baseurl +'/index.php ?option = com_pbbgs& format = raw& getinfo = wentame',
data:{'gameid':618},
dataType:'text',
async:false,
类型:'POST',
成功:函数(数据){
eval(数据);
if(window.gameURL){
goingameRaw();
}
}
});
返回false;
});
});
函数goingameRaw()
{
window.open(window.gameURL,'test','left = 20,top = 20,width = 1024,height = 640,工具栏= 0,可调整大小= 0,位置= 0' );
}
< / script>
示例响应主体:
window.gameURL = http://mydomain.com/index.php?option=com_pbbgs&format=raw&startgame=618&width=1024&height=640; checktutorial( 'JS' ,'攻击');
是的,弹出窗口应该是用户的直接结果行动。在Ajax回调中做他们不会有诀窍。另外,使用 async:false
很糟糕 - 在FF中,已知阻止整个浏览器。想想其他方法来做检查:
On my website there is a button that just used to call a function that calls window.open
, however, recently an adjustment was needed to do a server-side check before the popup was opened.
Ever since the code was added that does the AJAX call, browsers blocks the popup, that is opened in the success
callback of the AJAX call. I read that browsers might block the popup if it's not called by a user click event, so I tried setting the AJAX request to async: false
, which solved the problem in Firefox, but Google Chrome still keeps blocking my popup. Is there any way to get around this?
I could move the server-side check to the page that gets opened in the popup, but I'd like to do it before opening the popup, if possible.
Code:
<a id="attackButton" href="#">Attack Base!</a>
<script type="text/javascript">
$(function() {
$('#attackButton').click(function() {
$.ajax({
url: baseurl + '/index.php?option=com_pbbgs&format=raw&getinfo=goingame',
data: { 'gameid': 618 },
dataType: 'text',
async: false,
type: 'POST',
success: function(data) {
eval(data);
if (window.gameURL) {
goingameRaw();
}
}
});
return false;
});
});
function goingameRaw()
{
window.open(window.gameURL,'test','left=20,top=20,width=1024,height=640,toolbar=0,resizable=0,location=0');
}
</script>
Example response body:
window.gameURL="http://mydomain.com/index.php?option=com_pbbgs&format=raw&startgame=618&width=1024&height=640";checktutorial('js','attack');
Yes, popups should be a direct result of a user action. Doing them in ajax callback will not do the trick. Also, using async:false
is bad - in FF it is known to block the whole browser. Think of some other way to do the check:
- it could be the first thing you do in the popup
- you can open the popup on click and manipulate it later when the callback fires
- you can require the user to click again some button to trigger the popup (probably the worst solution)
- you can do it on page load
这篇关于如何防止Google Chrome阻止我的弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!