JS¼¼Êõ

³õѧJavascriptÖ®cookieƪ(Òë)

×ÖºÅ+ ×÷Õߣº À´Ô´£º    2014-11-17 20:00 ÎÒÒªÆÀÂÛ( )

³õѧJavascriptÖ®cookieƪ(Òë),ÔĶÁ³õѧJavascriptÖ®cookieƪ(Òë),[ÒëÕß×¢£º½«±¾ÎĵÄ×îºóʾÀý´úÂ뿽±´ÖÁÎı¾ÎļþÖÐ,¸üÃûÎļþΪhomepage.htm¸ñʽÎļþ£¬ÔÚä¯ÀÀÆ÷ÖÐÔËÐУ¬±¾ÎÄÒÔ¸ÃʾÀý´úÂë½²½â ¼ò½é ==================================== ÎÊÌâÊÇÎÒÏë½â¾ö×Ô¶¯·Ã

 [ÒëÕß×¢£º½«±¾ÎĵÄ×îºóʾÀý´úÂ뿽±´ÖÁÎı¾ÎļþÖÐ,¸üÃûÎļþΪhomepage.htm¸ñʽÎļþ£¬ÔÚä¯ÀÀÆ÷ÖÐÔËÐУ¬±¾ÎÄÒÔ¸ÃʾÀý´úÂë½²½â


  ¼ò½é

  ====================================

  ÎÊÌâÊÇÎÒÏë½â¾ö×Ô¶¯·ÃÎÊhttp://www.thehungersite.com¡£Õâ¸öÒ³ÃæÄܹ»ÏÞÖÆÄãÿһÌìÖ»·ÃÎÊËüÒ»´Î£¨²»ÒªÍü¼ÇÔÚÕâÁ´½ÓÉϵã»÷£©¡£
  ÏÖÔÚΪֹ£¬Ã¿´ÎÒ»¿ªÊ¼ÎÒ×öµÄµÚÒ»¼þʾÍÊÇÊÖ¹¤Ñ¡ÔñÎÒµÄÊéÇ©¼ÓÔØÕâ¸öÒ³Ãæ¡£

  Ϊʲô²»ÎªËü´´½¨Ò»¸ö¼òµ¥µÄ½Å±¾³ÌÐòÄØ£¿
  ÒòΪÎÒÏëÈÃËüÔÚNetscapeºÍIE϶¼Äܹ»Í¬ÑùµÄ¹¤×÷£¬ÎÒ¿ªÊ¼Ñ§Ï°Javascript¡£

  ½â¾ö

  ====================================

  Ö÷Ìâ˼ÏëºÜ¼òµ¥£º´´½¨Ò»¸öÒ³Ãæ²¢²âÊÔһϽñÌìÕâ¸öÒ³ÃæÊÇ·ñÒѱ»¼ÓÔعý£¬
  Èç¹ûûÓмÓÔØ£¬ÄǾÍͨ¹ýËüÁ´½Óµ½http://www.thehungersite.com£¬²¢ÇÒÉèÖÃ
  Õâ¸öÒ³Ãæ×÷Ϊä¯ÀÀÆ÷µÄÖ÷Ò³¡£

  »ñÈ¡Ò³Ãæ²¢Öض¨ÏòÊǺÜÈÝÒ׵ģ¬ÎÊÌâÊÇÈçºÎ¼ÇÒäÕâ¸öÒ³ÃæÒѱ»·ÃÎʹý¡£

  ÒòΪJavascriptûÓÐÎļþ·ÃÎʵŦÄÜ£¬¿´À´ÎÒÃÇÖ»ÄÜʹÓÃcookiesÁË¡£

  CookiesÊÇÒ»¸öÓдóСÏÞÖƵıäÁ¿£¬ËüÓëÒ»¸ö·þÎñÆ÷µÄÓòÃûÏà¹ØÁª£¬
  ĬÈÏÇé¿öÏÂcookieµÄÉú´æÆÚÊǵ±ä¯ÀÀÆ÷¹Ø±Õʱ±»Çå¿Õ£¨×¢Ò⣺²»Êǵ±ÄãÀ뿪
  Õâ¸öÒ³ÃæµÄʱºò£©£¬µ«¿ÉÒÔÓÃÒ»¸ö½Å±¾³ÌÐò¸Ä±äÕâÖÖÇé¿ö£¬
  ÔÚÓû§¹Ø±Õä¯ÀÀÆ÷ºóʹcookiesÄܹ»´æ´¢ÏÂÀ´£¬NetscapeÔÚÎļþÖÐʹÓÃËùÓеÄ
  Cookie£¬¶øIE·Ö±ð´æ´¢Ã¿¸öcookie¡£´ËÍ⣬²»Í¬µÄä¯ÀÀÆ÷»á´øÀ´Ò»Ð©ÒâÏë
  ²»µ½µÄÇé¿ö£¬Äã±ØÐëÈ·¶¨Ò»¸öÓû§ÔÚËüµÄä¯ÀÀÆ÷ÉèÖÃÖÐÊÇ·ñ¹Ø±ÕÁËcookies¡£

  Ò»Çж¼ºÜºÃÒ²ºÜÃֻÊÇÏÖÔÚÎÒ»¹Î´ÔÚIEÖвâÊÔËü£¬µ÷ÓÃʾÀýJavascriptÓï¾ä£º
  cookieExpires = "01-APR-" + nLyear + " GMT";
  document.cookies = cookieName + "=" + cookieValue + ";  expires=" + cookieExpires;

  È»ºóµ÷ÓÃ
  document.write(document.cookie);

  document.cookieÊǿյġ£

  ÔÚÊÔÑéºÍÑо¿ÁËÒ»ÏÂÉÏÃæµÄʾÀý³ÌÐòºó£¬»á·¢ÏÖ£º

  1¡£Äã²»Äܹ»¶ÁºÍÏÔʾcookie¡£Èç¹ûÄãÏë¿´Ò»ÏÂÕâ¸öcookieÄãÐèÒªÖ¸¶¨ÓëËüÏàͬµÄÁíÒ»¸ö×Ö·û´®±äÁ¿£¬ÈçÏ£º
   document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
   myvar = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
   document.write(myvar);

  2. ä¯ÀÀÆ÷ÓÃÁ˲»Í¬µÄÈÕÆÚ¸ñʽ£º
    NetscapeʹÓÃ"GMT"½áÊø£¬IEʹÓá°UTC"£¬ÕâÊÇÒòΪËü¿ÉÒÔ¸üºÃµÄ¹¹½¨Ò»¸öÈÕÆÚ£¬ÏóÏÂÃæÕâÑù£º
      var expdate = new Date()
   cookieExpires.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)) //+1 day
   cookieExpires.toGMTString()

   µ±ÄãÏÔʾÈÕÆÚ²¿·Ö
   document.write(expdate.getYear() + "<br>" + expdate.getMonth() + "<br>" + expdate.getDate());
   ¶ÔÓÚ2000-11-15µÄÈÕÆÚ£¬ÔÚIEÖÐÏÔʾΪ2000/10/15£¬ÔÚNetscapeÏÂÏÔʾ100/10/15£¨×¢£ºÒÑÈ·¶¨ÊÇһЩ½ÏµÍ°æ±¾µÄ
   Netscape ä¯ÀÀÆ÷µÄY2KÎÊÌ⣩

   ʾÀýÖп´µ½ÏñÏÂÃæÕâÑùµÄ²¿·Ö£º
   if (platform == "Mac") {
   lastVisit = lastVisit - (24 * 60 * 60 * 1000)
   }
   µ«ÎÒ²»¿ÉÄܼì²âËü¡£

  ÈÕÆÚ¶ÔÏóÓÐgetDateºÍgetDayµÄ·½·¨£¬µÚ¶þ¸ö·½·¨·µ»ØÔÚÒ»ÖÜÖÐÌìµÄË÷ÒýºÅ¡£

