• Hey Guest. Check out your NeoGAF Wrapped 2025 results here!

javascript help

Status
Not open for further replies.

7imz

Member
I've been trying to get this javascript to work on the xml document (look below)... but it's just not working properly... what i'm basically trying to do is to only output cameras at a certain price entered by the user, but the script only outputs a proper result only if the value in the first <camerasSold> tag matches the user value (and if it encounters a wrong value in the following tag, it won't display anything after)... other than that, it won't work... any ideas?

-----



function makePriceTable(xmldoc, url){

var table = document.createElement("table");
table.setAttribute("border", "1");
document.body.appendChild(table);

var caption = "Camera Data from " + url;
table.createCaption( ).appendChild(document.createTextNode(caption));
var header = table.createTHead( );
var headerrow = header.insertRow(0);
headerrow.insertCell(0).appendChild(document.createTextNode("Camera ID"));
headerrow.insertCell(1).appendChild(document.createTextNode("Model"));
headerrow.insertCell(2).appendChild(document.createTextNode("UPC"));
headerrow.insertCell(3).appendChild(document.createTextNode("Price"));
headerrow.insertCell(4).appendChild(document.createTextNode("Availability"));

var mCameras = xmldoc.getElementsByTagName("camerasSold");
var uprice = document.getElementById("pricerange").value;

for(var i=0; i < mCameras.length; i++){

var iCamera = mCameras;
var mCamPrice = iCamera.getAttribute("price");

if(uprice == mCamPrice){

var id = iCamera.getAttribute("cid");
var model = iCamera.getAttribute("model");
var upc = iCamera.getAttribute("UPC");
var avail = iCamera.getAttribute("availability");

var row = table.insertRow(i+1);
row.insertCell(0).appendChild(document.createTextNode(id));
row.insertCell(1).appendChild(document.createTextNode(model));
row.insertCell(2).appendChild(document.createTextNode(upc));
row.insertCell(3).appendChild(document.createTextNode(mCamPrice));
row.insertCell(4).appendChild(document.createTextNode(avail));
}
}
}

------

<merchant name="Best Buy" phone="1-866-BEST BUY">
<mURL>www.bestbuy.ca</mURL>
<hoursOfService>monday to friday 9 to 5</hoursOfService>
<deliveryTime>2 to 3 business days</deliveryTime>
<payment>Visa</payment>
<camerasSold cid="ephoto" model="ePhoto CL50" UPC="6984861204" price="2" availability="Yes"></camerasSold>
</merchant>

<merchant name="Amazon" phone="1-800-8amazon">
<mURL>www.amazon.com</mURL>
<hoursOfService>24/7</hoursOfService>
<deliveryTime>3 to 4 business days</deliveryTime>
<payment>Visa, Mastercard, American Express</payment>
<camerasSold cid="ephoto" model="ePhoto CL50" UPC="6984861204" price="1" availability="Yes"></camerasSold>
<camerasSold cid="powershot" model="PowerShot A80" UPC="9764030714" price="2" availability="No"></camerasSold>
<camerasSold cid="finepix" model="FinePix A350" UPC="5439601927" price="1" availability="Yes"></camerasSold>
</merchant>

-----
 
I'd have to see the whole page to know for certain, because what you put in the attachment isn't a wellformed XML document. That document has two roots (merchant). If they were in an enclosing <merchants> tag I might be able to go further.
 
<?xml version="1.0"?>
<!DOCTYPE application SYSTEM "digicam.dtd">

<application>
<camera id="ephoto" model="ePhoto CL50" UPC="6984861204">
<manufacturer>Agfa</manufacturer>
<imgSize>1600 x 1200</imgSize>
<LCDSize>1.8"</LCDSize>
<storageMedia>SM</storageMedia>
<lensDetail>3x</lensDetail>
<warrantyInfo>1 year</warrantyInfo>
<battery>4xAA</battery>
<URL>http://www.steves-digicams.com/cl50.html</URL>
</camera>

