1

I have a website with a python backend and a javascript/html front-end, naturally. There is a certain pop-up that I want to show every hour. It does not matter on which page of the site a user is on; once the popup appears, it should wait for one hour to show up again. I can’t figure out how to create the cookie to do this. Presently the popup is governed by the front-end and it shows up each time a person navigates to a different page on the site. I would prefer to keep the management completely in the front-end. But I don’t mind managing it on the server either. So how to do track that I have already shown the popup to a certain user/browser instance and so wait for another hour before showing it again?

The key problem for me is that the cookie is for the entire website not just for one web page

Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • You want it to respond real-time? Cause to create a cookie which expires after an hour isn't that hard I guess?;) And to trigger an action when a cookie isn't set... – Erik van de Ven Jan 09 '15 at 10:09
  • http://stackoverflow.com/questions/3794989/how-to-set-a-cookie-to-expire-in-1-hour-in-javascript provides the answer for your question I guess. They set a cookie on the root which expires after one hour. – Erik van de Ven Jan 09 '15 at 10:14
  • Does it really have to expire? I think I would rather remember the time when the last pop-up was shown and calculate the difference. A time-out could be scheduled when the page is loaded. There would still be a problem if the user has more than one tab - the pop-up might show simultaneously in all tabs. Maybe that's not a problem? – André Laszlo Jan 09 '15 at 10:14
  • Or you could use local storage to communicate between tabs :) http://stackoverflow.com/a/22001740/98057 – André Laszlo Jan 09 '15 at 10:16
  • For clarification: The idea is that no matter where the user lands (except for the homepage, actually), I show my popup and then wait one hour before showing it again. I image I need to use a cookie to do that. Once the cookie has been set, I want it to be accessible/readable from any other page soever; so that I can check that the popup was already presented and therefore should not be presented again. – Katedral Pillon Jan 09 '15 at 10:30

1 Answers1

1

You can use expires with your cookie.

    # Pseudocode
    var d = new Date(); 
    d.setTime(d.getTime() + 60*60*1000); // in milliseconds
    document.cookie = 'foo=bar;path=/;expires='+d.toGMTString()+';';

also you can use max-age with your cookie.

# Pseudocode    
document.cookie = 'foo=bar;path=/;max-age='+60*60+';';

Note : Internet Explorer does not support max-age, so if you want proper cookie persistence cross-browser, then stick to expires.

Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
  • So you are saying that as long as I set the cookie it will apply to my entire website not just a specific webpage? So that I can check the 'global' cookie from any page in my website? – Katedral Pillon Jan 09 '15 at 10:22
  • you can extend the response header of each page of your website to set cookie for each page.when you are returning response for a page override response header of that page and add above logic – Prashant Gaur Jan 09 '15 at 10:25
  • Now you have lost me. So the idea is that no matter where the user lands (except for the homepage, actually), I show my popup and then wait one hour before showing it again. I image I need to use a cookie to do that. Once the cookie has been set, I want it to be accessible/readable from any other page soever. – Katedral Pillon Jan 09 '15 at 10:28
  • If you will set a cookie while returning response for a page, It will be available for all other pages. – Prashant Gaur Jan 09 '15 at 10:42