如何让 Javascript 警报只显示一次,永远?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21567044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a Javascript alert display only once, ever?
提问by Ben
I'm terrible at Javascript, but unfortunately, I need this one task completed. I need a Javascript alert to only display once--not once per browser session, but only once ever. I'm working on a project for a school competition; the alert will pertain to how Internet Explorer isn't compatible with my site and Chrome or Firefox is recommended. However, I don't want to pester the judges with pop-ups, so it only appearing up once would be best. I found the code that's displayed below online, but I can't make it work correctly. It may be something as simple as a parenthesis missing somewhere--again, I'm not good with Javascript, just HTML and CSS. :P If alternative code is encouraged to carry out my request, then by all means, suggest it! Thanks to all!
我在 Javascript 方面很糟糕,但不幸的是,我需要完成这一项任务。我需要一个JavaScript警告,只显示一次-每个浏览器会话不是一次,而是一次过。我正在为学校比赛做一个项目;警报将与 Internet Explorer 与我的网站不兼容的方式有关,建议使用 Chrome 或 Firefox。但是,我不想用弹出窗口来纠缠评委,所以最好只出现一次。我在网上找到了下面显示的代码,但我无法使其正常工作。它可能就像某个地方缺少括号一样简单——同样,我不擅长 Javascript,只擅长 HTML 和 CSS。:P 如果鼓励替代代码执行我的请求,那么无论如何,建议它!谢谢大家!
<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]-->
回答by dev7
one
is NOT what you need as a page refresh will override all the 'memory'.
one
不是您需要的,因为页面刷新将覆盖所有“内存”。
You are most likely looking for localStorage
or, if you are using internet explorer prior to version 8, use cookies:
您很可能正在寻找localStorage
或者,如果您使用的是版本 8 之前的 Internet Explorer,请使用 cookie:
<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 IE versions prior to 8 you need to use cookies, since there is not suppor for localStorage
. It's the exact same concept, just different methods. Take a look at this questionfor implementing cookies on your page.
对于 8 之前的 IE 版本,您需要使用 cookie,因为不支持localStorage
. 这是完全相同的概念,只是方法不同。看看这个问题,以在您的页面上实施 cookie。
After you copied the code from the example, you should have 2 functions - createCookie()
and getCookie()
.
从示例中复制代码后,您应该有 2 个函数 -createCookie()
和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!
希望这可以帮助!