View Full Version : Cookie..?
joey23
08-22-2003, 05:48 PM
Hi,
I need to have the following but am unsure how to do it.
I have a flash intro on my website.
I want a cookie to be placed on my visitors computer when they visit the site. That way, when they return in the future, they don't need to see the same intro again.
Any ideas how to do this?
Thanks!!!
whackaxe
08-22-2003, 05:54 PM
you have the simple PHP method for cookies.
www.hotscripts.com/PHP/ look around there for tips and utorials on cookies and session handeling. or in JS, look at www.webmonkey.com in the javascript section. there is a part about JS cookies. wich are actually quite a pain to get working
The following is a cookie that runs a function in a set number of days.
The example below is for a popup window, just adapt the function dothis() to do what you want.
<SCRIPT language=JavaScript>
<!--
var expDays = 1; // number of days the cookie should last
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 getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value,expires) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function checkCount() {
var count = GetCookie('poponce');
if (count == null) {
count++;
SetCookie('poponce', count, exp);
// Action to take
dothis()
}
}
function dothis(){
var windowprops = "left=50,top=150,width=300,height=200,location=no,toolbar=no,menubar=no,scrollbars=no"; window.open("yourpage.htm", "", windowprops); // Use to load a page
}
// -->
</SCRIPT>
Insert checkCount() into your opening BODY tag.
Vladdy
08-22-2003, 07:03 PM
Just get rid of the intro.
Chances are it will cause users not to come back, so your cookie will never be used :thumbsup:
joey23
08-26-2003, 06:29 PM
Thanks for the help guys.
MR. J, thanks for the code.
One quick question;
I want it that the first time someone visits the site, they'll see a certain image (ex; imagea.gif). Then the cookie gets set, and therefore when they vist the next time, they see imageb (ex; imageb.gif).
Any idea with JavaScript how to do this?
Thanks!!
Please try the following, change the images to your own
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<SCRIPT language=JavaScript>
<!--
var expDays = 1; // number of days the cookie should last
var mycookie ="image_once" // cookie name
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 getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value,expires) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie(name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function checkCount() {
var count = GetCookie(mycookie);
if (count == null) {
count++;
SetCookie(mycookie, count, exp);
// Action to take
dothis()
}
}
function dothis(){
document.getElementById("my_image").src="pic2.jpg"
}
// -->
</SCRIPT>
</HEAD>
<BODY onload="checkCount()">
<center>
<img id="my_image" src="pic1.jpg">
<P>Use the following link to delete the cookie whilst testing then delete the link
<P><a href="#null" onclick="DeleteCookie('image_once')">Delete</a>
</center>
</BODY>
</HTML>
joey23
08-27-2003, 03:05 PM
Thanks Mr. J!!!!!!!!!!!
It works great with an image.
However, now I am trying to adapt it to work with flash and it isn't going too well. If you have any suggestions, I'd greatly appreciate them.
Here's my flash code;
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash4/cabs/swflash.cab#version=4,0,0,0" id="server2" width="780" height="135">
<param name="movie" id="my_image" value="flash/flash1.swf" src="">
<param name="quality" value="high">
<param name="bgcolor" value="#FFFFFF">
<embed name="server2" id="my_image" src="flash/flash1.swf" value="" quality="high" bgcolor="#FFFFFF" width="780" height="135" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</embed></object>
And I have this in the dothis() function;
function dothis(){
document.getElementById("my_image").src="flash/flash2.swf"
document.getElementById("my_image").value="flash/flash2.swf"
}
The cookie does get placed on the computer. However, it never changes the flash, it just uses whatever is in the code. It doesn't seem to use the function whether or not a cookie has been placed.
Any thoughts?
Thank You!
joey23
08-28-2003, 03:38 PM
Any ideas about this?
I'd really appreciate it. And thanks again for everyone's pevious help!
STDestiny
08-28-2003, 06:09 PM
Instead of setting it to an image, set it to a variable (say, flash1 and flash2)
Then put a javascript in the body of the document that checks to see which one is there and use document.write() to put out the correct one (using if-then-else)
-Andrew
You may be having problems because you have given
param name="movie"
and
embed name="server2"
the same ID, so you have 2 objects with the same ID
Please try the following
<HTML>
<HEAD>
<TITLE>colin</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF" onload="checkCount()">
<SCRIPT language=JavaScript>
<!--
var expDays = 1; // number of days the cookie should last
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 getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value,expires) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function checkCount() {
var count = GetCookie('poponce');
if (count == null) {
count++;
SetCookie('poponce', count, exp);
// Action to take
document.write("<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0\" WIDTH=550 HEIGHT=400>")
document.write("<PARAM ID=colin NAME=movie VALUE=\"movie2.swf\">")
document.write("<PARAM NAME=quality VALUE=high>")
document.write("<PARAM NAME=bgcolor VALUE=#FFFFFF>")
document.write("</OBJECT>")
}
else{
document.write("<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0\" WIDTH=550 HEIGHT=400>")
document.write("<PARAM ID=colin NAME=movie VALUE=\"movie4.swf\">")
document.write("<PARAM NAME=quality VALUE=high>")
document.write("<PARAM NAME=bgcolor VALUE=#FFFFFF>")
document.write("</OBJECT>")
}
}
// -->
</SCRIPT>
</BODY>
</HTML>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.