user5284068
user5284068

Reputation:

Better method to load cookies for bootstrap elements

I'm working on the option for hiding the panel's bodies and keep them hidden by reloading the page. It's a bootstrap driven code and the operation $("#requests").collapse() produces an animation that I don't like to see cause having 100 of panels collapsing each time by reloading the page can be annoying. So is there some better method to do this?

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length,c.length);
        }
    }
    return "";
}

function setCookie(cname) {
    var d = new Date();
    d.setTime(d.getTime() + (365*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    if(getCookie(cname) == 1) {
        document.cookie = cname + "=" + 0 + "; " + expires;
    } else {
        document.cookie = cname + "=" + 1 + "; " + expires;
    }
}

window.onload = function applyCookies() {
    if(getCookie("hidden") == 1) {  
        $("#requests").collapse();
    } 
}

Upvotes: 0

Views: 81

Answers (2)

RPaul
RPaul

Reputation: 66

Add #request{ display:none; }

Now, you just change your function to this

window.onload = function applyCookies() {
    if(getCookie("hidden") == 0) {
         $("#requests").css("display","block");
    }
}

Upvotes: 0

user5116395
user5116395

Reputation:

  if(getCookie("hidden") == 1) {  
        $("#requests").hide();
    } 

Upvotes: 1

Related Questions