【问题标题】:Set cookies in javascript - intergrated with pop up在 javascript 中设置 cookie - 与弹出窗口集成
【发布时间】:2012-03-15 12:44:14
【问题描述】:

我的网站现在在主页运行 javascript 弹出窗口,现在我想做当用户单击主页时,用户将转到主页并显示 javascript pop,但单击关闭后,当用户再次单击主页时,此 javascript 不会运行,此脚本仅活动一天。

那么,如何将 cookie 与我的 javascript 弹出窗口集成?

*这个弹出窗口运行完美,但只剩下 cookie 脚本

网址http://tsubamecorp.com/home/index.php?route=extras/blog/getblogcategory&blogpath=41

这是我的 javascript 弹出窗口:

<script>

    var $xx = jQuery.noConflict();

    $xx(document).ready(function() {

    //select all the a tag with name equal to modal
    $xx('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var id = $xx(this).attr('href');

        //Get the screen height and width
        var maskHeight = $xx(document).height();
        var maskWidth = $xx(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $xx('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $xx('#mask').fadeIn(1000);  
        $xx('#mask').fadeTo("slow",0.90);   

        //Get the window height and width
        var winH = $xx(window).height();
        var winW = $xx(window).width();

        //Set the popup window to center
        $xx(id).css('top',  winH/2-$xx(id).height()/1);
        $xx(id).css('left', winW/2-$xx(id).width()/2);

        //transition effect
        $xx(id).fadeIn(1000); 

    });

    //if close button is clicked
    $xx('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $xx('#mask').hide();
        $xx('.window').hide();
    });             

});
</script>
<script type="text/javascript">
window.onload = function() { $xx('a[href="#dialog1"]:eq(0)').click();
 }
</script>

【问题讨论】:

  • 不太确定你的问题:“这个脚本只激活一天。”,你的意思是在一天之内,每个用户可以看到弹窗一次,而另一天,会看到再来一次?
  • 你说的cookie在哪里。请更清楚地解释您的问题,标题有点误导。你想一键关闭弹窗吗?
  • 好的,你去这里tsubamecorp.com/home/index.php?route=extras/blog/… ..问题是当用户单击主页按钮时,每次用户单击时都会显示此弹出窗口..我希望仅在一天内运行一次此弹出窗口。 .
  • 我知道解决方案是设置 cookie..但我不知道该怎么做..

标签: javascript cookies setcookie


【解决方案1】:

类似的东西。 cookie 的过期是指 sys 删除 cookie 的时间之后。谷歌一些document.cookie

 Cookie = {
    get:function  ( key ) {
        var ret = this.toObj()[key];
        if( ret === undefined ){
            return ret;
        }
        return unescape ( this.toObj()[key] );
    },
    set:function  ( key, value, expires ) {
        if ( expires ){
            var exdata = new Date();
            exdata = exdata.setDate ( exdata.setDate() + expires ).toGMTString();
        }
        document.cookie = key+"="+escape(value) + ( exdata ? "; expires= " + exdata : "");
    },
    remove:function  ( key ) {
        document.cookie = key+"=remove;expires="+(new Date).toGMTString();
    },
    toJSON:function  (  ) {
        return JSON.stringify( this.toObj );
    },
    toObj:function  (  ) {
        var ret={}, cookiearr = document.cookie.split(";");
        var i, start, key, value;
        for ( i = 0; l = cookiearr[ i++ ];) {
            start = l.indexOf ("=");
            ret [ l.slice(0, start) ] = l.slice (start+1);
        }
        return ret;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多