iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-29-2005, 05:57 AM
WebProWorld New Member
 
Join Date: Feb 2005
Location: Exmouth, UK
Posts: 12
crewdy RepRank 0
Default Javascript Cookie Code Needed

Hi all,

I am using a piece of Javascript code to toggle the viewing of divisions of my website, so a division can be "opened" and "closed" at user disgresion. You can see a live example of what I mean here: www.crewdy.co.uk/layout.html

The problem that I have is that when the page is refreshed or a link is clicked, the page reloads and the divisions are re-opened. I am looking for a piece of code which will enable a cookie, telling the browser to keep that division open until the user next clicks the "open/close" link.

If anybody knows of where I can find a piece of code, or if anybody could help to write a piece of code to go with this, I would be greatly appreciative. May be safe to note that my JavaScript knowledge is zero-to-none; the code I am currently using is below.

Thanks,
Pete

Code:
<!--
function toggleLayer(whichLayer)
{
 if (document.getElementById)
 {
  var style2 = document.getElementById(whichLayer).style;
  style2.display = style2.display? "":"none";
 }
}
-->
Reply With Quote
  #2 (permalink)  
Old 06-30-2005, 05:12 PM
WebProWorld New Member
 
Join Date: Jun 2005
Location: Birmingham, Alabama
Posts: 2
aspnetapps.com RepRank 0
Default

Here's something I've used to save a user's background color preference:

<script language="JavaScript">

var expDays = 90;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function color(){
var favColor = GetCookie('color');
if (favColor == null) {
favColor = prompt("What is your favorite background color?");
SetCookie('color', favColor, exp);
}
document.bgColor=favColor;
return favColor;
}
function set(){
favColor = prompt("What is your favorite background color?");
SetCookie ('color', favColor, exp);
document.location="fgd.htm"
}
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
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) {
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();
}

</script>
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:56 PM.



Search Engine Optimization by vBSEO 3.3.0