jQuery cookie asp.net [英] jquery cookie asp.net

查看:60
本文介绍了jQuery cookie asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网站上使用jquery cookie插件.我在做什么是在页面加载上显示firstdiv等待一段时间,然后删除firstdiv并显示secdiv,然后再次等待一段时间,然后删除secdiv,最后显示maindiv.这可以正常工作,但希望仅在您首次访问主页时发生,而在该活动会话期间的每次后续访问中都不要发生.尝试使用jQuery来解决此问题.这是我现在所拥有的,并且似乎工作不佳.它只会在所有情况下加载maindiv.你能把我带到正确的方向吗?

I am trying to use jquery cookie plugin on my website. what am i doing is show firstdiv on page load wait for some time and remove the firstdiv and show the secdiv and again wait for sometime, then remove the secdiv and finally show the maindiv. This works fine but want this to happen only when you visit the home page first time but not also for every subsequent visits during that active session. trying jquery to fix this issue. here is what i have now and doesnt seem to work well. It just loads the maindiv on all the occasions. could you please put me in the right direction.

$(document).ready(function () {
       var visitcookie = $.cookie('visitcookie', 'first', { Path: "/", expires: 0 });
       if (visitcookie == 'first') {
            document.getElementById('secdiv').style.visibility = "hidden";
            document.getElementById('maindiv').style.visibility = "hidden";
            setTimeout(function () {
            $("#firstdiv").fadeOut("slow", function () {
            $("#firstdiv").remove();
            document.getElementById('secdiv').style.visibility = "visible";
            $("#secdiv").show();
            $("#secdiv").fadeOut("slow", function () {
            $("#secdiv").remove();
            document.getElementById('maindiv').style.visibility = "visible";
            $("#maindiv").show();
            $.cookie('visitcookie', 'second');
                        });
                    });
                }, 2000);
            }

            else {
                document.getElementById('firstdiv').style.visibility = "hidden";
                document.getElementById('secdiv').style.visibility = "hidden";
                document.getElementById('maindiv').style.visibility = "visible";
            }
        });

推荐答案

如果您发出访问cookie的警报,它将返回: visitcookie = first; expires = date ... 因此,我要做的是将12个字符的子字符串删除,以删除"visitcookie =,然后仅返回"first",希望对您有所帮助..

if you do an alert to visitcookie it returns: visitcookie=first; expires=date... so what I did is a substring 12 characters to remove the "visitcookie=" and then just return "first" hope this helps..

$(document).ready(function () {
   var visitcookie = $.cookie('visitcookie');
   switch(visitcookie)
   {
        case 'first':
        document.getElementById('secdiv').style.display = 'none';
        document.getElementById('maindiv').style.display = 'none';
        setTimeout(function () {
            $("#firstdiv").fadeOut("slow", function () {
                $("#firstdiv").remove();
                document.getElementById('secdiv').style.visibility = "visible";
                $("#secdiv").show();
                $("#secdiv").fadeOut("slow", function () {
                    $("#secdiv").remove();
                    document.getElementById('maindiv').style.visibility = "visible";
                    $("#maindiv").show();
                    $.cookie('visitcookie', 'second', { expires: 7 , Path: "/"});
                    });
                });
            }, 2000);
        break;

        case null:
            document.getElementById('secdiv').style.display = 'none';
            document.getElementById('maindiv').style.display = 'none';
            setTimeout(function () {
                $("#firstdiv").fadeOut("slow", function () {
                    $("#firstdiv").remove();
                    document.getElementById('secdiv').style.visibility = "visible";
                    $("#secdiv").show();
                    $("#secdiv").fadeOut("slow", function () {
                        $("#secdiv").remove();
                        document.getElementById('maindiv').style.visibility = "visible";
                        $("#maindiv").show();
                        $.cookie('visitcookie', 'second', { expires: 7 , Path: "/"});
                        });
                    });
                }, 2000);
            break;  
        default:
            document.getElementById('firstdiv').style.display = 'none';
            document.getElementById('secdiv').style.display = 'none';
            document.getElementById('maindiv').style.visibility = "visible";
        break;
   }

});

在页面上进行

* edit加载会捕获cookie值,然后我进行了一个开关,因此,如果该值为null,则它将经历显示所需内容的过程,然后将cookie值设置为second ..当您访问另一页面时然后返回,它只显示了主要的div.我还继续将过期"更改为7,因为在0时您无法正确测试此内容.希望对您有所帮助

*edit on page load you capture the cookie value then I did a switch so if the value is null then it goes through the process of display what you need then it sets the cookie value to second..when you visit another page and comeback it shows just the main div..I also went ahead and changed the "expire" to 7 because at 0 you couldn't test this correctly.. hope that helps you

这篇关于jQuery cookie asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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