Jump to content

ajax script hanging the browser if i add the parsing for two tags, works fine if i don't


Greywacke
 Share

Recommended Posts

hi all,got a bit of a problem here... the following script crashes the browser, only if the parts in bold are included. yet they have to be there... why could this be happening?

			// parse services			var services = xmldoc.getElementsByTagName("service");			for (var x = 0; x < services.length; x++) {				var i = 0;				var arr = new Array();				var aa = 0;				var arr1 = new Array();				var ss = 0;				var arr3 = new Array();				var wr = 0;				var arr5 = new Array();				[b]var pr = 0;				var arr7 = new Array();[/b]				for (var c = 0; c < services[x].childNodes.length; c++) {					var servicedetail = services[x].childNodes[c];					if (servicedetail.childNodes.length > 0) {						if (servicedetail.firstChild.data != null) {							arr[i] = servicedetail.firstChild.data;							i++;						}					} else if (servicedetail.nodeName == "attribute") {						var arr2 = new Array();						for (var a = 0; a < servicedetail.attributes.length; a++) {							arr2[a] = servicedetail.attributes[a].value;						}						arr1[aa] = arr2.join(";;;");						aa++;					} else if (servicedetail.nodeName == "supplier") {						var arr4 = new Array();						for (var s = 0; s < servicedetail.attributes.length; s++) {							arr4[s] = servicedetail.attributes[s].value;						}						arr3[ss] = arr4.join(";;;");						ss++;					} else if (servicedetail.nodeName == "wanted") {						var arr6 = new Array();						for (var w = 0; w < servicedetail.attributes.length; w++) {							arr6[w] = servicedetail.attributes[w].value;						}						arr5[wr] = arr6.join(";;;");						wr++;					}[b] else if (servicedetail.nodeName == "priceattribute") {						var arr8 = new Array();						for (var p = 0; p < servicedetail.attributes.length; w++) {							arr8[p] = servicedetail.attributes[p].value;						}						arr7[pr] = arr8.join(";;;");						pr++;					}[/b]					alert("arrays parsed "+c);				}				arr[i] = arr1.join(";.;");	// service attributes				i++;				arr[i] = arr3.join(";.;");	// service suppliers & regions				i++;				arr[i] = arr5.join(";.;");	// suppliers wanted per region				i++;				[b]arr[i] = arr7.join(";.;");	// price deduction attribute keys				i++;[/b]				alert("arrays joined "+x);				salert(arr.join("\n"));				if (arr[0]) addrecord("list_services", arr);			}

the xml pasted is quite lengthy (285 tags [first 5 with child text and a closing tag], in the <service> tag). the reason the arrays are joined is because this data gets stored in a new option's value attribute. there are only 2 keys at the moment but for the bakkie canopy service there will be 4 price determining keys. an attribute consists of a key and a value. the moment it runs past 0 indexed tag 564 it hangs if i include the bold lines, does not if i don't (it then ignores tag 565 and 566). that's the first <priceattribute> tag with two tag attributes. why would this be happening?

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

here is the abridged xml for this script (some attribute and supplier tags where removed):

