...

View Full Version : Cookie and Java Help Needed



javanewbie7
11-19-2010, 04:45 AM
OK, so a couple of days ago, someone on this forum was kind enough to write me a session cookie that way a lightbox would only pop up once per session. I've spent the last couple of days trying to combine to two to get them to work(they work separately.)

Here's the cookie script:


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



Here's my lightbox code:


<script type="text/javascript" src="./javascript/jquery/fancybox/jquery.mousewheel-3.0.4.pack.js"></script><script type="text/javascript" src="./javascript/jquery/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link media="screen" href="./themes/KAC/jquery.fancybox-1.3.4.css" type="text/css" rel="stylesheet" /> <script type="text/javascript">
$(document).ready(function() {
$("a#onload").fancybox({
'frameHeight': 390,
'frameWidth': 640,
'hideOnContentClick': false,
'easingIn': 2000
}).trigger('click');
});
}
}

</script></script></p>
<div style="display: none;" id="lightbox_content">
<div id="data"><img height="333px" width="700px" src=images/filecabinet/folder50/lightbox.jpg" title="Lightbox" alt="Double Your Donation: November 17-December 17" /></a>
<h1>Support your local station today!</h1>
</div>
</div>
<p></p>
<p><!-- PLEDGE LIGHTBOX CODE ENDS HERE --></p>

Sorry that I just don't "get it". But any help would be much appreciated. Thanksf

Old Pedant
11-19-2010, 08:08 AM
For what it's worth, NONE of that is JAVA code. It's all JavaSCRIPT.

Yes, you are posting in the right forum. But don't call it Java. It's not. That's an entirely different language.

Untested, but I think this will do it:


<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 null;}
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;
}

$(document).ready(function() {
if ( getCookie('myCookie') != null ) return; // no light box!
setCookie('myCookie', 'ANY VALUE'); // for next time
$("a#onload").fancybox({
'frameHeight': 390,
'frameWidth': 640,
'hideOnContentClick': false,
'easingIn': 2000
}).trigger('click');
});
}
}
</script>

And forget about all the other stuff related to cookies.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum