Jump to content

Archived

This topic is now archived and is closed to further replies.

tiki

open/close that saves itself?

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]

Share this post


Link to post
Share on other sites

×

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.