...

View Full Version : Help with session cookie for Lightbox



javanewbie7
11-16-2010, 06:34 PM
I need a session cookie that way only my lightbox only pops up one time per visit. I have no idea how to do this. Can anyone help? I've searched for session cookies and they only confuse me.

javanewbie7
11-16-2010, 06:50 PM
Let me add, if someone will write a session cookie for me, I will pay $25 for it. Anyone?

Philip M
11-16-2010, 07:28 PM
Here you are:-


<html>
<head>

<script type = "text/javascript">

function setCookie(isName,isVal) {
document.cookie = isName + "=" + isVal + ";expires=" + "path=/";
}

function getCookie(isName){
cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1) {return false}
endSlice = cookieStr.indexOf(";",startSlice+1);
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}

function dispCookie(isName) {
nValue = getCookie(isName);
if (nValue) {
alert ("Cookie Found"); // so do nothing
}

else {
alert ("The Cookie was not found");

//so add the code hereto cause Lightbox to appear

}
}

</script>

</head>

<body>
<input type = "button" value = 'Set Cookie' onclick = "setCookie('myCookie','myCookieValue')">
<br><br>
<input type = "button" value = 'Read Cookie' onclick = "dispCookie('myCookie')">

</body>
</html>

Please send your $25 to a medical charity of your choice.


"The louder he talked of his honour, the faster we counted our spoons." - Samuel Johnson

javanewbie7
11-16-2010, 07:43 PM
Awesome. Thanks so much!

A couple of questions:

1. Do I change anything? For example, "IsName" do I add something there?
2. Is there a site that teaches about cookies. I self-taught HTML and CSS, and am currently learning java script. But cookies itself confuse the heck out of me.

Thanks again. I will donate $25 to the American Cancer Society.

Philip M
11-16-2010, 07:54 PM
Awesome. Thanks so much!

A couple of questions:

1. Do I change anything? For example, "IsName" do I add something there?




NO! All you have to do is add the code to invoke Lightbox at the point I indicated, that is if the cookie is not found. Obviously you will want to remove the alerts (just for testing), remove the buttons (again just for testing) and change the body tag to:-


<body onload = "dispCookie('myCookie'); setCookie('myCookie','myCookieValue')">

In other words, onload first test to see if the cookie exists, if no cookie is found load Lightbox, if cookie is found do nothing, and then write a new cookie (whether or not it already exists).

The name of the cookie is the literal string myCookie and its value is the literal string myCookieValue. You can change those if you want of course, but I see no reason to do so.

IsName is not the same as isName. Javascript is case sensitive.



2. Is there a site that teaches about cookies. I self-taught HTML and CSS, and am currently learning java script. But cookies itself confuse the heck out of me.


Try w3schools.



Thanks again. I will donate $25 to the American Cancer Society.

Thank you for that! :)

javanewbie7
11-16-2010, 09:43 PM
One final question. (and please excuse my ignorance as I really don't know anything about cookies), why do I need buttons that say "set cookie" and "read cookie". Are they necessary? Thanks

Philip M
11-17-2010, 09:16 AM
One final question. (and please excuse my ignorance as I really don't know anything about cookies), why do I need buttons that say "set cookie" and "read cookie". Are they necessary? Thanks

You do not need the buttons. There are there purely for testing purposes and help you understand what is happening.

I have already said in Post #5:-

Obviously you will want to remove the alerts (just for testing), remove the buttons (again just for testing) and change the body tag to


<body onload = "dispCookie('myCookie'); setCookie('myCookie','myCookieValue')">

In other words, once you are satisfied that it is working, delete the alerts, remove the buttons, and change the body tag so that the cookie is set and read automatically on load (and not with the buttons). OK?

javanewbie7
11-17-2010, 03:41 PM
Yeah, sorry, I saw where you said to remove the alerts, but I skipped over the button part. Thanks so much for your help!

Freshmeat
04-16-2013, 01:44 AM
I follow this Post! I'm equally grateful. I big thanks to Philip M and Newbie for asking the question.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum