Created
November 6, 2013 04:52
-
-
Save KrotovRoman/7331103 to your computer and use it in GitHub Desktop.
Как записать данные в cookies
Работа с куками.
Работа с печеньками.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // установить куки | |
| function setCookie( name, value, expires, path, domain, secure ) { | |
| document.cookie = name + "=" + escape(value) + | |
| ((expires) ? "; expires=" + expires.toGMTString(): "") + | |
| ((path) ? "; path=" + path : "") + | |
| ((domain) ? "; domain=" + domain : "") + | |
| ((secure) ? "; secure" : ""); | |
| } | |
| // использование | |
| setCookie( 'option', id_option, new Date( (new Date()).getTime() + ( 60 * 60 * 24*24 ) ), '/' ); | |
| // получить куки | |
| function getCookie(cname) | |
| { | |
| var name = cname + "="; | |
| var ca = document.cookie.split(';'); | |
| for(var i=0; i<ca.length; i++) | |
| { | |
| var c = ca[i].trim(); | |
| if (c.indexOf(name)==0) return c.substring(name.length,c.length); | |
| } | |
| return ""; | |
| } | |
| //использование | |
| getCookie("test"); | |
| //если русские буквы в куках, то нужно делать декодировку полученных данных: | |
| unescape(getCookie("test")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment