每ip / 24小时显示div一次无法在Chrome中工作 [英] show div one time per ip/24hs not work in chrome

查看:113
本文介绍了每ip / 24小时显示div一次无法在Chrome中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究这个代码..并且可以在所有浏览器中工作,除了在chrome中。
我想每隔24小时显示一个div(A)每个IP一次。在显示div(A)后,我想在每次访问我的页面时显示另一个div(B)。
这是我的代码

I'm working on this code .. and works in all browsers, except in chrome. I want to show a div(A) 1 time per IP every 24 hours. After displaying that div(A), I want to show another div(B) every time that ip visit my page. This is my code

    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://github.com/carhartl/jquery-cookie/raw/master/jquery.cookie.js"></script>
    <script type="text/javascript">                                         
    $(document).ready(function() {
        if( $.cookie('showOnlyOne') ){
            //it is still within the day
            //hide the div
            $('#showOnlyOnceADay').hide();
            $('#showothertimes').show();
        } else {
            //either cookie already expired, or user never visit the site
            //create the cookie
            $.cookie('showOnlyOne', 'showOnlyOne', { expires: 1 });

            //and display the div
            $('#showOnlyOnceADay').show();
            $('#showothertimes').hide();
        }
    });
    </script>     
</head>
<body>
    <div id="showOnlyOnceADay">
      Div(A)
    </div>
    <div id="showothertimes">
     Div(B)
    </div>
</body>

在Chrome中同时显示两个div始终。有什么问题?谢谢!

In Chrome shows the two div at the same time always. What's the problem? Thank you!

推荐答案

您正在链接到github上的JavaScript文件的原始版本。这不是正确的MIME类型,因此您需要下载并自行提供。 Github raw不是CDN。

You are linking to the raw version of the javascript file on github. This is not being served with the correct MIME type, so you'll need to download it and serve it yourself. Github raw is not a CDN.

你是在本地机器上开发的吗?如果是这样,Chrome不会通过文件协议接受Cookie。您可以通过启用带有标志的Chrome来启用它们,也可以使用某个Web服务器通过HTTP进行测试。更多信息可以在这里找到:

Are you developing this on your local machine? If so, Chrome does not accept cookies over the file protocol. You can either enable them by starting Chrome with a flag, or test it over HTTP using a web server somewhere. More information can be found here:

https://github.com/carhartl/jquery-cookie/issues/231

另外,仅仅指出技术上你的代码不会是通过IP地址工作,因为Cookie存储在每台机器和每个浏览器中,并且易于清除和规避。除此之外,您的两个div的标记在源代码中仍然可用,并且可以使用开发工具轻松查看。如果安全性(或者说规则的可执行性)是一个问题,您应该考虑将cookie代码移到服务器端,并且只将相应的div写入浏览器。

Also, just to point out that technically your code will not be working by IP address as cookies are stored per machine and per browser as well as easily cleared and circumvented. Aside from this, your markup for both divs is all still available in the source and could be easily viewed with dev tools. If security (or rather the enforceability of your rule) is a concern you should think about moving the cookie code to the server side and only writing the appropriate div to the browser.

这篇关于每ip / 24小时显示div一次无法在Chrome中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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