...

View Full Version : javascript popup - cookie and center?



dommy
09-04-2006, 02:18 PM
Hi,

I've found plenty of references to js popup scripts that either center the popup to the screen no matter the resolution, or use a cookie to show the popup only once in x days...but I just can't seem to figure out how I could incorporate the two? If anyone has any ideas I would be very grateful!

What I need,

A srcipt that opens a popup in the center of the screen and places a cookie so that on revisiting the page, the popup no longer appears.

Cheers

dommy
09-05-2006, 02:13 PM
Come on guys please help out!

To get things started, here's the code I'm currently using...

COOKIE SCCRIPT

<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("welcome.asp", "dispwin",
"width=360,height=288,scrollbars=no,menubar=no");
document.cookie="COOKIE1=here; expires=Thu 01-01-2008 00:00:00 GMT;";
}
// -->
</SCRIPT>

This sets my cookie and it works great! Now I need to find out how to incorporate a center popup script to work with this...

I found this, but am unsure how to incorporate the two as this 'center popup script' requires that the body onload event be present, and that adds the popup link which is a bad thing as its already incorporated in the code above?? i.e. both scripts call the popup, and thus dont work together.

CENTER POPUP SCRIPT

<SCRIPT LANGUAGE="JavaScript">
<!--
function popup( filename, params, width, height, resizable, scrollbars, windowName, centered )
{
if ( width == 0 ) width = screen.availWidth - 20;
if ( height == 0 ) height = screen.availHeight - 20;
if ( centered == true ) {
topX = (screen.availWidth/2)-(width/2);
topY = (screen.availHeight/2)-(height/2);
} else {
topX = 20;
topY = 20;
}
if ( params != "" ) params = "?" + params;
window.open( filename + params, windowName, "resizable=" + resizable + ",scrollbars=" + scrollbars + ",top=" + topY + ", left=" + topX + ",width=" + width + ",height=" + height);
}

PopUp = function( width, height )
{
this.page = "";
this.params = "";
this.width = width;
this.height = height;
this.resizable = "yes";
this.scrollbars = "auto";
this.windowName = "win" + new Date().getMilliseconds();
this.centered = true;
}

PopUp.prototype.getPageLink = function()
{
return this.page + (this.params != "" ? "?" + this.params : "");
}
</head>
<body OnLoad="javascript:popup('welcome.asp','',360,288,'no','no','popup',true)">
// -->
</SCRIPT>[/


Hope someone can help!
:thumbsup:

Mr J
09-05-2006, 04:54 PM
Give this a try


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

winWidth=360
winHeight=288

winX = (screen.availWidth-winWidth)/2;
winY = (screen.availHeight-winHeight)/2;

newwin=open("welcome.asp", "dispwin","left="+winX+",top="+winY+",width="+winWidth+",height="+winHeight+",scrollbars=no,menubar=no");

document.cookie="COOKIE1=here; expires=Thu 01-01-2008 00:00:00 GMT;";
}
// -->
</SCRIPT>

dommy
09-05-2006, 11:23 PM
You da man! No really thank you for your time!

I'll look through it and learn a thing or 2.

Cheers:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum