0

I have here a code that every time the page reloads an alert will pop up. But I want the alert box to pop up once. For example, whenever I open that page an alert will pop up but if I reload that page, there will be no alert box that will pop up. Anyone knows how I can do that?

Here's my code for the alert box:

<script type="text/javascript">
    window.onload = function () { 
    alert("Sending of SMS is scheduled TODAY!");
    //dom not only ready, but everything is loaded
    }
</script>

Thanks for the help in advance.

Jae Eun
  • 57
  • 3
  • 15
  • Use `localStorage/cookie` – Tushar Sep 14 '15 at 04:08
  • I don't know how I can do that sorry but I'm new in Javascript – Jae Eun Sep 14 '15 at 04:09
  • 1
    [Possible duplicate](http://stackoverflow.com/questions/21567044/how-do-i-make-a-javascript-alert-display-only-once-ever) – ODelibalta Sep 14 '15 at 04:18
  • read this http://www.w3schools.com/js/js_cookies.asp – Nic Sep 14 '15 at 04:19
  • @JaeEun at what times do you want it to be shown again? on every visit to the site? once a day? – Swaprks Sep 14 '15 at 04:32
  • @SumanSingh this is one a bit different because he want to display the alert when the page is opened for that first time for that day. I am assuming this will alert only once daily.the post you commented will make the alert only appear once unless the localstorage is manually erased. That is why in the answer below he added the date for that particular day. – guradio Sep 14 '15 at 04:47
  • Thanks @Pekka but in question there is not mention for any condition like once in a day – Suman Singh Sep 14 '15 at 04:50

1 Answers1

0

Do as below:

<script type="text/javascript">
    var dateObj = new Date();
    var month = dateObj.getUTCMonth() + 1; //months from 1-12
    var day = dateObj.getUTCDate();
    var year = dateObj.getUTCFullYear();
    var today = year + "/" + month + "/" + day;
    window.onload = function () { 
          var item = localStorage.getItem("alertShown_"+today);
          if ( item === null || item != "Yes") { 
          //check if localStorage has value or it is == "Yes"
                 alert("Sending of SMS is scheduled TODAY!");
                 localStorage.setItem("alertShown_"+today,"Yes"); //set some value for future check
          }
    }
</script>
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • what if you go to site the following day will this alert?or do you need to manually reset the value of alertshown? – guradio Sep 14 '15 at 04:21