Advertisement
shaashwato1308

JavaScript - Cookies (Set & List)

Dec 23rd, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         function listCookies() {
  2.             var theCookies = document.cookie.split(';');
  3.             var aString = '';
  4.             for (var i = 1 ; i <= theCookies.length; i++) {
  5.                 aString += i + ' ' + theCookies[i-1] + "\n";
  6.             }
  7.             return aString;
  8.         }
  9.         function setCookie(cname, cvalue, exdays) {
  10.             var d = new Date();
  11.             d.setTime(d.getTime() + (exdays*24*60*60*1000));
  12.             var expires = "expires="+d.toUTCString();
  13.             document.cookie = cname + "=" + cvalue + "; " + expires;
  14.         }
  15.         function getCookie(cname) {
  16.             var name = cname + "=";
  17.             var ca = document.cookie.split(';');
  18.             for(var i=0; i<ca.length; i++) {
  19.                 var c = ca[i];
  20.                 while (c.charAt(0)==' ') c = c.substring(1);
  21.                 if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
  22.             }
  23.             return "";
  24.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement