Jump to content

variable scope


Guest cfican

Recommended Posts

I have various variables as the first lines in my JavaScript. These variables do appear in function calls and subsequent function calls, except the arrays. On the second time through one function, they forget they are arrays, and consider themselves "undefined". What can I do to assure the persistence of arrays throughout the life of the loaded page? The page is not re-loaded.

Link to comment
Share on other sites

please post your code.
<script language="JavaScript" type="text/javascript">document.onclick = pickIt;onerror=handleErrvar txt=""var object = null;var Binc = 0;var Linc = 0;var Dinc = 0;var mealitem = 0;var planDay = 1;var dayArray = new Array();var dayone = new Array();var daytwo = new Array();var daythr = new Array();var dayfor = new Array();var dayfiv = new Array();var daysix = new Array();var daysvn = new Array();function handleErr(msg,url,l){txt="There was an error on this page.\n\n"txt+="Error: " + msg + "\n"txt+="URL: " + url + "\n"txt+="Line: " + l + "\n\n"txt+="Click OK to continue.\n\n"alert(txt)return true}function pickIt(evt) { 	var evt = (evt) ? evt : ((window.event) ? event : null);	var objectID = (evt.target) ? evt.target.id : ((evt.srcElement) ? evt.srcElement.id : null);	if (objectID.indexOf('r')!=-1)  object = document.getElementById(objectID);	if (object) { 	x = parent.TablesFrame.document.getElementById("mealmyForm").elements[0];	y = parent.TablesFrame.document.getElementById("mealmyForm").elements[1];		y.value = planDay;	if (objectID.substr(1,1) == "B") {	Binc++; 	if (Binc < 6) {	x.value = Binc + objectID;	mealitem++;	} else {	Binc = 6;		}	}		if (objectID.substr(1,1) == "L") {	Linc++; 	if (Linc < 6) {	x.value = Linc + objectID;	mealitem++;	} else {	Linc = 6;		}	}		if (objectID.substr(1,1) == "D") {	Dinc++;	if (Dinc < 6) {	x.value = Dinc + objectID;	mealitem++;	} else {	Dinc = 6;		}	}	parent.TablesFrame.document.getElementById("mealmyForm").submit();	return;}}// user has pressed the "next" button function savenclear(LftRt) {	// this function is either activated pushing Next, where no param means next day, or by one of the arrows Lft or Rt// these values will need to be reset depending on the values they may have had , but for now zero em out	Binc = 0;	Linc = 0;	Dinc = 0;	// current day plan gif	// current day plan object 	for (var umero=0; umero < document.getElementById('dayplan').rows.length; umero++ ) {		dayArray[umero] = document.getElementById('dayplan').rows[umero].cells[0].innerHTML;		}switch (planDay) {	case 1:		dayone = dayArray; 		break;	case 2:		daytwo = dayArray; 		break;	case 3:		daythr = dayArray; 		break;	case 4:		dayfor = dayArray; 		break;	case 5:		dayfiv = dayArray; 		break;	case 6:		daysix = dayArray; 		break;	case 7:		daysvn = dayArray; 		break;}dayArray = {};	// go to previous day plan	if (LftRt == "Lft") {		if (planDay != 1) {			planDay--;		} 	} else {		LftRt = "Rt";	}	// go to new day plan day 	if (LftRt == "Rt") { 		if (planDay != 7) {			planDay++;			}	}	for (var numero=0; numero < 18; numero++ ) {		switch (numero) {			case 0: 				var x=document.getElementById('dayplan').rows[numero].cells;				x[0].innerHTML = "<p><strong>~Breakfast~</strong></p>"; 				break;			case 6:				var x=document.getElementById('dayplan').rows[numero].cells;				x[0].innerHTML = "<p><strong>~Lunch~</strong></p>";				break;			case 12:				var x=document.getElementById('dayplan').rows[numero].cells;				x[0].innerHTML = "<p><strong>~Dinner~</strong></p>";				break;			default:				switch (planDay) {					case 1:						if (!dayone.length == 0) {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = dayone[numero];							} else {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = " ";							}						break;					case 2:						if (!daytwo.length == 0) {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = daytwo[numero];							} else {							var x=document.getElementById('dayplan').rows[numero].cells;									x[0].innerHTML = " ";							}						break;					case 3:						if (!daythr.length == 0)  {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = daythr[numero];							} else {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = " ";							}						break;					case 4:						if (!dayone.length == 0) {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = dayfor[numero];							} else {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = " ";							}						break;					case 5:						if (!dayone.length == 0)  {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = dayfiv[numero];							} else {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = " ";							}						break;					case 6:						if (!dayone.length == 0)  {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = daysix[numero];							} else {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = " ";							}						break;					case 7:						if (!dayone.length == 0)  {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = daysvn[numero];							} else {							var x=document.getElementById('dayplan').rows[numero].cells;							x[0].innerHTML = " ";							}				}  // end inner switch				break;			}  // end outer switch  	} // end for		object = document.getElementById("picofday");	switch (planDay) {  // switch TO planday 		case 1:		   object.setAttribute("src","../images/day1.jpg");		   break;		case 2:		   object.setAttribute("src","../images/day2.jpg");		   break;		case 3:		   object.setAttribute("src","../images/day3.jpg");		   break;		case 4:		   object.setAttribute("src","../images/day4.jpg");		   break;		case 5:		   object.setAttribute("src","../images/day5.jpg");		   break;		case 6:		   object.setAttribute("src","../images/day6.jpg");		   break;		case 7:		   object.setAttribute("src","../images/day7.jpg");		   break;	} // end switch	x = parent.TablesFrame.document.getElementById("mealmyForm").elements[0];	y = parent.TablesFrame.document.getElementById("mealmyForm").elements[1];		z = parent.TablesFrame.document.getElementById("mealmyForm").elements[2];		x.value = "";	y.value = "";	z.value = planDay;	parent.TablesFrame.document.getElementById("mealmyForm").submit();		} // end savenclear</script>