----------------------------------------
  ÖªµÀÁËÕâЩ£¬»ù±¾ÉϾÍûÎÊÌâÁË£¨ÏÖÔÚÄã¿ÉÒÔ¿´Ò»ÏÂhomepage.htm)

£ÛÒëÕß×¢£º½«±¾ÎĵÄ×îºóʾÀý´úÂ뿽ÖÁÎı¾ÎļþÖв¢±£´æhtm¸ñʽ£¬È»ºóÔËÐÐ]

  ×îºóҪ˵Ã÷µÄÊÇ£¬Õâ²»½öÊÇÒ»¸öרÓõÄJS½Å±¾£¬Èç¹ûÄãÏ뽫ËüÓÃÔÚÄãµÄwebÒ³ÃæÉÏÄã±ØÐë×îС³Ì¶ÈµÄʹÓò»Í¬µÄä¯ÀÀÆ÷²âÊÔËü²¢×¢ÒâËüÃǵİ汾£¬Ðí¶àµÄ½Å±¾³ÌÐò°üº¬ÁËä¯ÀÀÆ÷ÀàÐͼì²âºÍ´óÁ¿µÄif...elseÓï¾ä£¬ÒÔ´¦ÀíÕâÑù²»Í¬¡£

  ʾÀýÒ³Ãæhomepage.htmÔ´´úÂë

  <html>
  <head>
  <title>Homepage</title>
  </head>
  <body>
  <a href='http://www.thehungersite.com/'>Manual redirection<br></a>
  <a href="javascript:ResetCookie()">Cookie reset</a>

  <script language="JavaScript">
  <!--
  var bVisitedToday = false;

  var lastVisit = GetCookie("lastVisit");
  if (lastVisit != null)
  {
  lastVisit = 1 * lastVisit;
  var lastHere = new Date(lastVisit); 
  var rightNow = new Date();

  if(lastHere.getYear() == rightNow.getYear()
     && lastHere.getMonth() == rightNow.getMonth()
     && lastHere.getDate() == rightNow.getDate())
  {
     bVisitedToday = true;
    }
  }

  if(bVisitedToday == false)
  {
  setLastlastVisitCookie();
  window.location="http://www.thehungersite.com/"
  }
  else
  {
  //window.location="about:blank"
  }

  function getCookieVal (offset)
  {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
  }
  function GetCookie (name) 
  {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
  }
  function SetCookie (name, value)
  {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
  }
  function setLastlastVisitCookie ()
  {
  var rightNow = new Date();
  var expdate = new Date();
  expdate.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)); //+1 day
  SetCookie ("lastVisit", rightNow.getTime(), expdate, "/");
  }
  function ResetCookie()
  {
    SetCookie("lastVisit", 0, null, "/");
  }
  // -->
  </script>
  </body>
  </html>

 

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • ³õѧJavascriptÖ®cookieƪ(Òë)_javascript½Ì³Ì½Ì³Ì

    ³õѧJavascriptÖ®cookieƪ(Òë)_javascript½Ì³Ì½Ì³Ì

    2015-10-09 12:16

ÍøÓѵãÆÀ
×