C
C
Chvalov2015-11-07 05:14:38
PHP
Chvalov, 2015-11-07 05:14:38

Show/Hide cookie block, problem with spaces?

There is a script like this:

<script>
function _setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
 
function show_hide(num_id) {
var divstyle = new String();
    divstyle = document.getElementById(num_id).style.display;
        if (divstyle.toLowerCase() == "none" || divstyle == "") {
            document.getElementById(num_id).style.display="block";
            var date = new Date( new Date().getTime() + 1000*60*60*24*365); // ставим куку на 365 дней
            _setCookie(num_id, escape("block"), date.toUTCString(), "/");
        } else {
            document.getElementById(num_id).style.display="none";
            var date = new Date( new Date().getTime() + 1000*60*60*24*365); // ставим куку на 365 дней
            _setCookie(num_id, escape("none"), date.toUTCString(), "/");
        }
}
</script>
 
<div onclick="show_hide('block_1');">click</div>
<div id="block_1" style="display:<?=isset($_COOKIE['block_1'])? $_COOKIE['block_1'] : 'none'; ?>">content 1 по умолчанию скрыт</div>
<br />
<div onclick="show_hide('block_2');">click</div>
<div id="block_2" style="display:<?=isset($_COOKIE['block_2'])? $_COOKIE['block_2'] : 'none'; ?>">content 2 по умолчанию скрыт</div>
<br />
<div onclick="show_hide('block_3');">click</div>
<div id="block_3" style="display:<?=isset($_COOKIE['block_3'])? $_COOKIE['block_3'] : 'block'; ?>">content 3 по умолчанию показан</div>
<br />
<div onclick="show_hide('block_4');">click</div>
<div id="block_4" style="display:<?=isset($_COOKIE['block_4'])? $_COOKIE['block_4'] : 'block'; ?>">content 4 по умолчанию показан</div>


I started to redo it to fit my needs in order not only to hide the blocks, but also to change the icon on the button to return the block back, but I ran into two problems:
1) I still don’t understand how to transfer the class normally, and not the style, it just returns once and that is not true: glyphicon %20 glyphicon-minus
Code piece:
<span id="span_pm" class="<?=isset($_COOKIE['span_pm'])? $_COOKIE['span_pm'] : 'glyphicon glyphicon-minus'; ?>" ></span>

2) I'm not sure if I'm doing it right to change the class, because even if you write the name of the class without a space, it returns once, and when the button is pressed, the class itself does not change, but the block is hidden

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2015-11-07
@Chvalov

%20 is an encoded space. Appears due to the use of escape().
Where is it returned? What do you have in mind?
The class doesn't change. This script does not even attempt to change any class.

A
Alexey, 2015-11-07
@alex-saratov

I try not to pollute cookies. Therefore, I use one variable (an array of settings).
I 'll convert its values ​​for saving and
unpack it like this
Advantage - you can access it once, when initializing scripts. Further accesses to the array in memory.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question