0

How would i go with triggering an alarm or parse it in the html file after writing a js cookie without reloading the page first?

I've tried $(window).bind("load", function () but with no success, the alarm still doesn't trigger without reloading the page, but the cookie is written

My JS

$(".addCookie").on("click", function() {
   Cookies.set("cookie", "added"); //adds cookie
})

$('.cookie').html(Cookies.get("cookie")); //writes the cookie in the html file

Codepen

ekclone
  • 1,030
  • 2
  • 17
  • 39
  • There is a similar question at http://stackoverflow.com/questions/4132561/is-there-a-way-to-listen-to-event-when-a-cookie-is-changed-in-a-firefox-extensio – tanaydin Jun 23 '16 at 12:34
  • I'm not sure I understand your question. Is there more code ? it looks like you're registering an event handler that would change the cookie at some later point, then immediately reading the cookie. – Touffy Jun 23 '16 at 12:36

1 Answers1

0

It looks like a simple case of not understanding asynchronous programming.

This adds a cookie, but not right now. The cookie code will only be evaluated when you click on the .addCookie element on the page:

$(".addCookie").on("click", function() {
   Cookies.set("cookie", "added"); //adds cookie
})

This dumps the cookie, right now, without waiting for you to click:

$('.cookie').html(Cookies.get("cookie")); //writes the cookie in the html file

If you write the cookie's contents in another onclick handler for another button, you could click that new button to dump the cookie after clicking the first button to set its value.

Touffy
  • 6,309
  • 22
  • 28
  • so there's no way I could parse the html directly after the cookie is written? – ekclone Jun 23 '16 at 13:30
  • The cookie is not inside the HTML. It's stored by the browser, accessible by any page from the same domain. You can of course parse the cookie right after writing it if you want (by putting the code inside the same event handler as the code that writes the cookie). – Touffy Jun 23 '16 at 15:22
  • Oh and by the way, the last line in your code doesn't write anything in the HTML file, it changes the Document Object Model, which is an in-memory representation of the HTML. You can't write files with regular webpage JavaScript. – Touffy Jun 23 '16 at 15:30