jquery和javascript函数计时

时间:2013-01-14 10:00:49

标签: javascript jquery

我正在尝试编写一个基于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()由正文onload="checkCookie()"调用,警报将val的值返回为undefined我知道这是因为checkCookie()正在在document.write之前加载,但我不知道如何克服这个困难。

1 个答案:

答案 0 :(得分: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的工作