html - How to write a cookie to remember a username in JavaScript -
i working on login page project , wondering how go setting cookie remember username ?
here sample code. html cookies.
<!doctype html> <html> <head> <script> function getcookie(c_name) { var c_value = document.cookie; var c_start = c_value.indexof(" " + c_name + "="); if (c_start == -1) { c_start = c_value.indexof(c_name + "="); } if (c_start == -1) { c_value = null; } else { c_start = c_value.indexof("=", c_start) + 1; var c_end = c_value.indexof(";", c_start); if (c_end == -1) { c_end = c_value.length; } c_value = unescape(c_value.substring(c_start,c_end)); } return c_value; } function setcookie(c_name,value,exdays) { var exdate=new date(); exdate.setdate(exdate.getdate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toutcstring()); document.cookie=c_name + "=" + c_value; } function checkcookie() { var username=getcookie("username"); if (username!=null && username!="") { alert("welcome again " + username); } else { username=prompt("please enter username:",""); if (username!=null && username!="") { setcookie("username",username,365); } } } </script> </head> <body onload="checkcookie()"> </body> </html>
Comments
Post a Comment