Advertisement
Guest User

cd1.html - Javascript (2)

a guest
Mar 24th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 13.79 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <style>
  5.       p { font-size:30px; }
  6.     </style>
  7.  
  8.     <script type="text/javascript">
  9.       var xmlDoc;
  10.       var pozycja=0;        // wskazuje numer aktualnie wyświetlanego elementu pliku XML
  11.  
  12.       // Funkcja czytająca --plik-- XML
  13.       function loadXML()
  14.       {
  15.         var xml = document.getElementById('xml').innerHTML;
  16.         xmlDoc = new DOMParser().parseFromString(xml, 'text/xml');
  17.  
  18.         showXML();
  19.       }
  20.  
  21.       function showXML()    //Funkcja uruchamiana przy wczytywaniu pliku XML i przy naciskaniu przycisków
  22.       {
  23.       // Do elementów dokumentu XML można się odwoływać poprzez funkcję getElementsByTagName() która zwraza kolekcję elementów
  24.       // Długość kolekcji sprawdza się korzystając z własności length np. xmlDoc.getElementsByTagName("cd") .length zwraca ilość elementów "cd"
  25.       // w pliku podpiętym do obiektu xmlDoc
  26.  
  27.         document.getElementById("intro").innerHTML="My collection of " + xmlDoc.getElementsByTagName("catalog")[0].attributes[0].value;
  28.         document.getElementById("artist").innerHTML=xmlDoc.getElementsByTagName("artist")[pozycja].childNodes[0].nodeValue;
  29.         document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[pozycja].childNodes[0].nodeValue;
  30.         document.getElementById("year").innerHTML=xmlDoc.getElementsByTagName("year")[pozycja].childNodes[0].nodeValue;
  31.        
  32.         //xmlDoc.getElementsByTagName("cd")[pozycja].attributes zwraca kolekcję atrybutów elementu "cd" o numerze kolejnym pozycja
  33.         if (xmlDoc.getElementsByTagName("cd")[pozycja].attributes.length>0)
  34.         {
  35.           if (xmlDoc.getElementsByTagName("cd")[pozycja].attributes.getNamedItem("cool").value=="cool")
  36.           {
  37.             document.getElementById("comm").innerHTML="It is cool!!!"
  38.           }
  39.         }
  40.         else
  41.         {
  42.           document.getElementById("comm").innerHTML=" ";
  43.         }
  44.       }
  45.  
  46.       function nextXML()
  47.       {
  48.         pozycja++;
  49.         if (pozycja>=xmlDoc.getElementsByTagName("cd").length) pozycja=0;
  50.         showXML();
  51.       }
  52.  
  53.       function previousXML()
  54.       {
  55.         pozycja--;
  56.         if (pozycja<0) pozycja=xmlDoc.getElementsByTagName("cd").length-1;
  57.        showXML();
  58.      }
  59.  
  60.    </script>
  61.   </head>
  62.  
  63.   <body onload="loadXML()">
  64.  
  65.     <h1 id="intro"></h1>
  66.     <br/>
  67.     <br/>
  68.  
  69.     <button name="Next" onclick="nextXML()">Next record</button>
  70.     <button name="Previous" onclick="previousXML()">Previous record</button>
  71.  
  72.     <p><b>Artist:</b> <span id="artist"></span></p>
  73.     <p><b>Title:</b> <span id="title"></span></p>
  74.     <p><b>Year:</b> <span id="year"></span></p>
  75.     <p><span id="comm"></span></p>
  76.  
  77.  
  78.     <script id="xml" type="text/xml">
  79.       <catalog type="Cool Music">
  80.         <cd cool="cool">
  81.           <title>Empire Burlesque</title>
  82.           <artist>Bob Dylan</artist>
  83.           <country>USA</country>
  84.           <company>Columbia</company>
  85.           <price>10.90</price>
  86.           <year>1985</year>
  87.         </cd>
  88.         <cd>
  89.           <title>King's Burlesque</title>
  90.           <artist>Bob Dylan</artist>
  91.           <country>USA</country>
  92.           <company>Columbia</company>
  93.           <price>10.90</price>
  94.           <year>1987</year>
  95.         </cd>
  96.         <cd cool="cool">
  97.           <title>Prince's Burlesque</title>
  98.           <artist>Bob Dylan</artist>
  99.           <country>USA</country>
  100.           <company>Columbia</company>
  101.           <price>10.90</price>
  102.           <year>1990</year>
  103.         </cd>
  104.         <cd>
  105.           <title>Hide your heart</title>
  106.           <artist>Bonnie Tyler</artist>
  107.           <country>UK</country>
  108.           <company>CBS Records</company>
  109.           <price>9.90</price>
  110.           <year>1988</year>
  111.         </cd>
  112.         <cd>
  113.           <title>Show your heart</title>
  114.           <artist>Bonnie Tyler</artist>
  115.           <country>UK</country>
  116.           <company>CBS Records</company>
  117.           <price>10.90</price>
  118.           <year>1989</year>
  119.         </cd>
  120.         <cd>
  121.           <title>Hide your stomach</title>
  122.           <artist>Bonnie Tyler</artist>
  123.           <country>UK</country>
  124.           <company>CBS Records</company>
  125.           <price>11.90</price>
  126.           <year>1990</year>
  127.         </cd>
  128.         <cd cool="cool">
  129.           <title>Greatest Hits</title>
  130.           <artist>DollyParton</artist>
  131.           <country>USA</country>
  132.           <company>RCA</company>
  133.           <price>9.90</price>
  134.           <year>1982</year>
  135.         </cd>
  136.         <cd cool="cool">
  137.           <title>Poorest Hits</title>
  138.           <artist>DollyParton</artist>
  139.           <country>USA</country>
  140.           <company>RCA</company>
  141.           <price>9.90</price>
  142.           <year>1986</year>
  143.         </cd>
  144.         <cd>
  145.           <title>Still got the blues</title>
  146.           <artist>Gary Moore</artist>
  147.           <country>UK</country>
  148.           <company>Virgin records</company>
  149.           <price>10.20</price>
  150.           <year>1990</year>
  151.         </cd>
  152.         <cd>
  153.           <title>Still got the saltzesson</title>
  154.           <artist>Gary Moore</artist>
  155.           <country>UK</country>
  156.           <company>Virgin records</company>
  157.           <price>10.20</price>
  158.           <year>1995</year>
  159.         </cd>
  160.         <cd>
  161.           <title>Still got the caschancka</title>
  162.           <artist>Gary Moore</artist>
  163.           <country>UK</country>
  164.           <company>Virgin records</company>
  165.           <price>10.20</price>
  166.           <year>1997</year>
  167.         </cd>
  168.         <cd>
  169.           <title>Still got the smalletz</title>
  170.           <artist>Gary Moore</artist>
  171.           <country>UK</country>
  172.           <company>Virgin records</company>
  173.           <price>10.20</price>
  174.           <year>2000</year>
  175.         </cd>
  176.         <cd>
  177.           <title>Eros</title>
  178.           <artist>Eros Ramazzotti</artist>
  179.           <country>EU</country>
  180.           <company>BMG</company>
  181.           <price>9.90</price>
  182.           <year>1997</year>
  183.         </cd>
  184.         <cd>
  185.           <title>Mars</title>
  186.           <artist>Eros Ramazzotti</artist>
  187.           <country>EU</country>
  188.           <company>BMG</company>
  189.           <price>9.90</price>
  190.           <year>1998</year>
  191.         </cd>
  192.         <cd>
  193.           <title>Venera</title>
  194.           <artist>Eros Ramazzotti</artist>
  195.           <country>EU</country>
  196.           <company>BMG</company>
  197.           <price>9.90</price>
  198.           <year>2000</year>
  199.         </cd>
  200.         <cd>
  201.           <title>One night only</title>
  202.           <artist>Bee Gees</artist>
  203.           <country>UK</country>
  204.           <company>Polydor</company>
  205.           <price>10.90</price>
  206.           <year>1998</year>
  207.         </cd>
  208.         <cd>
  209.           <title>One way only</title>
  210.           <artist>Bee Gees</artist>
  211.           <country>UK</country>
  212.           <company>Polydor</company>
  213.           <price>10.90</price>
  214.           <year>1999</year>
  215.         </cd>
  216.         <cd>
  217.           <title>Last night only</title>
  218.           <artist>Bee Gees</artist>
  219.           <country>UK</country>
  220.           <company>Polydor</company>
  221.           <price>10.90</price>
  222.           <year>2000</year>
  223.         </cd>
  224.         <cd>
  225.           <title>Sylvias Mother</title>
  226.           <artist>Dr.Hook</artist>
  227.           <country>UK</country>
  228.           <company>CBS</company>
  229.           <price>8.10</price>
  230.           <year>1973</year>
  231.         </cd>
  232.         <cd>
  233.           <title>Sylvias Father</title>
  234.           <artist>Dr.Hook</artist>
  235.           <country>UK</country>
  236.           <company>CBS</company>
  237.           <price>8.10</price>
  238.           <year>1975</year>
  239.         </cd>
  240.         <cd>
  241.           <title>Sylvias Little Pet</title>
  242.           <artist>Dr.Hook</artist>
  243.           <country>UK</country>
  244.           <company>CBS</company>
  245.           <price>8.10</price>
  246.           <year>1978</year>
  247.         </cd>
  248.         <cd>
  249.           <title>Maggie May</title>
  250.           <artist>Rod Stewart</artist>
  251.           <country>UK</country>
  252.           <company>Pickwick</company>
  253.           <price>8.50</price>
  254.           <year>1990</year>
  255.         </cd>
  256.         <cd>
  257.           <title>Maggie the Cashaloth</title>
  258.           <artist>Rod Stewart</artist>
  259.           <country>UK</country>
  260.           <company>Pickwick</company>
  261.           <price>8.50</price>
  262.           <year>1990</year>
  263.         </cd>
  264.         <cd cool="cool">
  265.           <title>Romanza</title>
  266.           <artist>Andrea Bocelli</artist>
  267.           <country>EU</country>
  268.           <company>Polydor</company>
  269.           <price>10.80</price>
  270.           <year>1996</year>
  271.         </cd>
  272.         <cd cool="cool">
  273.           <title>Randevoux</title>
  274.           <artist>Andrea Bocelli</artist>
  275.           <country>EU</country>
  276.           <company>Polydor</company>
  277.           <price>10.80</price>
  278.           <year>1998</year>
  279.         </cd>
  280.         <cd>
  281.           <title>When a man loves a woman</title>
  282.           <artist>Percy Sledge</artist>
  283.           <country>USA</country>
  284.           <company>Atlantic</company>
  285.           <price>8.70</price>
  286.           <year>1987</year>
  287.         </cd>
  288.         <cd>
  289.           <title>When a man loves a man</title>
  290.           <artist>Percy Sledge</artist>
  291.           <country>USA</country>
  292.           <company>Atlantic</company>
  293.           <price>8.70</price>
  294.           <year>1988</year>
  295.         </cd>
  296.         <cd>
  297.           <title>When a woman loves a woman</title>
  298.           <artist>Percy Sledge</artist>
  299.           <country>USA</country>
  300.           <company>Atlantic</company>
  301.           <price>8.70</price>
  302.           <year>1989</year>
  303.         </cd>
  304.         <cd>
  305.           <title>Black angel</title>
  306.           <artist>Savage Rose</artist>
  307.           <country>EU</country>
  308.           <company>Mega</company>
  309.           <price>10.90</price>
  310.           <year>1995</year>
  311.         </cd>
  312.         <cd>
  313.           <title>Blue angel</title>
  314.           <artist>Savage Rose</artist>
  315.           <country>EU</country>
  316.           <company>Mega</company>
  317.           <price>10.90</price>
  318.           <year>1997</year>
  319.         </cd>
  320.         <cd>
  321.           <title>RGB(20,130,80) angel</title>
  322.           <artist>Savage Rose</artist>
  323.           <country>EU</country>
  324.           <company>Mega</company>
  325.           <price>10.90</price>
  326.           <year>1999</year>
  327.         </cd>
  328.         <cd>
  329.           <title>1999 Grammy Nominees</title>
  330.           <artist>Many</artist>
  331.           <country>USA</country>
  332.           <company>Grammy</company>
  333.           <price>10.20</price>
  334.           <year>1999</year>
  335.         </cd>
  336.         <cd cool="cool">
  337.           <title>For the good times</title>
  338.           <artist>Kenny Rogers</artist>
  339.           <country>UK</country>
  340.           <company>Mucik Master</company>
  341.           <price>8.70</price>
  342.           <year>1995</year>
  343.         </cd>
  344.         <cd>
  345.           <title>Big Willie style</title>
  346.           <artist>Will Smith</artist>
  347.           <country>USA</country>
  348.           <company>Columbia</company>
  349.           <price>9.90</price>
  350.           <year>1997</year>
  351.         </cd>
  352.         <cd>
  353.           <title>Tupelo Honey</title>
  354.           <artist>Van Morrison</artist>
  355.           <country>UK</country>
  356.           <company>Polydor</company>
  357.           <price>8.20</price>
  358.           <year>1971</year>
  359.         </cd>
  360.         <cd>
  361.           <title>Soulsville</title>
  362.           <artist>JBiorn Hoel</artist>
  363.           <country>Norway</country>
  364.           <company>WEA</company>
  365.           <price>7.90</price>
  366.           <year>1996</year>
  367.         </cd>
  368.         <cd>
  369.           <title>The very best of</title>
  370.           <artist>Cat Stevens</artist>
  371.           <country>UK</country>
  372.           <company>Island</company>
  373.           <price>8.90</price>
  374.           <year>1990</year>
  375.         </cd>
  376.         <cd>
  377.           <title>Stop</title>
  378.           <artist>Sam Brown</artist>
  379.           <country>UK</country>
  380.           <company>A and M</company>
  381.           <price>8.90</price>
  382.           <year>1988</year>
  383.         </cd>
  384.         <cd>
  385.           <title>Bridge of Spies</title>
  386.           <artist>T`Pau</artist>
  387.           <country>UK</country>
  388.           <company>Siren</company>
  389.           <price>7.90</price>
  390.           <year>1987</year>
  391.         </cd>
  392.         <cd>
  393.           <title>Private Dancer</title>
  394.           <artist>Tina Turner</artist>
  395.           <country>UK</country>
  396.           <company>Capitol</company>
  397.           <price>8.90</price>
  398.           <year>1983</year>
  399.         </cd>
  400.         <cd>
  401.           <title>Midt om natten</title>
  402.           <artist>Kim Larsen</artist>
  403.           <country>EU</country>
  404.           <company>Medley</company>
  405.           <price>7.80</price>
  406.           <year>1983</year>
  407.         </cd>
  408.         <cd>
  409.           <title>Pavarotti Gala Concert</title>
  410.           <artist>Luciano Pavarotti</artist>
  411.           <country>UK</country>
  412.           <company>DECCA</company>
  413.           <price>9.90</price>
  414.           <year>1991</year>
  415.         </cd>
  416.         <cd>
  417.           <title>The dock of the bay</title>
  418.           <artist>Otis Redding</artist>
  419.           <country>USA</country>
  420.           <company>Atlantic</company>
  421.           <price>7.90</price>
  422.           <year>1987</year>
  423.         </cd>
  424.         <cd>
  425.           <title>Picture book</title>
  426.           <artist>Simply Red</artist>
  427.           <country>EU</country>
  428.           <company>Elektra</company>
  429.           <price>7.20</price>
  430.           <year>1985</year>
  431.         </cd>
  432.         <cd>
  433.           <title>Red</title>
  434.           <artist>The Communards</artist>
  435.           <country>UK</country>
  436.           <company>London</company>
  437.           <price>7.80</price>
  438.           <year>1987</year>
  439.         </cd>
  440.         <cd>
  441.           <title>Unchain my heart</title>
  442.           <artist>Joe Cocker</artist>
  443.           <country>USA</country>
  444.           <company>EMI</company>
  445.           <price>8.20</price>
  446.           <year>1987</year>
  447.         </cd>
  448.       </catalog>
  449.     </script>
  450.   </body>
  451. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement