jQuery Cookie 问题

发布于 2024-10-31 11:52:24 字数 1532 浏览 6 评论 0原文

我环顾四周,似乎找不到任何对我来说足够简单的东西...

我设置了一个网页来检测当前正在运行的浏览器,以及它是否是 Firefox 4.0 以外的浏览器,它会显示一个隐藏的 div,并发出警告,指出该页面最好在 Firefox 4.0 或更高版本中查看。该 div 中有一个按钮,可隐藏 div onclick。

我正在寻找一种方法来记住在会话期间已单击此按钮,以便当用户单击我的“主页”页面时,他们不会每次都收到相同的消息。

当前代码:

<head>
    <script src="js/browsercheck.js"></script>
    <script type="text/javascript">
        // external script "browsercheck.js" checks
        // which browser/version is being used
        // check browser and display message if != Firefox 4.0 or >
        function checkBrowser() {
            var browser = BrowserDetect.browser;
            var version = BrowserDetect.version;
            if (browser == "Firefox") {
                if (version >= 4) {
                    // do nothing
                }
            } else {
                document.getElementById("coverall").style.visibility="visible";
                document.getElementById("browser").innerHTML = browser + " " + version;
            } 
        }
        // remove overlay if user commands
        function removeCoverall() {
            document.getElementById("coverall").style.visibility="hidden";
        }
    </script>
</head>
<body>
    <div id="coverall" style="visibility:hidden;">
        <p>I see you're using <span id="browser"></span>. Please use Firefox.</p>
        <button type="button" onclick="removeCoverall()">I understand</button>
    </div>
</body>

I've looked around a lot, and can't seem to find anything that's simple enough for me to do...

I've set up a web page that detects which browser is currently running, and if it's something other than Firefox 4.0, it displays a hidden div that gives a warning stating that the page is best viewed in Firefox 4.0 or greater. Within that div is a button that hides the div onclick.

I'm looking for a way to remember that this button has been clicked during a session, so that when a user clicks on my "home" page, they don't get the same message every time.

Current code:

<head>
    <script src="js/browsercheck.js"></script>
    <script type="text/javascript">
        // external script "browsercheck.js" checks
        // which browser/version is being used
        // check browser and display message if != Firefox 4.0 or >
        function checkBrowser() {
            var browser = BrowserDetect.browser;
            var version = BrowserDetect.version;
            if (browser == "Firefox") {
                if (version >= 4) {
                    // do nothing
                }
            } else {
                document.getElementById("coverall").style.visibility="visible";
                document.getElementById("browser").innerHTML = browser + " " + version;
            } 
        }
        // remove overlay if user commands
        function removeCoverall() {
            document.getElementById("coverall").style.visibility="hidden";
        }
    </script>
</head>
<body>
    <div id="coverall" style="visibility:hidden;">
        <p>I see you're using <span id="browser"></span>. Please use Firefox.</p>
        <button type="button" onclick="removeCoverall()">I understand</button>
    </div>
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泡沫很甜 2024-11-07 11:52:24

使用 jQuery 和 cookie 插件,您可以执行以下操作:

 function removeCoverall() {
            $.cookie("user_clicked_ok", "true");
            document.getElementById("coverall").style.visibility="hidden";
        }

$(window).ready(function() {
  if ($.cookie("user_clicked_ok")=="true") {removeCoverall();}
});

更多详细信息,请访问:http://www.electrictoolbox.com/jquery-cookies/

Using jQuery and the cookie plugin, you can do:

 function removeCoverall() {
            $.cookie("user_clicked_ok", "true");
            document.getElementById("coverall").style.visibility="hidden";
        }

$(window).ready(function() {
  if ($.cookie("user_clicked_ok")=="true") {removeCoverall();}
});

More details at: http://www.electrictoolbox.com/jquery-cookies/

风情万种。 2024-11-07 11:52:24

removeCoverall 函数中,您可以设置一个 cookie 来指示用户关闭 div 并在 checkBrowser 函数中验证 cookie 是否存在,然后再显示 div。

In the removeCoverall function you could set a cookie which indicates that the user closed the div and in checkBrowser function verify if the cookie is present before showing the div.

守望孤独 2024-11-07 11:52:24
You seem to have the right idea, you need cookies! YUM!

JS

function removeCoverall() {
    var date = new Date();
    date.setTime(date.getTime()+(7*24*60*60*1000));// expires in one week
    document.cookie = 'skipNotify=1;expires='+date.toGMTString()+'; path=/';
    document.getElementById("coverall").style.visibility="hidden";
}

现在在 JavaScript 中检索 cookie 可能很困难,但您可以使用 PHP!

PHP

function checkBrowser() {
    <?php if(isset($_COOKIE['skipNotify']))echo'return;';?>
    var browser = BrowserDetect.browser;
    var version = BrowserDetect.version;
    if (browser == "Firefox") {
        if (version >= 4) {
            // do nothing
        }
    } else {
        document.getElementById("coverall").style.visibility="visible";
        document.getElementById("browser").innerHTML = browser + " " + version;
    } 
}

如果用户拥有 cookie,上面的代码会注入一个 return 语句,因此对该函数的调用不会执行任何操作。

正如其他帖子中提到的,我强烈建议您使用 jQuery 来满足您的所有 javascript 需求。它非常流行、稳定且有用!我只给出了我的答案,因为它不使用任何第三方解决方案。

You seem to have the right idea, you need cookies! YUM!

JS

function removeCoverall() {
    var date = new Date();
    date.setTime(date.getTime()+(7*24*60*60*1000));// expires in one week
    document.cookie = 'skipNotify=1;expires='+date.toGMTString()+'; path=/';
    document.getElementById("coverall").style.visibility="hidden";
}

Now retrieving the cookie can be difficult in javascript, but you can use PHP!

PHP

function checkBrowser() {
    <?php if(isset($_COOKIE['skipNotify']))echo'return;';?>
    var browser = BrowserDetect.browser;
    var version = BrowserDetect.version;
    if (browser == "Firefox") {
        if (version >= 4) {
            // do nothing
        }
    } else {
        document.getElementById("coverall").style.visibility="visible";
        document.getElementById("browser").innerHTML = browser + " " + version;
    } 
}

The above code injects a return statement if the user has the cookie, so the call to the function won't do anything.

As mentioned in the other posts, I highly recommend you use jQuery for all your javascript needs. It's very popular, stable and useful! I only gave my answer as it does not use any third party solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文