×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Gábor Papp
Added: May 21, 2017 5:53 PM
Views: 5
Tags: no tags
  1. function setCookie(cname, cvalue, exdays) {
  2.     var d = new Date();
  3.     d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  4.     var expires = "expires="+d.toUTCString();
  5.     document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  6. }
  7.  
  8. function getCookie(cname) {
  9.     var name = cname + "=";
  10.     var ca = document.cookie.split(';');
  11.     for(var i = 0; i < ca.length; i++) {
  12.         var c = ca[i];
  13.         while (c.charAt(0) == ' ') {
  14.             c = c.substring(1);
  15.         }
  16.         if (c.indexOf(name) == 0) {
  17.             return c.substring(name.length, c.length);
  18.         }
  19.     }
  20.     return "";
  21. }
  22.  
  23. var user = getCookie("username");
  24. setCookie("username", user, 365);