How to make Javascript alert display only once, ever?

I am terrible in Javascript, but unfortunately I need this one task. I need Javascript to be displayed only once - not once for a browser session, but only once. I am working on a school contest project; the warning will relate to how Internet Explorer is incompatible with my site, and Chrome or Firefox is recommended. However, I don’t want to pester judges with pop-ups, so only once would be better. I found the code that appears below online, but I cannot get it to work correctly. It may be something as simple as a missing parenthesis somewhere - again, I am not very good at Javascript, just HTML and CSS .: P If alternative code is recommended to fulfill my request, then be sure to suggest it! Thanks everyone!

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <!--[if IE]> <script type="text/javascript"> $(document).one(function() { alert("You are using Internet Explorer to view this webpage. Your experience may be subpar while using Internet Explorer; we recommend using an alternative internet browser, such as Chrome or Firefox, to view our website."); } ); </script> <![endif]--> 
+3
javascript jquery html css alert
source share
1 answer

one NOT what you need as page refreshing will override all "memory".

You are most likely looking for localStorage or, if you are using Internet Explorer prior to version 8, use cookies:

  <script type="text/javascript"> var alerted = localStorage.getItem('alerted') || ''; if (alerted != 'yes') { alert("You are using Internet Explorer to view this webpage. Your experience may be subpar while using Internet Explorer; we recommend using an alternative internet browser, such as Chrome or Firefox, to view our website."); localStorage.setItem('alerted','yes'); } </script> 

EDIT:

For versions of IE prior to 8, you need to use cookies, as there is no support for localStorage . This is the same concept, just different methods. Take a look at this question for implementing cookies on your page.

After you copied the code from, you should have 2 functions - createCookie() and getCookie() .

 var createCookie = function(name, value, days) { .... //copy it from the other question .. } function getCookie(c_name) { .... //copy it from the other question .. } <!--[if IE]> <script type="text/javascript"> var alerted = getCookie('alerted') || ''; if (alerted != 'yes') { alert("You are using Internet Explorer to view this webpage. Your experience may be subpar while using Internet Explorer; we recommend using an alternative internet browser, such as Chrome or Firefox, to view our website."); createCookie('alerted','yes',365);//cookies expires after 365 days } </script> <![endif]--> 

Hope this helps!

+6
source share

All Articles