2014-01-23 68 views
0

剛剛開始,我對javascript非常陌生,對任何愚蠢的評論或提前發生的明顯錯誤表示歉意。使用javascript讀取和使用cookie值

這是我目前正在工作:

<div id="currency_select"> 
    <form action="/Default.asp?" method="post" name="CurrencyChoice"> 
     <select onchange="this.form.submit()" name="ER_ID"> 
      <option value="3">EUR</option> 
      <option value="2">GBP</option> 
     </select> 
     <script type="text/javascript"> 
      document.forms['CurrencyChoice'].elements['ER_ID'].value = ''; 
     </script> 
    </form> 
</div> 

我想讀從以下餅乾「ER%5fID」的值,然後在value=''域插入以上。

說實話,我只是在提高損失,因爲我不確定最好的方法是讀取cookie的值,然後將其值插入我想要的位置。

再次爲任何新手錯誤道歉。我不得不在飛行中學習JavaScript,並且必須在幾天前開始。

所以我花了很多時間相當今天試圖找出我需要什麼,我認爲是這樣的:

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); 
     } 
    } 
} 

但是我仍然不確定如何有內相應的結果返回價值領域?

在此先感謝您的幫助。

+0

或者如果你不在乎舊的IE,只需要ER_ID.value =''... – dandavis

+1

google「setCookie」,然後如果需要的話,「getCookie」 – dandavis

+0

如果你在意你可能要考慮使用localStorage關於網站性能... – dandavis

回答

0

對不起,我只是不在意重寫這個,W3Scools tutorial on cookies是相當comprihensive,甚至給你完全完整的功能,讀取和寫入cookie。

這也是一般JS學習的好資源,所以你一定要檢查一下。

的代碼有如下:

function setCookie(cname,cvalue,exdays) 
{ 
var d = new Date(); 
d.setTime(d.getTime()+(exdays*24*60*60*1000)); 
var expires = "expires="+d.toGMTString(); 
document.cookie = cname + "=" + cvalue + "; " + expires; 
} 

function getCookie(cname) 
{ 
var name = cname + "="; 
var ca = document.cookie.split(';'); 
for(var i=0; i<ca.length; i++) 
    { 
    var c = ca[i].trim(); 
    if (c.indexOf(name)==0) return c.substring(name.length,c.length); 
    } 
return ""; 
} 

和用法是:

function checkCookie() 
{ 
var username=getCookie("username"); 
if (username!="") 
    { 
    alert("Welcome again " + username); 
    } 
else 
    { 
    username = prompt("Please enter your name:",""); 
    if (username!="" && username!=null) 
    { 
    setCookie("username",username,365); 
    } 
    } 
} 

但你可以閱讀更多的W3Scools自己的網站。

編輯:因此,這裏是在您的特定情況下所承諾的全功能樣品:

<html> 
    <head> 
     <meta http-equiv="X-UA-Compatible" content="IE=9" /> 
     <title>Cookie Example</title> 
     <script type="text/javascript"> 

     //adds the [String].trim() method to the [String] object if missing 
     if(!String.prototype.trim) { 
      String.prototype.trim = function() { 
      return this.replace(/^\s+|\s+$/g,''); 
      }; 
     } 

      var cookieName = "ER_ID"; 

      function setCookie(cname,cvalue,exdays) 
      { 
       var d = new Date(); 
       d.setTime(d.getTime()+(exdays*24*60*60*1000)); 
       var expires = "expires="+d.toGMTString(); 
       document.cookie = cname + "=" + cvalue + "; " + expires; 
      } 

      function getCookie(cname) 
      { 
       var name = cname + "="; 
       var ca = document.cookie.split(';'); 
       for(var i=0; i<ca.length; i++){ 
        var c = ca[i].trim(); 
        if (c.indexOf(name)==0) return c.substring(name.length,c.length); 
       } 
       return ""; 
      } 

      function setOptionFromCookie() { 

       var select = document.getElementById('ER_ID'), index, value = getCookie(cookieName); 

       index = select.options.length; 
       while(index--) { 
        if(select.options[index].value == value) { 
         break; 
        } 
       } 

       if(index > -1) { 
        select.selectedIndex = index; 
       } else { 
        alert('no such value in selection'); 
       } 
      } 

      function setValue() { 
       var value = document.getElementById('value').value; 
       setCookie(cookieName, value, 365); 
      } 
     </script> 
    </head> 
    <body> 
     The value that will go into the cookie "ER_ID": <input id="value" value="2" /> 
     <br/> 
     <button onclick="setValue()">Set Cookie</button> 
     <button onclick="setOptionFromCookie()">Set Option from cookie</button> 
     <hr /> 
     <div id="currency_select"> 
      <form action="/Default.asp?" method="post" name="CurrencyChoice"> 
       <select onchange="this.form.submit()" id="ER_ID" name="ER_ID"> 
        <option value="1" selected="selected">Select Currency</option> 
        <option value="3">EUR</option> 
        <option value="2">GBP</option> 
       </select> 
       <script type="text/javascript"> 

       </script> 
      </form> 
     </div> 
    </body> 
</html> 

因爲我沒有在我的瀏覽器,比如一個cookie我也做了一個可能性,爲你設置cookie 。但它應該很容易理解。只需首先設置cookie,然後按下「從Cookie設置選項」即可讀取cookie並根據該值設置選項。

+0

我很確定我沒有要求你爲我寫任何東西,也是我花了W3School的最後三天包括這一個的各種JS相關的基礎知識。但是,謝謝指出顯而易見的。 道歉,如果我的知識缺乏是一個煩惱... –

+0

@Zen你誤解,閱讀和寫作餅乾我會給的答案是一樣的在W3Scools(沒有意義重寫存在的東西,我使用相同的我自己),但如果你仍然不知道如何使用它們,那麼我會重新爲你解答你的具體問題。 – Idra

+0

@ZenAz這裏提供的是一個全功能的示例,使用您的HTML(大部分)使用與W3Schools相同的功能,順便說一句,下一次你問什麼時候注意你知道的東西! – Idra