I am using the below script to popup a coupon window automatically. However, Chrome and other browsers are blocking the popup, as expcected. Is there a way to avoid this from being blocked by the pop-up blocker? Also, how do I center it?
Code:
<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>
Also to center you would do something like so:
Here's a fiddle for visual aid:
http://jsfiddle.net/Maikel1234/C5rRm/1/
Yes: Call
window.open
from within the event handler of a user-generated event, such as aclick
. This is the primary criterion browsers use when deciding whether to block a popup, as it gives at least some indication the user asked for something.Rather than a pop-up, for something like this I would strongly recommend using a banner bar or similar. You'd have something like this in the markup at the top of the HTML:
And then change the last bit of that code to:
That will show the banner to users who don't have the cookie set yet. You'd probably want to have something in the banner that lets the user close it, which involves hooking user events on elements (a close button or similar). For instance:
...with styling on the
span
to make it look nice. Then:(There are other ways to hook things up, but they're slightly more complicated.)