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条)

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本