Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting a cookie so popup window opens only one time 1

Status
Not open for further replies.

JRAY

Technical User
Mar 28, 2001
13
US
I have this piece of code to open my Flash intro:

var introWindow;
introWindow=window.open("Intro2.htm","Introduction","width=550, height=390");
introWindow.moveTo(200,200);

I'm not a programmer, so I need some help with cookies. How do I get the intro window to pop up only once in a session?

Thanks!
 
Ok, hold on:

<html>
<head>

<script type=text/javascript>
var TEST_ONLY = true;// set to false to not run this in test mode

/// BEGIN COOKIE CONSTRUCT
function Cookie(document,name,hours,path,domain,secure) {
// any VAR in &quot;this&quot; that does not start with a &quot;$&quot; will
// be written into the cookie (read from also)
this.$doc = document
this.$name = name
if (hours) this.$expiration=new Date((new Date()).getTime()+hours*3600000); else this.$expiration = null
if (path) this.$path = path; else this.$path = null
if (domain) this.$domain = domain; else this.$domain = null
if (secure) this.$secure = true; else this.$secure = false
}

function CookieWrite() {
var cookieval=&quot;&quot;
for(var prop in this) {
if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') || prop == '') continue
if (cookieval != &quot;&quot;) cookieval += '&'
cookieval+=prop+&quot;:&quot;+escape(this[prop])
}
var cookie=this.$name+&quot;=&quot;+cookieval
if (this.$expiration) cookie+='; expires=' + this.$expiration.toGMTString()
if (this.$path) cookie+='; path=' + this.$path
if (this.$domain) cookie+='; domain=' + this.$domain
if (this.$secure) cookie+='; secure'
this.$doc.cookie=cookie
}

function CookieRead() {
var allcookies=this.$doc.cookie
if (allcookies==&quot;&quot;) {
return false
}
var start= allcookies.indexOf(this.$name+'=')
if (start== -1) {
return false
}
start += this.$name.length+1
var end=allcookies.indexOf(';',start)
if (end == -1) end=allcookies.length
var cookieval = allcookies.substring(start,end)
var a = cookieval.split('&')
for (var i=0;i<a.length;i++) a=a.split(':')
for (var i=0;i<a.length;i++) this[a[0]]=unescape(a[1])
return true
}

function CookieDelete() {
var cookie = this.$name+'='
if (this.$path) cookie+='; path='+this.$path
if (this.$domain) cookie+='; domain='+this.$domain
cookie+='; expires=Fri, 02-Jan-2003 00:00:00 GMT' // MAKE IT EXPIRE!
this.$doc.cookie=cookie
}

new Cookie()
Cookie.prototype.write = CookieWrite
Cookie.prototype.del = CookieDelete
Cookie.prototype.read = CookieRead
/// END COOKIE CONSTRUCT
</script>

<script>
// The following script determines if this is a first time user
// [based upon a cookie counter value]
// If it is it makes sure that the user goes to page #1 first.
// It checks to find out what page # the user should be on,
// If the user is on that page it allows the rest of the page to load,
// otherwise the user is sent to the proper page.
// If the user is on the correct page the cookie counter is incremented
// so that the next page the user can look at is the next # page.

var myCookie = new Cookie(document,&quot;PopupOrNoPopup&quot;,365); // expire in 1 year.
if (!myCookie.read() || !myCookie.counter)
myCookie.counter = 1; // first visit!
myCookie.write(); // store off the cookie!

var _loc = document.location.toString();
if (myCookie.counter>1)
document.location=&quot;download.htm&quot;
else
{
var introWindow;
introWindow=window.open(&quot;download.htm&quot;,&quot;Introduction&quot;,&quot;width=550, height=390&quot;);
introWindow.moveTo(200,200);
}


// if the logic gets this far then the user is on the correct page.
myCookie.counter++;
myCookie.write();
function clearCookie() { myCookie.del(); }
</script>


</head>
</html>

HTH,

Quasibobo Don't eat yellow snow!
 
Forgot to tell you what happens: The first time the pop-up is shown, after that the user is automaticly redirected to (in my case) download.htm. Wait....I'll change some things:


var _loc = document.location.toString();
if (myCookie.counter>1)
document.location=&quot;Intro1.htm&quot;
else
{
var introWindow;
introWindow=window.open(&quot;Intro2.htm&quot;,&quot;Introduction&quot;,&quot;width=550, height=390&quot;);
introWindow.moveTo(200,200);
};
document.location=&quot;Intro1.htm&quot;

Now both times the user is directed to Intro1.htm, only the first time the pop-up is shown! This is better.....(I think)

HTH,

Quasibobo Don't eat yellow snow!
 
Quasibobo,

I had no idea it was going to be that intensive! The script worked perfectly. Thank you so much for your help!

JRAY
 
Here's a &quot;cookiejar&quot; object that makes working with cookies very easy: Click on the &quot;too light&quot; beige link of the left that says &quot;Get TDCookieJar here&quot;. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top