Job with Cookies on JavaScript
Creation cookies in JavaScript
JavaScript supports the built - in object with a name document.cookie for job with kukisam. This object stores{keeps} all kukisy, accessible to page from which the script is started.
If to insert value in document.cookie it will be created kukis:
<SCRIPT LANGUAGE = "JavaScript">
document.cookie = " foo=bar; path =/; expires=Mon, 01-Jan-2001 00:00:00 GMT ";
</SCRIPT>
For job with kukisami it is possible to use functions SetCookie:
<SCRIPT LANGUAGE = "JavaScript">
function setCookie (name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape (value) +
((expires)? "; expires = " + expires: " ") +
((path)? "; path = " + path: " ") +
((domain)? "; domain = " + domain: " ") +
((secure)? "; secure ": " ");
}
</SCRIPT>
Values name and value are obligatory, and the others are not obligatory. The example of use of this function is below resulted:
<SCRIPT LANGUAGE = "JavaScript">
setCookie ("foo", "bar", " Mon, 01-Jan-2001 00:00:00 GMT ", "/");
</SCRIPT>
Function SetCookie is rather universal, when it is necessary to create much kukisov or when it is required to establish parameters hurriedly.
Reception of value cookies in JavaScript
For reception of value kukisov in JavaScript, it is possible to use document.cookie. Usually, document.cookie has a line of the following format:
foo=bar; this=that; somename=somevalue;.....
This line contains pairs imja=znachenie, divided{shared} by a semicolon. Function getCookie () which allows to carry out analysis of parameters of this line is below resulted:
function getCookie (name) {
var cookie = " " + document.cookie;
var search = " " + name + "=";
var setStr = null;
var offset = 0;
var end = 0;
if (cookie.length> 0) {
offset = cookie.indexOf (search);
if (offset! =-1) {
offset + = search.length;
end = cookie.indexOf (";", offset)
if (end ==-1) {
end = cookie.length;
}
setStr = unescape (cookie.substring (offset, end));
}
}
return (setStr);
}
Example of use of this function:
myVar = GetCookie ("foo");
In this case value of a variable myVar will be equal bar.

|