Can I somehow follow changes to cookies (for my domain) in my client side javascript. For example a function that gets called if a cookie gets changed , deleted or added
In order of preference
Why? because cookies I depend on in window / tab #1 can get changed in window / tab #2.
I found out that chrome allows extensions to be notified of cookies changes. But thats my least favorite option
Cookies can only be read by the website the domain that creates them; you can use sub-domains domains, url paths. Cookies are generally considered insecure if used from the client side, and should not be used to hold sensitive data if accessed from the client side.
Change a Cookie with JavaScript With JavaScript, you can change a cookie the same way as you create it: document.
For security reasons, the cookie is accepted only if the server is a member of the domain specified by the domain string. If foo.com sent a cookie which had the domain name of bar.com , or even .com , then JavaSCript code on bar.com could read that cookie.
This code defines a function, setCookie(). This function will create a cookie with the name “username”, value “Max Brown” with an expiration date of 30 days from the time it was created.
One option is to write a function that periodically checks the cookie for changes:
var checkCookie = function() { var lastCookie = document.cookie; // 'static' memory between function calls return function() { var currentCookie = document.cookie; if (currentCookie != lastCookie) { // something useful like parse cookie, run a callback fn, etc. lastCookie = currentCookie; // store latest cookie } }; }(); window.setInterval(checkCookie, 100); // run every 100 ms
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With