如何使Javascript警报显示只有一次,永远? [英] How do I make a Javascript alert display only once, ever?

查看:161
本文介绍了如何使Javascript警报显示只有一次,永远?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可怕的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(您正在使用Internet Explorer查看此网页。在使用Internet Explorer时,您的体验可能不佳;我们建议使用其他互联网浏览器Firefox,查看我们的网站。);});
< / script>
<![endif] - >


解决方案

$ c>不是你需要的,因为页面刷新将覆盖所有的'内存'。



你最有可能寻找 localStorage 或者,如果您在版本8之前使用Internet Explorer,请使用Cookie:

 < script type = text / javascript> 
var alerts = localStorage.getItem('remind')|| '';
if(alerts!='yes'){
alert(您正在使用Internet Explorer查看此网页。使用Internet Explorer时,您的体验可能不佳;我们建议使用其他互联网浏览器如Chrome或Firefox,查看我们的网站。
localStorage.setItem('remind','yes');
}
< / script>



编辑



对于8之前的IE版本,您需要使用Cookie,因为不支持 localStorage 。这是完全相同的概念,只是不同的方法。请查看此问题以实施



在您复制了示例,您应该有两个函数 - createCookie() getCookie()

  var createCookie = function(name,value,days){
....
//从其他问题中复制它
..
}

function getCookie(c_name){
....
//从另一个问题中复制它
..
}

<! - [if IE]
< script type =text / javascript>
var remind = getCookie('remind')|| '';
if(alerts!='yes'){
alert(您正在使用Internet Explorer查看此网页。使用Internet Explorer时,您的体验可能不佳;我们建议使用其他互联网浏览器如Chrome或Firefox,查看我们的网站。
createCookie('remind','yes',365); // cookie expires after 365 days
}
< / script>
<![endif] - >

希望这有助于!


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 is NOT what you need as a page refresh will override all the '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 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.

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]-->

Hope this helps!

这篇关于如何使Javascript警报显示只有一次,永远?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