Jump to content

open/close that saves itself?


tiki

Recommended Posts

Im trying to create a javascript open and close feature (you know expand or collapse). That is easily done with a simple function, but I want this to also remember if its open or closed so the next time they visit its how they wanted it, open or closed.

So far this is what I have and its not working, any help would be greatly appreciated.

[code]
// COLLAPSE CONTENT SCRIPT, BY RYAN BARR and haxed by Miles Johnson

function set(name, value, expires) {
    if (!expires) {
        expires = new Date();
    }
    document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

function fetch(name) {
    cookie_name = name + "=";
    cookie_length = document.cookie.length;
    cookie_begin = 0;
    
    while (cookie_begin < cookie_length) {
        value_begin = cookie_begin + cookie_name.length;
        
        if (document.cookie.substring(cookie_begin, value_begin) == cookie_name) {
            var value_end = document.cookie.indexOf (";", value_begin);
            
            if (value_end == -1) {
                value_end = cookie_length;
            }
            return unescape(document.cookie.substring(value_begin, value_end));
        }
        
        cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
        if (cookie_begin == 0) {
            break;
        }
    }
    return null;
}

function save(id, addcollapsed) {
    var collapsed = fetch("admin_collapse");
    var tmp = new Array();

    if (collapsed != null) {
        collapsed = collapsed.split("\n");

        for (i in collapsed) {
            if (collapsed[i] != id && collapsed[i] != "") {
                tmp[tmp.length] = collapsed[i];
            }
        }
    }

    if (addcollapsed) {
        tmp[tmp.length] = id;
    }

    expires = new Date();
    expires.setTime(expires.getTime() + (1000 * 86400 * 365));
    set("admin_collapse", tmp.join("\n"), expires);
}

function toggle(id){
    var div = document.getElementById(id);
    
    if(div.style.display == 'none') {
        save(id, false);
        div.style.display = 'block';
    } else{
        save(id, true);
        div.style.display = 'none';
    }
}[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.