简体   繁体   中英

Have my page display cookie information, when certain actions will set a cookie

Is there a way to have my page continuously monitor for a cookie in realtime with Javascript, and if the cookie ever is set then it will send an alert?

The part that I don't understand is how do you check for the cookie in realtime? For example, a user might click a checkbox, a cookie will get set, and then the page to "see" that the cookie is now set and trigger an alert, all in real-time without delay.

Here's an example (not written correctly) that kinda shows what I'm going for.

<div id="click" onclick="setcookie()"></div>

<script>
function checkcookie() {
if (cookie_exists)
alert('cookie exists')
}
</script>

The part that I don't understand is how do I get checkcookie() function to continuously be monitoring for the existence of the cookie I just set with my click?

Here are basic functions for writing and reading cookies:

  function setCookie(cname, cvalue, exdays) {
   var d = new Date();
   d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
   var expires = 'expires=' + d.toUTCString();
   document.cookie = cname + '=' + cvalue + ';' + expires + '';
   path = '/';
}
function getCookie(cname) {
var name = cname + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
  var c = ca[i];
  while (c.charAt(0) == ' ') {
    c = c.substring(1);
  }
  if (c.indexOf(name) == 0) {
    return c.substring(name.length, c.length);
  }
}
return null;
}
function checkCookie(cname) {
  var cookie = getCookie(cname);
  if (cookie != null) {
    return true;
  } else {
    return false;
  }
}

function remove_cookie(cname) {
    document.cookie = cname + '=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;'
}

from w3schools.com/js/js_cookies.asp

The only way is to use

window.setInterval("CheckCookie()", 1000);

Which checks for cookie every second

You have to implement an interval timeout to continuously check for a cookie's value.

 function getCookie(name) {
     var dc = document.cookie;
     var prefix = name + "=";
     var begin = dc.indexOf("; " + prefix);
     if (begin == -1) {
         begin = dc.indexOf(prefix);
         if (begin != 0) return null;
     }
     else
     {
         begin += 2;
         var end = document.cookie.indexOf(";", begin);
         if (end == -1) {
         end = dc.length;
         }
     }
     // because unescape has been deprecated, replaced with decodeURI
     //return unescape(dc.substring(begin + prefix.length, end));
     return decodeURI(dc.substring(begin + prefix.length, end));
 } 

 // Method used for continuously monitoring a cookie
 function monitorCookie(cookieName) {
     setInterval(function() {
         var cookieValue = getCookie(cookieName);

         console.log('Yummy Cookie =' + cookieValue);
     }, 500);// monitor cookie every 500 miliseconds.
 }

 $(document).ready(function() {
     monitorCookie("YummyCookie");
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM