Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool How to create JavaScript for loop for my code

    Does anyone know how i can create a "for loop" in javascript for my code below:

    I want to create a loop so that I don't have to write one by one as I have already done below. My code works is just that writing one by one is tedious work. Please help! Thanks in advance
    <script type="text/javascript">

    var saveString = "";
    var saveString1 = "";
    var saveString2 = "";
    var saveString3 = "";
    var saveString4 = "";
    var saveString5 = "";
    var saveString6 = "";
    var saveString7 = "";
    var saveString8 = "";
    var saveString9 = "";
    var saveString10 = "";
    var saveString11 = "";
    var saveString12 = "";
    var saveString13 = "";
    var saveString14 = "";
    var saveString15 = "";
    var saveString16 = "";
    var saveString17 = "";
    var saveString18 = "";
    var saveString19 = "";
    var saveString20 = "";



    function getCookie(isName){

    var cookieStr = document.cookie;
    var startSlice = cookieStr.indexOf(isName+"=");
    if (startSlice == -1){return false}
    var endSlice = cookieStr.indexOf(";",startSlice+1)
    if (endSlice == -1){endSlice = cookieStr.length}
    var isData = cookieStr.substring(startSlice,endSlice)
    var isValue = isData.substring(isData.indexOf("=")+1,isData.length);
    return isValue;
    }

    function saveForm(){

    var saveString = "";
    var saveString1 = "";
    var saveString2 = "";
    var saveString3 = "";
    var saveString4 = "";
    var saveString5 = "";
    var saveString6 = "";
    var saveString7 = "";
    var saveString8 = "";
    var saveString9 = "";
    var saveString10 = "";
    var saveString11 = "";
    var saveString12 = "";
    var saveString13 = "";
    var saveString14 = "";
    var saveString15 = "";
    var saveString16 = "";
    var saveString17 = "";
    var saveString18 = "";
    var saveString19 = "";
    var saveString20 = "";

    //var nText = document.getElementsByTagName('input');
    //for (i=0; i<nText.length-1; i++ )
    // {saveString += nText[i].value+":"}

    var isIdx = document.forms[0]['report'].selectedIndex;
    saveString = saveString+isIdx;
    document.cookie = "formData="+saveString+"";

    var isIdx1 = document.forms[1]['report'].selectedIndex;
    saveString1 = saveString1+isIdx1;
    document.cookie = "formData1="+saveString1+"";

    var isIdx2 = document.forms[2]['report'].selectedIndex;
    saveString2 = saveString2+isIdx2;
    document.cookie = "formData2="+saveString2+"";

    var isIdx3 = document.forms[3]['report'].selectedIndex;
    saveString3 = saveString3+isIdx3;
    document.cookie = "formData3="+saveString3+"";

    var isIdx4 = document.forms[4]['report'].selectedIndex;
    saveString4 = saveString4+isIdx4;
    document.cookie = "formData4="+saveString4+"";

    var isIdx5 = document.forms[5]['report'].selectedIndex;
    saveString5 = saveString5+isIdx5;
    document.cookie = "formData5="+saveString5+"";

    var isIdx6 = document.forms[6]['report'].selectedIndex;
    saveString6 = saveString6+isIdx6;
    document.cookie = "formData6="+saveString6+"";

    var isIdx7 = document.forms[7]['report'].selectedIndex;
    saveString7 = saveString7+isIdx7;
    document.cookie = "formData7="+saveString7+"";

    var isIdx8 = document.forms[8]['report'].selectedIndex;
    saveString8 = saveString8+isIdx8;
    document.cookie = "formData8="+saveString8+"";

    var isIdx9 = document.forms[9]['report'].selectedIndex;
    saveString9 = saveString9+isIdx9;
    document.cookie = "formData9="+saveString9+"";

    var isIdx10 = document.forms[10]['report'].selectedIndex;
    saveString10 = saveString10+isIdx10;
    document.cookie = "formData10="+saveString10+"";

    var isIdx11 = document.forms[11]['report'].selectedIndex;
    saveString11 = saveString11+isIdx11;
    document.cookie = "formData11="+saveString11+"";

    var isIdx12 = document.forms[12]['report'].selectedIndex;
    saveString12 = saveString12+isIdx12;
    document.cookie = "formData12="+saveString12+"";

    var isIdx13 = document.forms[13]['report'].selectedIndex;
    saveString13 = saveString13+isIdx13;
    document.cookie = "formData13="+saveString13+"";

    var isIdx14 = document.forms[14]['report'].selectedIndex;
    saveString14 = saveString14+isIdx14;
    document.cookie = "formData14="+saveString14+"";

    var isIdx15 = document.forms[15]['report'].selectedIndex;
    saveString15 = saveString15+isIdx15;
    document.cookie = "formData15="+saveString15+"";

    var isIdx16 = document.forms[16]['report'].selectedIndex;
    saveString16 = saveString16+isIdx16;
    document.cookie = "formData16="+saveString16+"";

    var isIdx17 = document.forms[17]['report'].selectedIndex;
    saveString17 = saveString17+isIdx17;
    document.cookie = "formData17="+saveString17+"";

    var isIdx18 = document.forms[18]['report'].selectedIndex;
    saveString18 = saveString18+isIdx18;
    document.cookie = "formData18="+saveString18+"";

    var isIdx19 = document.forms[19]['report'].selectedIndex;
    saveString19 = saveString19+isIdx19;
    document.cookie = "formData19="+saveString19+"";

    var isIdx20 = document.forms[20]['report'].selectedIndex;
    saveString20 = saveString20+isIdx20;
    document.cookie = "formData20="+saveString20+"";

    }

    function restoreValues(){

    var nValues = getCookie('formData');
    var nValues1 = getCookie('formData1');
    var nValues2 = getCookie('formData2');
    var nValues3 = getCookie('formData3');
    var nValues4 = getCookie('formData4');
    var nValues5 = getCookie('formData5');
    var nValues6 = getCookie('formData6');
    var nValues7 = getCookie('formData7');
    var nValues8 = getCookie('formData8');
    var nValues9 = getCookie('formData9');
    var nValues10 = getCookie('formData10');
    var nValues11 = getCookie('formData11');
    var nValues12 = getCookie('formData12');
    var nValues13 = getCookie('formData13');
    var nValues14 = getCookie('formData14');
    var nValues15 = getCookie('formData15');
    var nValues16 = getCookie('formData16');
    var nValues17 = getCookie('formData17');
    var nValues18 = getCookie('formData18');
    var nValues19 = getCookie('formData19');
    var nValues20 = getCookie('formData20');



    var n = 0;
    if (nValues && nValues1 && nValues2 && nValues3 && nValues4 && nValues5 && nValues6 && nValues7 &&
    nValues8 && nValues9 && nValues10 && nValues11 && nValues12 && nValues13 && nValues14 && nValues15
    && nValues16 && nValues17 && nValues18 && nValues19 && nValues20)
    {
    //alert(nValues)
    var isIndex = nValues.substring(nValues.length-1,nValues.length);
    var isIndex1 = nValues1.substring(nValues1.length-1,nValues1.length);
    var isIndex2 = nValues2.substring(nValues2.length-1,nValues2.length);
    var isIndex3 = nValues3.substring(nValues3.length-1,nValues3.length);
    var isIndex4 = nValues4.substring(nValues4.length-1,nValues4.length);
    var isIndex5 = nValues5.substring(nValues5.length-1,nValues5.length);
    var isIndex6 = nValues6.substring(nValues6.length-1,nValues6.length);
    var isIndex7 = nValues7.substring(nValues7.length-1,nValues7.length);
    var isIndex8 = nValues8.substring(nValues8.length-1,nValues8.length);
    var isIndex9 = nValues9.substring(nValues9.length-1,nValues9.length);
    var isIndex10 = nValues10.substring(nValues10.length-1,nValues10.length);
    var isIndex11 = nValues11.substring(nValues11.length-1,nValues11.length);
    var isIndex12 = nValues12.substring(nValues12.length-1,nValues12.length);
    var isIndex13 = nValues13.substring(nValues13.length-1,nValues13.length);
    var isIndex14 = nValues14.substring(nValues14.length-1,nValues14.length);
    var isIndex15 = nValues15.substring(nValues15.length-1,nValues15.length);
    var isIndex16 = nValues16.substring(nValues16.length-1,nValues16.length);
    var isIndex17 = nValues17.substring(nValues17.length-1,nValues17.length);
    var isIndex18 = nValues18.substring(nValues18.length-1,nValues18.length);
    var isIndex19 = nValues19.substring(nValues19.length-1,nValues19.length);
    var isIndex20 = nValues20.substring(nValues20.length-1,nValues20.length);
    document.forms[0]['report'].selectedIndex = isIndex;
    document.forms[1]['report'].selectedIndex = isIndex1;
    document.forms[2]['report'].selectedIndex = isIndex2;
    document.forms[3]['report'].selectedIndex = isIndex3;
    document.forms[4]['report'].selectedIndex = isIndex4;
    document.forms[5]['report'].selectedIndex = isIndex5;
    document.forms[6]['report'].selectedIndex = isIndex6;
    document.forms[7]['report'].selectedIndex = isIndex7;
    document.forms[8]['report'].selectedIndex = isIndex8;
    document.forms[9]['report'].selectedIndex = isIndex9;
    document.forms[10]['report'].selectedIndex = isIndex10;
    document.forms[11]['report'].selectedIndex = isIndex11;
    document.forms[12]['report'].selectedIndex = isIndex12;
    document.forms[13]['report'].selectedIndex = isIndex13;
    document.forms[14]['report'].selectedIndex = isIndex14;
    document.forms[15]['report'].selectedIndex = isIndex15;
    document.forms[16]['report'].selectedIndex = isIndex16;
    document.forms[17]['report'].selectedIndex = isIndex17;
    document.forms[18]['report'].selectedIndex = isIndex18;
    document.forms[19]['report'].selectedIndex = isIndex19;
    document.forms[20]['report'].selectedIndex = isIndex20;
    }
    }

    onload=restoreValues;
    onbeforeunload=saveForm;

    </script>

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,764
    Thanks
    19
    Thanked 155 Times in 146 Posts
    I would store and reference my "variables" inside an object. For example:

    Code:
    var someObject = {};
    for (var i=0; i<someNumber; i++)
    {
        someObject['saveString'+i] = "";
    }
    JS objects can use dot or bracket notation, so you can create your dynamic object properties with bracket notation. (And, similarly, manipulate existing dynamic properties that way as well.)
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • Users who have thanked chump2877 for this post:

    ilainef (06-01-2012)

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,495
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script type="text/javascript">
    <!--
    
    function cookie(nme){
     var re=new RegExp(nme+'[^;]+','i');
     if (document.cookie.match(re)){
      return document.cookie.match(re)[0].split("=")[1];
     }
     return null
    }
    
    function setcookie(){
     var frms=document.forms,v='',z0;
     for (var z0=0;z0<frms.length;z0++){
      if (frms[z0]['report']){
       v+=frms[z0]['report'].selectedIndex+'|';
      }
     }
     document.cookie='report='+v;
    }
    
    function getcookie(){
     var frms=document.forms,c=cookie('report'),cnt=0,z0=0;
     if (c){
      c=c.split('|');
      for (var z0=0;z0<frms.length;z0++){
       if (frms[z0]['report']){
        frms[z0]['report'].selectedIndex=c[cnt++];
       }
      }
     }
    }
    
    onload=getcookie;
    onbeforeunload=setcookie;   //  onbeforeunload is unreliable, use onchange
    
    //-->
    </script>
    
    </head>
    
    <body>
     <form >
      <select name="report" onchange="setcookie();">
       <option >1</option>
       <option >2</option>
      </select>
     </form>
    
     <form >
      <select name="report" onchange="setcookie();" >
       <option >1</option>
       <option >2</option>
      </select>
     </form>
    
     <form >
      <select name="report" onchange="setcookie();" >
       <option >1</option>
       <option >2</option>
      </select>
     </form>
    
     <form >
      <select name="report" onchange="setcookie();" >
       <option >1</option>
       <option >2</option>
      </select>
     </form>
    
    
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    ilainef (06-01-2012)

  • #4
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thank you

    Thank you, it works great! Your code is way better than what I had ! .

  • #5
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quick question: How would I go about adding an expiration date to this cookie?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by ilainef View Post
    Quick question: How would I go about adding an expiration date to this cookie?
    Code:
    var date = new Date();   // today
    var days = 365;  // days cookie to persist
    date.setTime(date.getTime() + (days*24*60*60*1000)); 
    var expires = "; expires = "+ date.toGMTString();
    document.cookie = 'report=' + v + expires + "; path=/";      // available throughout the directory
    Last edited by Philip M; 06-08-2012 at 08:34 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,495
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script type="text/javascript">
    <!--
    
    function cookie(nme){
     var re=new RegExp(nme+'[^;]+','i');
     if (document.cookie.match(re)){
      return document.cookie.match(re)[0].split("=")[1];
     }
     return null
    }
    
    var days=10;  // the number of days persistence
    
    function setcookie(){
     var frms=document.forms,v='',z0;
     for (var z0=0;z0<frms.length;z0++){
      if (frms[z0]['report']){
       v+=frms[z0]['report'].selectedIndex+'|';
      }
     }
     document.cookie='report='+v+';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
    }
    
    function getcookie(){
     var frms=document.forms,c=cookie('report'),cnt=0,z0=0;
     if (c){
      c=c.split('|');
      for (var z0=0;z0<frms.length;z0++){
       if (frms[z0]['report']){
        frms[z0]['report'].selectedIndex=c[cnt++];
       }
      }
     }
    }
    
    onload=getcookie;
    onbeforeunload=setcookie;   //  onbeforeunload is unreliable, use onchange
    
    //-->
    </script>
    
    </head>
    
    <body>
     <form >
      <select name="report" onchange="setcookie();">
       <option >1</option>
       <option >2</option>
      </select>
     </form>
    
     <form >
      <select name="report" onchange="setcookie();" >
       <option >1</option>
       <option >2</option>
      </select>
     </form>
    
     <form >
      <select name="report" onchange="setcookie();" >
       <option >1</option>
       <option >2</option>
      </select>
     </form>
    
     <form >
      <select name="report" onchange="setcookie();" >
       <option >1</option>
       <option >2</option>
      </select>
     </form>
    
    
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •