如何让 Javascript 警报只显示一次,永远? [英] How do I make a Javascript alert display only once, ever?
问题描述
我在 Javascript 方面很糟糕,但不幸的是,我需要完成这一项任务.我需要一个 Javascript 警报只显示一次——不是每个浏览器会话一次,而是只显示一次永远.我正在为学校比赛做一个项目;警报将与 Internet Explorer 与我的网站不兼容的方式有关,建议使用 Chrome 或 Firefox.但是,我不想用弹出窗口来纠缠评委,所以最好只出现一次.我在网上找到了下面显示的代码,但我无法使其正常工作.它可能就像某个地方缺少括号一样简单——同样,我不擅长 Javascript,只擅长 HTML 和 CSS.:P 如果鼓励替代代码执行我的请求,那么无论如何,建议它!谢谢大家!
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!
<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]-->
推荐答案
one
不是您所需要的,因为页面刷新会覆盖所有内存".
one
is NOT what you need as a page refresh will override all the 'memory'.
您很可能正在寻找 localStorage
,或者,如果您使用的是版本 8 之前的 Internet Explorer,请使用 cookie:
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>
对于 8 之前的 IE 版本,您需要使用 cookie,因为不支持 localStorage
.这是完全相同的概念,只是方法不同.看看这个问题来实现您页面上的 cookie.
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 question for implementing cookies on your page.
从示例中复制代码后,你应该有 2 个函数 - createCookie()
和 getCookie()
.
After you copied the code from the example, 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]-->
希望这有帮助!
这篇关于如何让 Javascript 警报只显示一次,永远?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!