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 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上