How to set cookies through javascript?

by barkkathulla 2013-11-30 13:00:59

To set cookie value using javascript:


<script language="JavaScript">
// Function to get form element
function storeemail(element) {
var value = getCookie(element.name);
if (value != null) element.value = value;
}

// Function to store form element
function passemail(element) {
setCookie(element.name, element.value, exp);
}
//get cookie function
function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}

// to store cookie
function setCookie(name, value, expires) {
document.cookie = name + "=" + escape(value) + "; path=/" +
((expires == null) ? "" : "; expires=" + expires.toGMTString());
}
//settime function
var exp = new Date();
exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 31));
</script>



<form><table>
<tr>
<td>Your mailid: </td>
<td><input type="text" name="yourname" onfocus="storeemail(this)" onblur="passemail(this)"></td>
</tr>
</table></form>

921
like
0
dislike
0
mail
flag

You must LOGIN to add comments