gpt4 book ai didi

javascript - 窗口弹出窗口阻止程序问题

转载 作者:行者123 更新时间:2023-11-30 13:02:15 24 4
gpt4 key购买 nike

我正在使用以下脚本自动弹出优惠券窗口。然而,正如预期的那样,Chrome 和其他浏览器正在阻止弹出窗口。有没有办法避免这个被弹出窗口拦截器拦截?另外,如何将它居中?

代码:

<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>

最佳答案

Is there a way to avoid this from being blocked by the pop-up blocker?

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


对于类似这样的内容,我强烈建议使用横幅栏或类似内容,而不是弹出窗口。你会在 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 的用户显示横幅。您可能希望横幅中有一些东西可以让用户关闭它,这涉及在元素(关闭按钮或类似按钮)上 Hook 用户事件。例如:

<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";
};

(还有其他方法可以连接起来,但它们稍微复杂一些。)

关于javascript - 窗口弹出窗口阻止程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16993448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com