使用cookie仅在第一页加载时加载彩盒 [英] Using cookies to load colorbox on first page load only

查看:112
本文介绍了使用cookie仅在第一页加载时加载彩盒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过网络上的参考代码,只在页面加载时加载一个特定元素

I have tried a reference code from the web to load a particular element only once on page load

以下是示例代码


    <script type="text/javascript">
        $(document).ready(function(){
    $.colorbox({width:"40%", height:"63%", inline:true, href:"#subscribe"});
}); 
</script>
 <script type="text/javascript"> 
  $(document).ready(function(){
             if (document.cookie.indexOf('visited=true') === -1) {
                   var expires = new Date();
                   expires.setDate(expires.getDate() 31);
                   document.cookie = "visited=true;
expires=" expires.toUTCString();
               } 
             </script> 

有人可以指导我,有什么不对使用此代码

Can someone guide me, whats wrong with this code

以下是我正在编辑的html页面

Here is the html page where I am editing


<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset=utf-8 />
    <title>Popup Subscription Box</title>
    <style type="text/css">
        body{font:12px/1.2 Verdana, Arial, san-serrif; padding:0 10px;}
        a:link, a:visited{text-decoration:none; color:#416CE5; border-bottom:1px solid #416CE5;}
        h2{font-size:13px; margin:15px 0 0 0;}
    </style>
    <link media="screen" rel="stylesheet" href="colorbox.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script&gt;
    <script src="../colorbox/jquery.colorbox-min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
    $.colorbox({width:"30%", inline:true, href:"#subscribe"});
}); 
</script>
 <script type="text/javascript"> 
  jQuery(document).ready(function(){
             if (document.cookie.indexOf('visited=true') == -1) {
                   var expires = new Date();
                   expires.setDate(expires.getDate() 31);
                   document.cookie = "visited=true;
expires=" expires.toUTCString();
               } 
             </script> 
<style type="text/css">
.example8 { display:none; }
</style>
</head>
<body>
    <h1>Demonstration</h1>

* /这是div id =订阅信息* /

*/ Here is the div id=subscribe info goes */

< / body>
< / html>

</body> </html>

推荐答案

你的cookie集......有点可怕(例如,这个:expires.setDate(expires) .getDate()31);并不意味着什么。在getDate和31之间没有运算符(加号)。你似乎也有一个换行符。在这里,这会打破一切。

Your cookie set was... sort of horribly broken (for instance, this: "expires.setDate(expires.getDate() 31);" doesn't mean anything. You don't have a an operator (plus) between getDate and 31. You also seemed to have a newline character in there, which would break everything.

可能更重要的是,你需要在程序流程中实际调用colorbox,否则,你只需要每次调用它。

Possibly more importantly, you need to actually put your call to colorbox inside of your program flow as well, otherwise, you'd just be calling it every time.

如果您转储其他脚本(立即启动彩盒的那个),并且如果您实际上有一个带有页面上的订阅ID:

The following should work for you if you dump your other script (the one that starts colorbox immediately), and if you actually have a div with an ID of subscribe on the page:

jQuery(document).ready(function(){
    if (document.cookie.indexOf('visited=true') == -1) {
        var fifteenDays = 1000*60*60*24*15;
        var expires = new Date((new Date()).valueOf() + fifteenDays);
        document.cookie = "visited=true;expires=" + expires.toUTCString();
        $.colorbox({width:"30%", inline:true, href:"#subscribe"});
    }
});

这是一个小提琴

这篇关于使用cookie仅在第一页加载时加载彩盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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