Link to comment
Share on other sites

you are clearing the array with this line
dayArray = {};

is this the one that becomes undefined?

No. That array is deliberately reset to an empty array each time through that function. It is the other arrays, which do not become empty, they become undefined.But thanks for looking.
Link to comment
Share on other sites

How can an array, declared at the start of the JS, lose it's value and become undefined? And how can I prevent that from happening. The code in this thread displays the JS I am working with, but it is rather involved, which is why I ask in the general sence. I suppose I could write a mini program and try and reproduce the failure. Is that what I should do?

Link to comment
Share on other sites

I am having trouble following your code, if you can reproduce it on a smaller scale it would be much easier to sort out the trouble.I am stumped. I can see any reason for then to lose there data.I see that you are submiing a form and it appears to be in another frame, correct? Are you positive that is working correctly and the page with this script is not getting reloaded?Are you calling this function from any other pages?

Link to comment
Share on other sites

I see that you are submitting a form and it appears to be in another frame, correct?
Yes. But first, the problem. I solved it, and it works like a charm. What I did wrong was to declare all these arrays, but then later in the code I treat them as though they were objects, by using a object.property syntax. Actually, it was an array.length. That is why the array started behaving like an object. So, I changed this
			 switch (planDay) {					case 1:					  if (!dayone.length == 0) { to this 			 switch (planDay) {					case 1:		   		   if (dayone[numero] != " ") { do stuff.

That was it. As you gathered, this JS is part of a larger app. I use JS on the Presentation frame, and then use mostly PHP on the Hidden frame. The form is given it's input values from the presentation from, but the form exists on the hidden frame. The form is then submitted by an act on the presentation page, and the hidden page reloads and handles the form, doing it's table ins and outs of the tables. Thanks for your interest.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...