Random num in cookie
by Vladimir
HTML
<script src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>
JavaScript
var body = $('body');
if(!$.cookie('rand')) {
body.append('<div>No cookie</div>');
} else {
body.append('<div>Cookie exist</div>');
}
//-----------------------------------------------------
if(!$.cookie('rand')) {
var iMax = 10, // num length
rndStr = Math.random().toString().substr(2, iMax);
while(rndStr[0] == 0) {
rndStr[0] = Math.random().toString().substr(2, 1); // first symbol is not zeo
}
$.cookie('rand', rndStr, {
expires: 36500,
path: "/"
});
}
//---------------------------------------------------------
body.append('<div>Cookie is ' + $.cookie('rand') + '</div>');