Listening to an event in cookies

Asked

Viewed 34 times

1

I’m developing an application to be used in two browser tabs. Eventually a tab modifies a cookie, so I need to refresh the other tab to load the correct information. That way you can listen to a change in a cookie, so I can refresh the other tab?

inserir a descrição da imagem aqui

Other suggestions are welcome.

  • For this I strongly recommend localStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/Window.localStorage

  • @Pauloroberto is possible to listen to events with localStorage?

  • gives a read on this question and the answers https://stackoverflow.com/q/26974084/2319589

1 answer

1

No, it is not possible. There is no native API for this. And in general it is bad practice to depend on communication between tabs.

If you need to observe changes in cookies, maybe you can use setInterval to read cookies from time to time. So you can see if there have been changes and make a decision about what to do based on this.

  • The bad of setInterval is that it can degrade performance. A question, why is it a bad practice to depend on tabs communication? ps my communication is with server.

  • You have no guarantee that the page in the other tab is really the page you want/need, the control of sessions is complicated, you need to synchronize processes (opening to race conditions), the native support for this is poor...

  • 1

    with the arrival of localStorage the cookie was half depreciated :P

  • but yes, I could do a Heartbeat listening to the value of x and storing the value of x and when the value is different from the stored do something. In this case the x would be the value of the cookie/or what is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.