duanchi4184 2012-09-25 19:02
浏览 106
已采纳

使用Cookie在页面加载上显示弹出消息

I'm creating a popup message on my website with this tutorial: LINK

It's supposed to only display/popup, when the user is using IE7 or less, and only once - so I need to use cookies.

The tutorial uses a jQuery plugin called "Reveal" and is activated like so:

<script type="text/javascript">
$(document).ready(function() {
    $('#button').click(function(e) {        // Button which will activate our modal
        $('#modal').reveal({                // The item which will be opened with reveal
            animation: 'fade',              // fade, fadeAndPop, none
            animationspeed: 600,            // how fast animtions are
            closeonbackgroundclick: true,   // if you click background will modal close?
            dismissmodalclass: 'close'      // the class of a button or element that will close an open modal
        });
    return false;
    });
});

The HTML that goes along with this script is written like so:

    <div id="modal">
        <div id="heading">
            Are you sure you want to do that?
        </div>
        <div id="content">
            <p>Clicking yes will make Comic Sans your new system<br> default font. Seriously, have you thought this through?</p>
            <a href="#" class="button green close"><img src="images/tick.png">Yes, do it now!</a>
            <a href="#" class="button red close"><img src="images/cross.png">No, I’m insane!</a>
        </div>
    </div>

I tried adding the cookie myself to the "Reveal"-plugin, using THIS script, but with no luck:

<script type="text/javascript">
    $(document).ready(function() {
        if ( $.cookie ( 'first_visit' ) !== '0' ){
            $('#modal').reveal({                // The item which will be opened with reveal
                animation: 'fade',              // fade, fadeAndPop, none
                animationspeed: 600,            // how fast animtions are
                closeonbackgroundclick: true,   // if you click background will modal close?
                dismissmodalclass: 'close'      // the class of a button or element that will close an open modal
            });
            $.cookie( 'first_visit' , '0' );
        return false;
        }
    });
</script>

What am I doing wrong? I want this popup message to display on IE7 and below - only on the first time visiting the site.

  • 写回答

2条回答 默认 最新

  • dpr77335 2012-09-25 19:41
    关注

    You have an error on line 12 in script.js. You are using a method called validationEngine. Are you sure you loaded this, because I don't see it anywhere. You can also try to type $.validationEngine in your console, and it will tell you it is undefined. This will probably stop your javascript execution.

    Either make sure you include this validation engine, or don't use it, and get back if the error is still there. From what I can see, your ussage of the cookie plugin is correct. It is also initialized, and I can use it on your site through the console.

    EDIT

    You are returning false after calling the modalizer initialization. I guess this is because you copied it from code that belongs to a click event on a html anchor (<a />), and it needs to stop the default action from being carried out (going to the link). I don't think it does anything in this case, but returning false on $.ready seems instintively like a pretty bad idea. Get rid of that!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图