Advertisement
UrQuan

HW JS2 Pr3.2

Mar 24th, 2015
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //HTML
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. </head>
  7. <body>
  8.  
  9. <div id="txtCDInfo">//Dugmad i to
  10. <button onclick="loadXMLDoc('http://jsbin.com/hugafagiwe/2.js', tableCD)">Get CD info</button>
  11.   <button onclick="loadXMLDoc('http://jsbin.com/hugafagiwe/2.js', averageCD)">Get the average</button>
  12. </div>
  13.  
  14. </body>
  15. </html>
  16.  
  17.  
  18.  
  19. //Javascript
  20.  
  21. /*
  22. IZ http://jsbin.com/hugafagiwe/2.js
  23. Učitavamo novi objekt unutar našeg programa
  24.  
  25. <CD>
  26. <TITLE>Big Willie style</TITLE>
  27. <ARTIST>Will Smith</ARTIST>
  28. <COUNTRY>USA</COUNTRY>
  29. <COMPANY>Columbia</COMPANY>
  30. <PRICE>9.90</PRICE>
  31. <YEAR>1997</YEAR>
  32. */
  33.  
  34. var CD = function(t, a, coun, c, p, y){
  35.   this.title = t;
  36.   this.artist = a;
  37.   this.country = coun;
  38.   this.company = c;
  39.   this.price = p;
  40.   this.year = y;
  41. }
  42.  
  43. CRrr = function(t, a, coun, c, p, y) {
  44.   CD.call(this, t, a, coun, y);
  45. };
  46. CDrr.prototype = Object.create(CD.prototype);
  47. CDrr.prototype.price = 20.00;
  48. CDrr.prototype.company = "Reizdanja RR";
  49. CDrr.prototype.age = function(){
  50.   console.log(2015 - this.year);
  51. }
  52.  
  53. function prebaci(xh){
  54.   x = JSON.parse(xh.responseText);
  55.   arrayObjectsCD = []
  56.   for(var i=0; i< x.CD.length; i++)  
  57.     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));
  58.   return(arrayObjectsCD)
  59. }
  60.  
  61. function tableCD(xh){
  62.   x = JSON.parse(xh.responseText);
  63.   var txt,x,xx,i;
  64.   txt = "<table border='1'><tr><th>Title</th><th>Artist</th></tr>";
  65.   for( i=0; i<x.CD.length; i++ ) {
  66.     txt=txt + "<tr>";
  67.     txt=txt + "<td>" + x.CD[i].TITLE + "</td>";
  68.     txt=txt + "<td>" + xx=x.CD[i].ARTIST + "</td>";
  69.     txt=txt + "</tr>";
  70.   }
  71.   txt=txt + "</table>";
  72.   document.getElementById('txtCDInfo').innerHTML = txt;
  73. }
  74.  
  75. function averageCD(xh){
  76.   x = JSON.parse(xh.responseText);
  77.   var txt,x,xx,i;
  78.   xx=0;
  79.   txt = "Total of all prices: ";
  80.   for( i=0; i<x.CD.length; i++ )
  81.         xx+=parseFloat(x.CD[i].PRICE);
  82.   txt += parseInt(xx*100)/100;
  83.   xx = xx/x.CD.length
  84.   txt += ", and the average: " + parseInt(xx*100)/100;
  85.   document.getElementById('txtCDInfo').innerHTML = txt;
  86. }
  87.  
  88. function loadXMLDoc(url, cb) {
  89.     var xmlhttp;
  90.     xmlhttp = new XMLHttpRequest();
  91.     xmlhttp.onreadystatechange=function() {
  92.         if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) cb(xmlhttp);
  93.     };
  94.    
  95.     xmlhttp.open("GET",url,true);
  96.     xmlhttp.send();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement