Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Mikael Malmgren
Added: Aug 2, 2018 1:13 PM
Views: 3
Tags: no tags
  1. <script>function setCookie(name, value, expires) {
  2.     var cookie = name + "=" + value + "; path=/; domain=." + location.hostname.replace(/^www\./i, "");
  3.  
  4.     if (typeof expires !== "undefined") {
  5.         var now = new Date();
  6.         now.setTime(now.getTime() + expires * 24 * 60 * 60 * 1000);
  7.         cookie += "; expires=" + now.toUTCString();
  8.     }
  9.  
  10.     document.cookie = cookie;
  11. }
  12.  
  13. function getCookie(name) {
  14.     var cookies = document.cookie.split(";"),
  15.         toReturn;
  16.  
  17.     for (var i = 0; i < cookies.length; i++) {
  18.         var cookie = cookies[i].trim();
  19.         if (cookie.indexOf(name + "=") === 0) {
  20.             toReturn = cookie.substring((name + "=").length, cookie.length);
  21.         }
  22.     }
  23.  
  24.     return toReturn;
  25. }
  26.  
  27. (function() {
  28.     var pageviewCount = getCookie("pageviewCount");
  29.  
  30.     if (typeof pageviewCount === "undefined") {
  31.         pageviewCount = 1;
  32.     } else {
  33.         pageviewCount++;
  34.     }
  35.  
  36.     setCookie("pageviewCount", pageviewCount, 30);
  37.  
  38. })();
  39. </script>
  40.