0

我正在尝试编写一个基于 cookie 的菜单,其中包含三个选项(三个链接)。现在我希望当用户访问网站时,他们必须选择三个选项之一。用户的选择可以存储在 cookie 中,这样下次当他到达同一站点时,他不必再次进行选择,并且必须自动将他引导到他之前的选择。

这是我的代码

</style>
<!-- style Develpor page ends here-->
<script src="jqery1.8.js"></script>
<script type="text/javascript">

var val;

$(document).ready(function (e) {
  $('.nav a').click(function (event) {
    event.preventDefault();
    val = $(this).index();

    if (val == 0) {

      $('#hide_main').hide();
      $('.main_BSNS').animate({
        opacity: "show",
        height: "400px"
      }, 'slow')

    } //if val==0 ends here

    else if (val == 1) {

      $('#hide_main').hide();
      $('.main_ACTNT').animate({
        opacity: "show",
        height: "400px"
      }, 'slow')

    } else if (val == 2) {

      $('#hide_main').hide();
      $('.main_devp').animate({
        opacity: "show",
        height: "400px"
      }, 'slow')

    }
  });
});



<!--cookies starts here-->
function getCookie(c_name) {
  var i, x, y, ARRcookies = document.cookie.split(";");
  for (i = 0; i < ARRcookies.length; i++) {
    x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
    y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
    x = x.replace(/^\s+|\s+$/g, "");
    if (x == c_name) {
      return unescape(y);
    }
  }
}

function setCookie(c_name, value, exdays) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
  document.cookie = c_name + "=" + c_value;
}

function checkCookie() {
  var username = getCookie("BSNS");
  if (username != null && username != "") {
    $('#hide_main').hide();
    $('.main_BSNS').animate({
      opacity: "show",
      height: "400px"
    }, 'slow')

  } else {

    username=val;
    alert(val);
    if (username != null && username != "") {
      setCookie("BSNS", username, 365);
      $('#hide_main').hide();
      $('.main_BSNS').animate({
        opacity: "show",
        height: "400px"
      }, 'slow')

    }
  }
}
<!--cookies ends here-->
</script>

现在函数checkCookie()由 body 调用,onload="checkCookie()"并且警报返回值,val因为undefined我知道这是因为checkCookie()之前正在加载document.write但我不知道如何克服这个困难。

4

1 回答 1

0

嘿,伙计们看看这个……我做了一个新代码,它确实对我有用……我把它放在这里,这样寻找像我这样的活动的人可能会发现它很有帮助……

<script type="text/javascript">
var val,cookiee;
$(document).ready(function(e) {

            $('.nav a').click(function(event) {
                event.preventDefault();
                 val=$(this).index();

                if(val==0){

                $('#hide_main').hide();
                $('.main_BSNS').animate({opacity:"show",height:"400px"},'slow')


                    var value="BSNS"
                    var today = new Date();
                    var expire = new Date();
                    nDays=1;
                    expire.setTime(today.getTime() + 3600000*24*nDays);
                    document.cookie = escape(value) + ";expires="+expire.toGMTString();

                    }//if val==0 ends here

                else if(val==1){

                    $('#hide_main').hide();
                    $('.main_ACTNT').animate({opacity:"show",height:"400px"},'slow')                                                

                    var value="ACTNT"
                    var today = new Date();
                    var expire = new Date();
                    nDays=1;
                    expire.setTime(today.getTime() + 3600000*24*nDays);
                    document.cookie = escape(value) + ";expires="+expire.toGMTString();
                    }

                else if(val==2){

                    $('#hide_main').hide();
                    $('.main_devp').animate({opacity:"show",height:"400px"},'slow')                                             

                    var value="APP"
                    var today = new Date();
                    var expire = new Date();
                    nDays=1;
                    expire.setTime(today.getTime() + 3600000*24*nDays);
                    document.cookie = escape(value) + ";expires="+expire.toGMTString();

                    }




            });
});
function checkCookie(){

cookiee=document.cookie;//assigning cookie value to var cookiee
var check_BSNS=cookiee.match(/BSNS/);//checking if cookie has word BSNS
var check_ACTNT=cookiee.match(/ACTNT/);//checking if cookie has word ACTNT
var check_APP=cookiee.match(/APP/);//checking if cookie has word BSNS

if(check_BSNS=="BSNS" && check_ACTNT==null && check_APP==null)//code to executed if cookie has BSNS
{
                $('#hide_main').hide();
                $('.main_BSNS').animate({opacity:"show",height:"400px"},'slow')


    }

else if(check_BSNS==null && check_ACTNT=="ACTNT" && check_APP==null)//code to executed if cookie has ACTNT
{
            $('#hide_main').hide();
            $('.main_ACTNT').animate({opacity:"show",height:"400px"},'slow')                                                


    }

else if(check_BSNS==null && check_ACTNT==null && check_APP=="APP")//code to executed if cookie has APP
{
            $('#hide_main').hide();
            $('.main_devp').animate({opacity:"show",height:"400px"},'slow')                                                     

    }





}

window.onload=checkCookie();// 此代码将在窗口打开后立即加载函数 checkCookie...

现在这个 checkCookie() 函数将被 body onload="checkCookie()" 调用....我只是把注释放在那里以显示 co 的工作

于 2013-01-14T19:24:40.853 回答