<camera id="powershot" model="PowerShot A80" UPC="9764030714">
<manufacturer>Canon</manufacturer>
<imgSize>2272 x 1704</imgSize>
<LCDSize>1.5"</LCDSize>
<storageMedia>CF</storageMedia>
<lensDetail>3x</lensDetail>
<warrantyInfo>2 years</warrantyInfo>
<battery>4xAA</battery>
<URL>http://www.steves-digicams.com/2003_reviews/a80.html</URL>
</camera>

<camera id="exs3" model="EX-S3" UPC="9059330173">
<manufacturer>Casio</manufacturer>
<imgSize>2048 x 1536</imgSize>
<LCDSize>2"</LCDSize>
<storageMedia>SD/MMC</storageMedia>
<lensDetail>36mm</lensDetail>
<warrantyInfo>1 year</warrantyInfo>
<battery>Lithium rechargeable battery</battery>
<URL>http://www.steves-digicams.com/2003_reviews/exs3.html</URL>
</camera>

<camera id="finepix" model="FinePix A350" UPC="5439601927">
<manufacturer>Fuji </manufacturer>
<imgSize>5-Megapixel</imgSize>
<LCDSize>1.7"</LCDSize>
<storageMedia>xD</storageMedia>
<lensDetail>3x</lensDetail>
<warrantyInfo>6 months</warrantyInfo>
<battery>2xAA</battery>
<URL>http://www.steves-digicams.com/2005_reviews/fuji_a350.html</URL>
</camera>

<merchant name="Future Shop" phone=" 1-800-663-2275">
<mURL>www.futureshop.ca</mURL>
<hoursOfService>24/7</hoursOfService>
<deliveryTime>5 to 7 business days</deliveryTime>
<payment>MasterCard</payment>
<camerasSold cid="ephoto" model="ePhoto CL50" UPC="6984861204" price="2" availability="Yes"></camerasSold>
<camerasSold cid="powershot" model="PowerShot A80" UPC="9764030714" price="1" availability="No"></camerasSold>
</merchant>

<merchant name="Best Buy" phone="1-866-BEST BUY">
<mURL>www.bestbuy.ca</mURL>
<hoursOfService>monday to friday 9 to 5</hoursOfService>
<deliveryTime>2 to 3 business days</deliveryTime>
<payment>Visa</payment>
<camerasSold cid="ephoto" model="ePhoto CL50" UPC="6984861204" price="2" availability="Yes"></camerasSold>
</merchant>

<merchant name="Amazon" phone="1-800-8amazon">
<mURL>www.amazon.com</mURL>
<hoursOfService>24/7</hoursOfService>
<deliveryTime>3 to 4 business days</deliveryTime>
<payment>Visa, Mastercard, American Express</payment>
<camerasSold cid="ephoto" model="ePhoto CL50" UPC="6984861204" price="1" availability="Yes"></camerasSold>
<camerasSold cid="powershot" model="PowerShot A80" UPC="9764030714" price="2" availability="No"></camerasSold>
<camerasSold cid="finepix" model="FinePix A350" UPC="5439601927" price="1" availability="Yes"></camerasSold>
</merchant>



</application>
 
var row = table.insertRow(i+1);

As soon as you miss a camera, this is going to be trying to insert a row beyond the end of the table. Not sure what exactly that should do, but you may find it just fails. Try using another variable that actually tracks the number of cameras you've added, or don't specify an index.
 
iapetus said:
As soon as you miss a camera, this is going to be trying to insert a row beyond the end of the table. Not sure what exactly that should do, but you may find it just fails. Try using another variable that actually tracks the number of cameras you've added, or don't specify an index.

Yeah, that was a weirdness, but just assumed that the DOM was going to 'do the right thing'.
 
Phoenix said:
Yeah, that was a weirdness, but just assumed that the DOM was going to 'do the right thing'.

That's never a good plan when it comes to Javascript manipulating DOM. :)
 
Status
Not open for further replies.
Top Bottom