Click to See Complete Forum and Search --> : Pop-up Cookie???
I'm trying to create a piece of java-script that I can use to detect a visitors browser type and version when they land on a page within my site, and then display a pop-up window with a message based on the visitors browser type/version. The script also needs to write a cookie so the pop-up window does not display when a visitor views additional pages within the site.
This is what I have so far. I'm just missing the cookie part now. This code is an include file that is refrenced from every page on my site.
<script language="JavaScript">
<!--
var br = navigator.appName
var brv = navigator.appVersion
if(br == "Netscape")
{
if(brv<"5.0")
{
newwindow=window.open("","new1","width=500,height=175");
newwindow.document.write("<font face='arial, helvetica, ms sans serif' size='2'><b>Please note:</b><br><br>Our site is best viewed using Netscape 5.0 or higher browsers. It appears you are using Netscape "+brv+". Please visit <a href='http://www.netscape.com' onClick='window.opener.location=this.href;window.close(); return false'>Netscape</a> for a free copy of a newer version of Netscape.<br><br>If you believe this message is the result of a system error, please contact our <a href='mailto:webmaster@yadayada.com'>webmaster</a>.<p align='right'><a href='javascript:window.close()'>close window</a></p></font>");
newwindow.document.close();
}
else
{
document.write()
}
}
// -->
</script>
One thing to keep in mind...Even Netscape 7 comes in as a version 5 browser using navigator.appVersion. http://javascript.internet.com/user-details/browser-version2.html shows how to get the accurate browser verions. Not sure if it matters to you, since you are looking for a pre-5 browser, but I thought I'd let you know. As far as the cookie goes, you could try looking at the following link and see if that is what you are looking for. http://javascript.internet.com/cookies/only-popup-once.html
hfraser
12-09-2002, 05:50 PM
here try this
a littlemore elegant and reusable!
<script language="JavaScript" type="text/JavaScript">
function setCookieNav(){
var cookieDies = new Date();
// i set the cookie to expire in a week
cookieDies.setTime(cookieDies.getTime() + 7*24*60*60*1000);
// set the content of the cookie
document.cookie = "visit=true.;expires=" + cookieDies.toGMTString();
}
function getCookieNav(){
var MyCookie = new String(document.cookie);
var myCookieHeader = "visit=";
var MyCookieStart = MyCookie.indexOf(myCookieHeader);
// extract data
// if the cookie does not exist well set one and build your window!
if( MyCookieStart = -1){
setCookieNav();
buildNavInfo();
};
}
function buildNavInfo(){
var br = navigator.appName ;
var brv = navigator.appVersion ;
if(br == "Netscape")
{
if(brv<"5.0")
{
newwindow=window.open("","new1","width=500,height=175");
newwindow.document.write("<font face='arial, helvetica, ms sans serif' size='2'><b>Please note:</b><br><br>Our site is best viewed using Netscape 5.0 or higher browsers. It appears you are using Netscape "+brv+". Please visit <a href='http://www.netscape.com' onClick='window.opener.location=this.href;window.close(); return false'>Netscape</a> for a free copy of a newer version of Netscape.<br><br>If you believe this message is the result of a system error, please contact our <a href='mailto:webmaster@yadayada.com'>webmaster</a>.<p align='right'><a href='javascript:window.close()'>close window</a></p></font>");
newwindow.document.close();
}
}
}
</script>
<body onLoad="getCookieNav();">
As a spin off of this, I think I may need a script to do very close to this same thing. What I would want is the user to have to check a checkbox before it won't pop the window up. Know how to do this?
hfraser
12-09-2002, 06:21 PM
i really dont understand what you mean could you clarify a bit!
A million thanks to you hfraser! Your code did it. Thanks again.
Sure. What I am trying to do is pop up a window to let people know that they need to download the Macromedia Flash 6 player. I don't need to detect if they have the Flash player..I want the window to pop up either way. What I need is a script that will write a cookie "IF" they have checked a checkbox that says something like "For future visits, bypass this message" and then, on future visits, if the cookie was written bypass the popup window. If you need me to explain further, let me know.
hfraser
12-10-2002, 12:51 AM
ok i tweaked it a little bit now the get and set cookie functions are reusable for further use
the get cookie returns you false or the value of the cookie
the set cookie functions enables you to actually set tons of cookie if you want too with it! (the expiry is set in days!)
and the checkFlashPlugin function returns true or fals to do what ever you want out of it ! hope it helps ... now you even have bits and peices of reusable code!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function checkFlashPlugin() {
var gtoFlash=false;
with (navigator) if (appName.indexOf('Microsoft')==-1 || (plugins && plugins.length)) {//check netscape
gotFlash=(plugins && plugins['Shockwave Flash']);
}
else if (appVersion.indexOf('3.1')==-1) {//check the rest
if (window.MM_flash!=null){
gotFlash=window.MM_flash;}
else {gotFlash=true;}
}
//build you window how you want it
return gotFlash;
}
function setMyCookie(myCookieName, myCookieValue, myCookieTime){
// cookie time gotta be in days
var cookieDies = new Date();
// i set the cookie to expire in a week
cookieDies.setTime(cookieDies.getTime() + myCookieTime*24*60*60*1000);
// set the content of the cookie
document.cookie = myCookieName+"="+myCookieValue+";expires=" + cookieDies.toGMTString();
}
function getMyCookie(whatCookie){
var myCookie = new String(document.cookie);
var myCookieHeader = whatCookie+"=";
var myCookieStart = myCookie.indexOf(myCookieHeader);
var myCookieEnd = myCookie.indexOf(';',myCookieStart);
var hans = new String("hanshans");
if (myCookieEnd == -1){
myCookieEnd = myCookie.length;
}
// extract data
// if the cookie does not exist well set one and build your window!
if( myCookie.indexOf(myCookieHeader) != -1){
myCookieStart = myCookieHeader.length;
return myCookie.substring(myCookieStart, myCookieEnd);
}
else{
return false;}
}
function checkCookieFlash(){
var gotFlash = checkFlashPlugin();
var gotCookie = eval(getMyCookie('flashCookie'));
//set the state of the checkbox to true
document.flashForm.flash.status = gotCookie;
if (!gotCookie) {
//build your window flash window
buildwindow(gotFlash);}
}
function setFlashCookie(){
if(document.flashForm.flash.status){
//if the box is checked
setMyCookie('flashCookie', 'true', 365);}
else{
//otherwise if the box is unchecked
setMyCookie('flashCookie', 'false', 365);}
}
function buildwindow(gotFlash){
// build it the way you want your window to be
newwindow=window.open("","flashWindow","width=200,height=100");
// if we got flash
if(gotFlash){
// build your window with flash or what ever info you want to give them
newwindow.document.write('i got flash');}
else{
// build your window to get flash
newwindow.document.write('i should get flash');
}
newwindow.document.close();
}
//-->
</script>
</head>
<body onLoad="checkCookieFlash()">
<form name="flashForm" action="" method="get">
click me and i will set you cookie<input name="flash" type="checkbox" value="" onClick="setFlashCookie()">
</form>
</body>
</html>
I think that is close to what I need. I should be able to modify it to do what I want. I didn't want it to detect for Flash. I just wanted to pop up a window every time they visit the site, unless they have checked the checkbox saying not to. So, I should just have to remove the stuff about Flash, right? One other quick question...Does anyone know of a website or tutorial that explains cookies? I'm not the least bit familiar with how they work, or how to use them...
Okay, I got the script working. I simplified it quite a bit (because I didn't need to detect Flash). Thanks for your help.
Is there a simple way to set the cookie to expire per browser session?