<service>			<serviceid>1</serviceid>	<servicedescription>Bakkie Canopy</servicedescription>	<costperlead>20</costperlead>	<duplicatedays>30</duplicatedays>	<showdetails>1</showdetails>	<attribute id="393" description="budget" value="R3,000 to R4,000" />	<attribute id="394" description="budget" value="R4,000 to R5,000" />	<attribute id="395" description="budget" value="R5,000 to R7,500" />	<attribute id="396" description="budget" value="R7,500 to R10,000" />	<attribute id="397" description="budget" value="R10,000 to R12,500" />	<attribute id="398" description="budget" value="R12,500 plus" />	<attribute id="1" description="canopy_req" value="new_white" />	<attribute id="2" description="canopy_req" value="new_colour_coded" />	<attribute id="11" description="canopy_req" value="pre-owned_white" />	<attribute id="12" description="canopy_req" value="pre-owned_colour_coded" />	<attribute id="400" description="canopy_style" value="spacesaver" />	<attribute id="306" description="products_description" value="Meiya" />	<attribute id="401" description="products_description" value="Single" />	<attribute id="402" description="products_description" value="Asiawing" />	<attribute id="403" description="products_description" value="Chana" />	<attribute id="405" description="products_description" value="Fiat" />	<attribute id="286" description="Requirement" value="Pre-owned - White" />	<attribute id="287" description="Requirement" value="Pre-owned - Colour-coded" />	<attribute id="288" description="Requirement" value="New - White" />	<attribute id="289" description="Requirement" value="New - Colour-coded" />	<attribute id="307" description="vehicle_make_model" value="Meiya" />	<attribute id="308" description="vehicle_make_model" value="Asiawing" />	<attribute id="309" description="vehicle_make_model" value="Chana" />	<attribute id="371" description="vehicle_make_model" value="Ford - F250" />	<attribute id="370" description="vehicle_make_model" value="Fiat" />	<attribute id="312" description="vehicle_make_model" value="DFM" />	<attribute id="313" description="vehicle_make_model" value="Kia" />	<attribute id="314" description="vehicle_make_model" value="Fiat - Strada" />	<supplier id="9" name="Andy Cab (Cape Town)" regionid="28" regionname="Western Cape - Cape Town" />	<supplier id="53" name="Bekkers Canopy Centre (Witbank)" regionid="41" regionname="Mpumalanga - Highveld" />	<supplier id="53" name="Bekkers Canopy Centre (Witbank)" regionid="30" regionname="Mpumalanga - Lowveld" />	<supplier id="27" name="Bobaas Canopies (Vereeniging)" regionid="37" regionname="Gauteng - Ekurhuleni Metro" />	<supplier id="27" name="Bobaas Canopies (Vereeniging)" regionid="20" regionname="Gauteng - Joburg Metro" />	<supplier id="28" name="Bucco Canopies (Gauteng)" regionid="37" regionname="Gauteng - Ekurhuleni Metro" />	<supplier id="28" name="Bucco Canopies (Gauteng)" regionid="20" regionname="Gauteng - Joburg Metro" />	<supplier id="28" name="Bucco Canopies (Gauteng)" regionid="24" regionname="Gauteng - Tshwane Metro" />	<supplier id="22" name="Bucco Canopies (Maritzburg)" regionid="40" regionname="KwaZulu Natal - Maritzburg" />	<supplier id="21" name="Bucco Canopies (Pinetown)" regionid="17" regionname="KwaZulu Natal - Durban" />	<supplier id="21" name="Bucco Canopies (Pinetown)" regionid="40" regionname="KwaZulu Natal - Maritzburg" />	<supplier id="45" name="Bucco Canopies (Port Elizabeth)" regionid="43" regionname="Eastern Cape - Buffalo City" />	<supplier id="45" name="Bucco Canopies (Port Elizabeth)" regionid="18" regionname="Eastern Cape - Nelson Mandela" />	<supplier id="39" name="Bush Buck Canopies (Cape Town)" regionid="28" regionname="Western Cape - Cape Town" />	<supplier id="23" name="Canopy and Weca Centre (Centurion)" regionid="37" regionname="Gauteng - Ekurhuleni Metro" />	<supplier id="23" name="Canopy and Weca Centre (Centurion)" regionid="20" regionname="Gauteng - Joburg Metro" />	<supplier id="23" name="Canopy and Weca Centre (Centurion)" regionid="24" regionname="Gauteng - Tshwane Metro" />	<supplier id="38" name="Canopy King (Pinetown)" regionid="17" regionname="KwaZulu Natal - Durban" />	<supplier id="38" name="Canopy King (Pinetown)" regionid="40" regionname="KwaZulu Natal - Maritzburg" />	<supplier id="50" name="Canopy Land (Rustenburg)" regionid="42" regionname="Northwest - Klerksdorp" />	<supplier id="50" name="Canopy Land (Rustenburg)" regionid="42" regionname="Northwest - Klerksdorp" />	<supplier id="19" name="Canopy Workshop (Pinetown)" regionid="17" regionname="KwaZulu Natal - Durban" />	<supplier id="19" name="Canopy Workshop (Pinetown)" regionid="40" regionname="KwaZulu Natal - Maritzburg" />	<supplier id="34" name="Canopyman (George)" regionid="38" regionname="Western Cape - George" />	<supplier id="35" name="Canopymart (Bellville)" regionid="28" regionname="Western Cape - Cape Town" />	<supplier id="47" name="CC Canopies (Ermelo)" regionid="41" regionname="Mpumalanga - Highveld" />	<supplier id="20" name="City Canopies (Durban)" regionid="17" regionname="KwaZulu Natal - Durban" />	<supplier id="20" name="City Canopies (Durban)" regionid="40" regionname="KwaZulu Natal - Maritzburg" />	<supplier id="8" name="Continental Canopies (Cape Town)" regionid="28" regionname="Western Cape - Cape Town" />	<supplier id="55" name="Crestrider Canopies (Bloemfontein)" regionid="34" regionname="Free State - Bloemfontein" />	<supplier id="55" name="Crestrider Canopies (Bloemfontein)" regionid="39" regionname="Free State - Goldfields" />	<supplier id="54" name="Crestrider Canopies (PE)" regionid="43" regionname="Eastern Cape - Buffalo City" />	<supplier id="54" name="Crestrider Canopies (PE)" regionid="18" regionname="Eastern Cape - Nelson Mandela" />	<supplier id="46" name="Faaz Fit Canopy (West Rand)" regionid="20" regionname="Gauteng - Joburg Metro" />	<supplier id="33" name="George Canopies (George)" regionid="38" regionname="Western Cape - George" />	<supplier id="7" name="Just Canopies (Cape Town)" regionid="28" regionname="Western Cape - Cape Town" />	<supplier id="49" name="Millenium Canopies (East Rand)" regionid="37" regionname="Gauteng - Ekurhuleni Metro" />	<supplier id="49" name="Millenium Canopies (East Rand)" regionid="20" regionname="Gauteng - Joburg Metro" />	<supplier id="48" name="Pikkies Canopies (Middelburg)" regionid="41" regionname="Mpumalanga - Highveld" />	<supplier id="52" name="Puma Canopies (Nelspruit)" regionid="30" regionname="Mpumalanga - Lowveld" />	<supplier id="51" name="Rhino Linings (Rustenburg)" regionid="42" regionname="Northwest - Klerksdorp" />	<supplier id="51" name="Rhino Linings (Rustenburg)" regionid="19" regionname="Northwest - Rustenburg" />	<supplier id="36" name="Roamerrand (Cape Town)" regionid="28" regionname="Western Cape - Cape Town" />	<supplier id="43" name="Roamerrand (Polokwane)" regionid="23" regionname="Limpopo - Polokwane" />	<supplier id="44" name="SA Canopy (Polokwane)" regionid="23" regionname="Limpopo - Polokwane" />	<wanted id="43" name="Eastern Cape - Buffalo City" amount="3" />	<wanted id="18" name="Eastern Cape - Nelson Mandela" amount="3" />	<wanted id="34" name="Free State - Bloemfontein" amount="5" />	<wanted id="39" name="Free State - Goldfields" amount="3" />	<wanted id="37" name="Gauteng - Ekurhuleni Metro" amount="5" />	<wanted id="20" name="Gauteng - Joburg Metro" amount="5" />	<wanted id="24" name="Gauteng - Tshwane Metro" amount="5" />	<wanted id="17" name="KwaZulu Natal - Durban" amount="5" />	<wanted id="40" name="KwaZulu Natal - Maritzburg" amount="3" />	<wanted id="23" name="Limpopo - Polokwane" amount="3" />	<wanted id="41" name="Mpumalanga - Highveld" amount="3" />	<wanted id="30" name="Mpumalanga - Lowveld" amount="3" />	<wanted id="35" name="Northern Cape - Kimberley" amount="3" />	<wanted id="28" name="Western Cape - Cape Town" amount="5" />	<wanted id="38" name="Western Cape - George" amount="3" />	[b]<priceattribute keyid="393" keydesc="budget" />	<priceattribute keyid="1" keydesc="canopy_req" />[/b]</service>

the tags in bold are what i need to read, but if i try, the browser hangs.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

defining variables, especially arrays such asvar i = 0;var arr = new Array();var aa = 0;var arr1 = new Array();var ss = 0;var arr3 = new Array();var wr = 0;var arr5 = new Array();var pr = 0;var arr7 = new Array();within a for loop really can't be a good idea, all of these should be outside usually at top. resetting these to 0, yes as in "i = 0;" etc

Edited by dsonesuk
Link to comment
Share on other sites

well they are outside the loops in which they are used, and defined as their starting count of 0 and empty arrays... this is unfortunately not the problem.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

oh my GOD!if you look closely, you will see the for loop for those tags is as follows:

			for (var p = 0; p < servicedetail.attributes.length; w++) {

this causes it to loop forever, "hanging" the browser. the incrementer is all wrong! it should look as follows:

			for (var p = 0; p < servicedetail.attributes.length; p++) {

issue resolved!! :)

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
 Share

×
×
  • Create New...