0

I'm having a problem relating to storing a variable within Javascript. I have a web monitoring page (I have written the code which works), which refreshes automatically every 5 mins.

If one of the sites I am monitoring goes down, the JS sends an email to the admin, but I only want to send this once.

My logic was to:

Check if a site is down if variable emailPreviouslySent == 'no' then send email to admin write to variable emailPreviouslySent = 'yes'

Page would refresh

Check if a site is down emailPreviouslySent == 'yes'

email would not be sent again

However I'm finding that the variable is emptied each time the page refreshes.

Is there a way of carrying the variable even though the page is refreshing, or perhaps another way around this?

Thanks for your help.

2
  • 1
    Have you tried cookies ? Commented Jun 20, 2012 at 15:22
  • 1
    You can usecookies, or HTML5 local storage. Commented Jun 20, 2012 at 15:23

1 Answer 1

4

However I'm finding that the variable is emptied each time the page refreshes.

You are inside state-less HTTP request. Store your variable in cookie to retain its value even after page refresh but notice that a cookie can be deleted by a user too.

Or as rightly suggested by @Matt Ball, a better approach would be to use HTML5's localStorage feature.

Sign up to request clarification or add additional context in comments.

3 Comments

That fast, thanks. Not looked into cookies, however that may be the way to go, as I'm (sadly) forced to use IE.
May I suggest taking a look at store.js (github.com/frankkohlhepp/store-js), it makes using localStorage nice and easy. And I believe it has solutions for working with IE6 and above :-)
@BenEverard: Ah have used that, it does the job well. Thanks for adding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.