...

View Full Version : How to create JavaScript for loop for my code



ilainef
05-31-2012, 10:38 AM
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>

chump2877
05-31-2012, 11:02 AM
I would store and reference my "variables" inside an object. For example:


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.)

vwphillips
05-31-2012, 01:33 PM
<!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>

ilainef
06-01-2012, 12:15 AM
Thank you, it works great! Your code is way better than what I had ! . :thumbsup:

ilainef
06-08-2012, 07:17 AM
Quick question: How would I go about adding an expiration date to this cookie?

Philip M
06-08-2012, 07:45 AM
Quick question: How would I go about adding an expiration date to this cookie?


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

vwphillips
06-08-2012, 09:37 AM
<!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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum