简体   繁体   English

刷新页面后的Javascript变量

[英]Javascript variables after refresh page

I try to create my own collapsed sidebar, and I want to have the cookie saved for settings(collapsed or expanded bar). 我尝试创建自己的折叠边栏,并且希望保存Cookie以进行设置(折叠或展开栏)。 Here`s my code from to footer included file: 这是我的代码从到页脚包含的文件:

var cookieRightSideBar;
var $postscontainer;
var $container;
var $infocontainer;
cookieRightSideBar = document.cookie.replace(/(?:(?:^|.*;\s*)rightside_container\s*\=\s*([^;]*).*$)|^.*$/, "$1");
$postscontainer = document.getElementById('leftside');
$container = document.getElementById('rightside_container');
$infocontainer = document.getElementById('rightside');

function collapseRightside() {
    document.cookie = (cookieRightSideBar == 'collapsed') ? 'rightside_container=expanded' : 'rightside_container=collapsed';
    $postscontainer.style.width = ($postscontainer.style.width == '99%') ? '80%' : '99%';
    $container.style.display = ($container.style.display == 'none') ? 'block' : 'none';
    $infocontainer.style.width = ($infocontainer.style.width == '0%') ? '19%' : '0%';
}


if (document.getElementById('leftside')) {
    var $myheight = document.getElementById ('leftside').offsetHeight - 62;
    document.getElementById('rightside').style.maxHeight = $myheight + 'px';
}

if (cookieRightSideBar == 'expanded') {
    $container.style.display = 'block';
    $infocontainer.style.width =  "19%";
    $postscontainer.style.width = "80%";
}

Its almost worked, except couple sadly things. 它几乎起作用了,除了令人悲伤的事情。 Then after refresh a page, I click to button for expand my bar, looks like function collapseRightside not worked correctly after first click(bar collapsed if was expanded but not expanded if was collapsed ). 然后刷新页面后,我单击按钮以扩展我的栏,第一次单击后函数函数collapseRightside不能正常工作(如果展开,则栏折叠,如果折叠, 则不展开 )。 This function properly work with any stages only after second click. 仅在第二次单击后,此功能才能在任何阶段正常使用。 And one more... If I collaps bar, and expand again and then refresh page, the bar appear collapsed. 还有一个...如果我折叠栏,然后再次展开然后刷新页面,则该栏显示为折叠状态。 I understarnd what I have mistakes in logical things, but I can`t understand where? 我低估了逻辑上的错误,但是我不知道在哪里? Thanks for any help!!! 谢谢你的帮助!!!

This seems to fix your problem. 这似乎可以解决您的问题。

https://jsfiddle.net/L5unwnL9/5/ https://jsfiddle.net/L5unwnL9/5/

The problem was that you were checking for $container.style.display == 'none' while that value is null on page load. 问题是您正在检查$container.style.display == 'none'而该值在页面加载时为null。 This fixed it on both container and infocontainer. 这将其固定在容器和信息容器上。 I've also added createCookie and getCookie functions just because it was easier to use for me. 我还添加了createCookie和getCookie函数,只是因为它对我来说更易于使用。

var cookieRightSideBar;
var $postscontainer;
var $container;
var $infocontainer;
var e = document.getElementById('onclick');
e.onclick = collapseRightside;
cookieRightSideBar = getCookie("rightside_container")
$container = document.getElementById('rightside_container');
$infocontainer = document.getElementById('rightside');

function collapseRightside() {
    cookieRightSideBar = (getCookie('rightside_container') == 'collapsed') ? 'expanded' : 'collapsed';

    createCookie("rightside_container", cookieRightSideBar);


    $container.style.display = (!$container.style.display || $container.style.display == 'none') ? 'block' : 'none';

    $infocontainer.style.width = (!$infocontainer.style.width || $infocontainer.style.width == '0%') ? '19%' : '0%';
}


if (document.getElementById('leftside')) {
    var $myheight = document.getElementById('leftside').offsetHeight - 62;
    document.getElementById('rightside').style.maxHeight = $myheight + 'px';
}


if (cookieRightSideBar === 'expanded') {
    $container.style.display = 'block';
    $infocontainer.style.width = "19%";
}


function createCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM