【问题标题】:Display A Popup Only Once Per User每个用户仅显示一次弹出窗口
【发布时间】:2014-06-12 16:27:34
【问题描述】:

这个问题已经有了答案,但我仍然不确定它是如何工作的。

我在 footer.php 中使用以下 HTML:

<div id="popup">
    <div>
        <div id="popup-close">X</div>
            <h2>Content Goes Here</h2>
    </div>
</div>

以及以下 Javascript:

$j(document).ready(function() {
    $j("#popup").delay(2000).fadeIn();
    $j('#popup-close').click(function(e) // You are clicking the close button
    {
    $j('#popup').fadeOut(); // Now the pop up is hiden.
    });
    $j('#popup').click(function(e) 
    {
    $j('#popup').fadeOut(); 
    });
});

一切都很好,但我只想为每个用户显示一次弹出窗口(可能使用所有论坛帖子都在讨论的 cookie),但我不知道如何将它合并到上面的 JS 中。

我知道我必须在页脚中加载 cookie JS:

<script type="text/javascript" src="scripts/jquery.cookies.2.2.0.min.js"></script> 

但这就是我所理解的,谁能告诉我添加了 cookie 的 JS/jQuery 应该是什么样子?

谢谢

詹姆斯

【问题讨论】:

    标签: javascript jquery cookies


    【解决方案1】:

    *注意:由于数据存储在浏览器内存中,这将在每个浏览器中显示一次弹出窗口。

    试试 HTML localStorage。

    方法:

    • localStorage.getItem('key');
    • localStorage.setItem('key','value');

    $j(document).ready(function() {
        if(localStorage.getItem('popState') != 'shown'){
            $j('#popup').delay(2000).fadeIn();
            localStorage.setItem('popState','shown')
        }
    
        $j('#popup-close, #popup').click(function() // You are clicking the close button
        {
            $j('#popup').fadeOut(); // Now the pop up is hidden.
        });
    });
    

    Working Demo

    【讨论】:

    • 这个解决方案实际上会为每个浏览器显示一个弹出窗口,而不是每个用户。
    • @KobeBryan 您可以使用 sessionStorage 以便在您关闭浏览器窗口时自动重置。
    • 知道方法了。
    【解决方案2】:

    本例使用jquery-cookie

    检查 cookie 是否存在且未过期 - 如果其中任何一个失败,则显示弹出窗口并设置 cookie(半伪代码):

    if($.cookie('popup') != 'seen'){
        $.cookie('popup', 'seen', { expires: 365, path: '/' }); // Set it to last a year, for example.
        $j("#popup").delay(2000).fadeIn();
        $j('#popup-close').click(function(e) // You are clicking the close button
            {
            $j('#popup').fadeOut(); // Now the pop up is hiden.
        });
        $j('#popup').click(function(e) 
            {
            $j('#popup').fadeOut(); 
        });
    };
    

    【讨论】:

    • 感谢您的快速回复 Calvin,在阅读了 HTML 5 Storage 之后,它似乎是更快更安全的选择!
    • 去吧,这就是这个网站的全部内容。如果它有效并且运作良好,请坚持下去。
    • new doumentation here 你可以像这样使用它:Cookies.set('popup', 'seen', { expires: 30, path: '/' });
    【解决方案3】:

    您可以使用 php.ini 解决此问题。您只在第一页加载时回显弹出窗口的代码。

    另一种方法...设置一个cookie,它基本上是一个位于浏览器中并包含某种数据的文件。在第一个页面加载时,您将创建一个 cookie。然后在之后的每个页面中检查您的 cookie 是否已设置。如果已设置,则不显示弹出窗口。但是,如果未设置,则设置 cookie 并显示弹出窗口。

    伪代码:

    if(cookie_is_not_set) {
        show_pop_up;
        set_cookie;
    }
    

    【讨论】:

    • 感谢 Nicolas 的快速回复,在阅读了 HTML 5 Storage 之后,它似乎是更快更安全的选择!
    【解决方案4】:

    为使用 Ionic 的人提供快速解答。我只需要显示一次工具提示,所以我使用 $localStorage 来实现这一点。这是为了播放曲目,所以当他们按下播放时,它会显示一次工具提示。

    $scope.storage = $localStorage; //connects an object to $localstorage
    
    $scope.storage.hasSeenPopup = "false";  // they haven't seen it
    
    
    $scope.showPopup = function() {  // popup to tell people to turn sound on
    
        $scope.data = {}
        // An elaborate, custom popup
        var myPopup = $ionicPopup.show({
            template: '<p class="popuptext">Turn Sound On!</p>',
            cssClass: 'popup'
    
        });        
        $timeout(function() {
            myPopup.close(); //close the popup after 3 seconds for some reason
        }, 2000);
        $scope.storage.hasSeenPopup = "true"; // they've now seen it
    
    };
    
    $scope.playStream = function(show) {
        PlayerService.play(show);
    
        $scope.audioObject = audioObject; // this allow for styling the play/pause icons
    
        if ($scope.storage.hasSeenPopup === "false"){ //only show if they haven't seen it.
            $scope.showPopup();
        }
    }
    

    【讨论】:

      【解决方案5】:

      您可以使用localStorage 的removeItem() 类在浏览器关闭时销毁该键:

      window.onbeforeunload = function{
          localStorage.removeItem('your key');
      };
      

      【讨论】:

        【解决方案6】:

        只显示一次弹出窗口的代码(在这种情况下为引导模式):

        modal.js

         $(document).ready(function() {
             if (Cookies('pop') == null) {
                 $('#ModalIdName').modal('show');
                 Cookies('pop', '365');
             }
         });
        

        这是 Rails 的完整代码片段:

        将上面的脚本添加到你的 js repo (在 Rails 中:app/javascript/packs)

        在 Rails 中,我们有一个特定的脚本打包方式,所以:

        1. 下载 js-cookie 插件(需要使用 Javascript Cokkies)https://github.com/js-cookie/js-cookie(名称应为:'js.cookie.js')

          /*!
           * JavaScript Cookie v2.2.0
           * https://github.com/js-cookie/js-cookie
           *
           * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
           * Released under the MIT license
           */
          ;(function (factory) {
            var registeredInModuleLoader = false;
            if (typeof define === 'function' && define.amd) {
              define(factory);
              registeredInModul
           ...
          
        2. 将//= require js.cookie 添加到application.js

        它将完美运行 365 天!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-15
          相关资源
          最近更新 更多