2011-03-31 12 views
0
function w_cookie_wid(wid02) { 
    if (! document.cookie) { 
     document.cookie = "w_wid=1;path=/"; 
     if (! document.cookie) { 
     var w_date = new Date(); 
     return (w_date.getSeconds() % 20 + 1); 
     } 
     else return 2 - 1; 
    } 
    var prefix = wid02 + "="; 
    var begin = document.cookie.indexOf("; " + prefix); 
    if (begin == - 1) { 
     begin = document.cookie.indexOf(prefix); 
     if (begin != 0) { 
     return null; 
     } 
    } 
    else { 
     begin += 2; 
    } 
    var end = document.cookie.indexOf(";", begin); 
    if (end == - 1) { 
     end = document.cookie.length; 
    } 
    return unescape(document.cookie.substring(begin + prefix.length, end)); 
} 
+0

どこからtiを取得しましたか? – Neal

+0

笑...面白い構造 – Mika

+5

それはStackOverflowでdownvotesを引き起こす。 – Ender

答えて

4

この関数の目的は、ページCookieからCookie値を取得しようとすることです。クッキーが設定されていない場合、この関数はデフォルト値の"w_wid=1;path=/"を設定し、1を返します。クッキーがサポートされていない場合、1から20までのセミランダム(時間ベース)値が返され、クッキーは設定されません。クッキーが設定されている場合、関数は名前パラメータ(wid02)に対応する値を取得しようとします。名前が見つからない場合、関数はnullを返します。そうでない場合は値が返されます。

ラインで注釈を付け機能、:

function w_cookie_wid(wid02) { 
    //if there are no cookies for this page 
    if (!document.cookie) { 
     //set a cookie value associated with the root 
     document.cookie = "w_wid=1;path=/"; 
     //if there still are no cookies (not supported/allowed) 
     if (!document.cookie) { 
      //make a new date representing the current time 
      var w_date = new Date(); 
      //return a number between 1 and 20 
      //based on the current time 
      return (w_date.getSeconds() % 20 + 1); 
     } 
     //return 1 if the cookie set was successful 
     else return 2 - 1; 
    } 
    //create the name portion for a cookie value 
    var prefix = wid02 + "="; 
    //check to see if that value is already set, 
    //but not as the first item 
    var begin = document.cookie.indexOf("; " + prefix); 
    //if it isn't 
    if (begin == -1) { 
     //check to see if it is set as the first item 
     begin = document.cookie.indexOf(prefix); 
     //if it isn't set (at all) 
     if (begin != 0) { 
      //return a null value 
      return null; 
     } 
    } 
    //if it IS set somewhere 
    else { 
     //set begin to be the index of beginning 
     //of the name/value pair 
     begin += 2; 
    } 
    //get the index of the first semi-colon after 
    //the beginning of the name/value 
    var end = document.cookie.indexOf(";", begin); 
    //if there isn't one 
    if (end == -1) { 
     //set the end index as the length of the cookie(s) 
     end = document.cookie.length; 
    } 
    //return the cookie name/value pair string 
    return unescape(document.cookie.substring(begin + prefix.length, end)); 
} 

もう宿題をやるために私達に聞かないでください。

+0

+1時間をかけて各行を分析します。 –

関連する問題