Utifrån din kod, antar jag att du vill spara ett lagers display-värde. Då kan vi utnyttja spangos cookiebibliotek:
/********************************************
script.webforum.nu
Cookiebibliotek skrivet av spango
<gustaf-c@dsv.su.se>
Sprid gärna detta script till nära och kära!
(men lämna kvar dessa rader)
********************************************/
// Cookieklassen
function Cookie(name, value, expires, domain, path, secure){
this.name = name;
this.value = value;
this.expires = expires;
this.domain = domain;
this.path = path;
this.secure = secure;
}
// Metoder för cookieklassen
function set(){
var cookiestr = escape(new String(this.name)) + "=" + escape(new String(this.value));
if(this.expires) cookiestr += "; Expires=" + this.expirationString();
if(this.domain) cookiestr += "; Domain=" + this.domain;
if(this.path) cookiestr += "; Path=" + this.path;
if(this.secure) cookiestr += "; Secure";
document.cookie = cookiestr;
}
function expirationString(){
var ex = this.expires;
if(typeof ex == typeof "string") return ex;
if(typeof ex == typeof 1){
var msPerDag = 1000 * 60 * 60 * 24;
ex = new Date();
ex.setTime(ex.getTime() + (msPerDag * this.expires));
}
return ex.toUTCString ? ex.toUTCString() : ex.toGMTString();
}
function setDomainToCurrent(){
this.domain = self.location.host;
}
function setPathToCurrent(){
var pth = self.location.pathname.substr(self.location.pathname.indexOf ("/"));
if(!pth) pth = "/";
else pth = pth.substring(0, pth.lastIndexOf("/") + 1);
this.path = pth;
}
// Allmäna funktioner
function getCookie(name, alt){
if(!alt) alt = "";
name = escape(name) + "=";
if(document.cookie.indexOf(name) == -1) return alt;
var kaka = document.cookie.substr(document.cookie.lastIndexOf(name));
kaka = kaka.substr(name.length);
var slut = (kaka.indexOf(';') == -1) ? kaka.length : kaka.indexOf(';');
kaka = kaka.substring(0, slut);
return unescape(kaka);
}
function deleteCookie(name, path){
var cookiestr = escape(name) + "=_; Expires=Thursday, 01-Jan-1970 00:00:00 GMT";
if(path) cookiestr += "; Path=" + path;
document.cookie = cookiestr;
}
function getAllCookieNames(){
var ac = document.cookie.split(" ");
var allCookies = new Array();
for(var i = 0; i < ac.length; i++){
var tmp = ac[ i].split("=");
allCookies[ i] = unescape(tmp[0]);
}
return allCookies;
}
// Lägg till metoder
Cookie.prototype.set = set;
Cookie.prototype.expirationString = expirationString;
Cookie.prototype.setDomainToCurrent = setDomainToCurrent;
Cookie.prototype.setPathToCurrent = setPathToCurrent;
// Statiska metoder
Cookie.get = getCookie;
Cookie.remove = deleteCookie;
Cookie.getAllNames = getAllCookieNames;
function expandCollapse(){
var args = arguments,
len = args.length;
for (var i = 0; i < len; ++i){
var elm = document.getElementById(args[i]);
elm.style.display = (elm.style.display == "none") ? "block" : "none";
if (Cookie.get(args[i])){
Cookie.remove(args[i]);
}
new Cookie(args[i], elm.style.display, 365, false, "/").set();
}
}
window.onload = function(){
var c = Cookie.getAllNames(),
i = c.length;
while (i--){
document.getElementById(c[i]).style.display = Cookie.get(c[i]);
}
}
Med reservation för eventuella fel