Advertisement
FahimFaisal

Cookie_management

Oct 11th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.95 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Cookie handling</title>
  6. </head>
  7.  
  8. <script>
  9.  
  10.     function setCookie(cname,cvalue,exdays) {
  11.         var d = new Date();
  12.         d.setTime(d.getTime() + (exdays*24*60*60*1000));
  13.         var expires = "expires=" + d.toGMTString();
  14.         document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  15.         document.write(cname + "=" + cvalue + ";" + expires + ";path=/");
  16.     }
  17.  
  18.    /* function  setCookie1(cname,cvalue,exdays) {
  19.         var d=new Dare();
  20.         d.setTime(d.getTime()+(exdays*24*60*60*1000));
  21.         var expires="expires="+d.prototype.toString();
  22.         document.write(expires);
  23.         document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  24.  
  25.     }*/
  26.  
  27.     function getCookie(cname) {
  28.         var name = cname + "=";
  29.         var decodedCookie = decodeURIComponent(document.cookie);
  30.         document.write(decodedCookie+"<br/>");
  31.         var ca = decodedCookie.split(';');
  32.         for(var i = 0; i < ca.length; i++) {
  33.            var c = ca[i];
  34.            document.write(c+"<br/>");
  35.             while (c.charAt(0) == ' ') {
  36.                 //document.write("<-----------------------------");
  37.                c = c.substring(1);
  38.                //document.write(c+"<br/>");
  39.             }
  40.             if (c.indexOf(name) == 0) {
  41.                 document.write(c.length+"  "+name.length);
  42.                 return c.substring(name.length, c.length);
  43.             }
  44.         }
  45.         return "";
  46.     }
  47.  
  48.     function checkCookie() {
  49.         var user=getCookie("username");
  50.         if (user != "") {
  51.             alert("Welcome again " + user);
  52.         } else {
  53.             user = prompt("Please enter your name:","");
  54.             if (user != "" && user != null) {
  55.                setCookie("username", user , 30);
  56.             }
  57.         }
  58.     }
  59. </script>
  60. </head>
  61.  
  62. <body onload="checkCookie()"></body>
  63.  
  64. </html>
  65.  
  66. </script>
  67. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement