Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //HTML
- <!DOCTYPE html>
- <html>
- <head>
- </head>
- <body>
- <div id="txtCDInfo">//Dugmad i to
- <button onclick="loadXMLDoc('http://jsbin.com/hugafagiwe/2.js', tableCD)">Get CD info</button>
- <button onclick="loadXMLDoc('http://jsbin.com/hugafagiwe/2.js', averageCD)">Get the average</button>
- </div>
- </body>
- </html>
- //Javascript
- /*
- IZ http://jsbin.com/hugafagiwe/2.js
- Učitavamo novi objekt unutar našeg programa
- <CD>
- <TITLE>Big Willie style</TITLE>
- <ARTIST>Will Smith</ARTIST>
- <COUNTRY>USA</COUNTRY>
- <COMPANY>Columbia</COMPANY>
- <PRICE>9.90</PRICE>
- <YEAR>1997</YEAR>
- */
- var CD = function(t, a, coun, c, p, y){
- this.title = t;
- this.artist = a;
- this.country = coun;
- this.company = c;
- this.price = p;
- this.year = y;
- }
- CRrr = function(t, a, coun, c, p, y) {
- CD.call(this, t, a, coun, y);
- };
- CDrr.prototype = Object.create(CD.prototype);
- CDrr.prototype.price = 20.00;
- CDrr.prototype.company = "Reizdanja RR";
- CDrr.prototype.age = function(){
- console.log(2015 - this.year);
- }
- function prebaci(xh){
- x = JSON.parse(xh.responseText);
- arrayObjectsCD = []
- for(var i=0; i< x.CD.length; i++)
- arrayObjectsCD.push(new CD(x.CD[i].TITLE,x.CD[i].ARTIST,x.CD[i].COUNTRY,x.CD[i].COMPANY,x.CD[i].PRICE,x.CD[i].YEAR));
- return(arrayObjectsCD)
- }
- function tableCD(xh){
- x = JSON.parse(xh.responseText);
- var txt,x,xx,i;
- txt = "<table border='1'><tr><th>Title</th><th>Artist</th></tr>";
- for( i=0; i<x.CD.length; i++ ) {
- txt=txt + "<tr>";
- txt=txt + "<td>" + x.CD[i].TITLE + "</td>";
- txt=txt + "<td>" + xx=x.CD[i].ARTIST + "</td>";
- txt=txt + "</tr>";
- }
- txt=txt + "</table>";
- document.getElementById('txtCDInfo').innerHTML = txt;
- }
- function averageCD(xh){
- x = JSON.parse(xh.responseText);
- var txt,x,xx,i;
- xx=0;
- txt = "Total of all prices: ";
- for( i=0; i<x.CD.length; i++ )
- xx+=parseFloat(x.CD[i].PRICE);
- txt += parseInt(xx*100)/100;
- xx = xx/x.CD.length
- txt += ", and the average: " + parseInt(xx*100)/100;
- document.getElementById('txtCDInfo').innerHTML = txt;
- }
- function loadXMLDoc(url, cb) {
- var xmlhttp;
- xmlhttp = new XMLHttpRequest();
- xmlhttp.onreadystatechange=function() {
- if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) cb(xmlhttp);
- };
- xmlhttp.open("GET",url,true);
- xmlhttp.send();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement