-->

窗口弹出窗口拦截器问题(window popup blocker issue)

2019-09-28 03:27发布

我使用下面的脚本来自动弹出一个窗口券。 然而,Chrome和其他浏览器拦截弹出窗口,为expcected。 是否有办法避免这种情况的被阻止通过弹出窗口阻止程序? 此外,我怎么中心呢?

码:

<SCRIPT LANGUAGE="JavaScript">
<!--
function GetCookie(name) {
  var arg=name+"=";
  var alen=arg.length;
  var clen=document.cookie.length;
  var i=0;
  while (i<clen) {
    var j=i+alen;
    if (document.cookie.substring(i,j)==arg)
      return "here";
    i=document.cookie.indexOf(" ",i)+1;
    if (i==0) break;
  }
  return null;
}
var visit=GetCookie("COOKIE1");
if (visit==null){
   var expire=new Date();
    window.name = "thiswin";
    newwin=open("popup.html", "dispwin",  
    "width=730,height=424,scrollbars=no,menubar=no");
   document.cookie="COOKIE1=here; expires=Thu 01-01-2004 00:00:00 GMT;";
}
// -->
</SCRIPT>

Answer 1:

是否有办法避免这种情况的被阻止通过弹出窗口阻止程序?

是:调用window.open从用户生成的事件,对事件处理程序中,如click 。 这是首要标准浏览器决定是否阻止弹出窗口,因为它提供了至少一些指示用户要求的东西时使用。


而不是弹出,这样的事情我会强烈建议使用横幅条或类似。 你有这样的事情在该HTML的顶部标记:

<div id="first-time-banner" style="display: none">text here</div>

然后更改该代码的最后一位:

if (visit==null){
   var expire=new Date();
   document.getElementById("first-time-banner").style.display = "block";
   document.cookie="COOKIE1=here; expires=Thu 01-01-2004 00:00:00 GMT;";
}

这将显示横幅没有设置cookie但谁的用户。 你可能会希望有一个让用户关闭它的旗帜,其中包括钩住元素的用户事件(关闭按钮或类似)的东西。 例如:

<div id="first-time-banner" style="display: none">
    text here
    <span id="first-time-banner-close">X</span>
</div>

......与造型span ,以使它看起来不错。 然后:

document.getElementById("first-time-banner-close").onclick = function() {
    document.getElementById("first-time-banner").style.display = "none";
};

(还有其他的方法来挂钩的事情,但他们稍微复杂的是。)



Answer 2:

此外,以中心,你会做一些像这样:

<div id="PopUpFad" style="left: 514.5px; opacity: 0.9999899999999999; position: absolute;
 top: 50%; 
 left: 50%; 
 margin-top: -195px; 
 margin-left: -314px;" class="show"><div class="PopUpFadClose"><a href="#" onclick="PopUpFadCloseX()"><img src="http://livefitrevolution.org/wp-content/plugins/cool-fade-popup/close.jpg"></a></div><div><img src="http://livefitrevolution.org/wp-content/uploads/motivation-difference.jpg"></div></div>

下面是对视觉辅助提琴:

http://jsfiddle.net/Maikel1234/C5rRm/1/



文章来源: window popup blocker issue