AJAX ilovalari
Ushbu bobda XML, HTTP, DOM va JavaScript’dan foydalanadigan ba’zi HTML ilovalari namoyish etiladi.
Ishlatiladigan XML hujjat
Ushbu bobda "cd_catalog.xml" nomli XML fayldan foydalanamiz.
XML ma’lumotlarini HTML jadvalda ko‘rsatish
Bu misol har bir <CD> elementini aylanib chiqadi va <ARTIST> hamda <TITLE> elementlarining qiymatlarini HTML jadvalda ko‘rsatadi:
Misol
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse:collapse;
}
th, td {
padding: 5px;
}
</style>
</head>
<body>
<button type="button" onclick="loadXMLDoc()">Get my CD collection</button>
<br><br>
<table id="demo"></table>
<script>
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xmlhttp.open("GET", "cd_catalog.xml", true);
xmlhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>Artist</th><th>Title</th></tr>";
var x = xmlDoc.getElementsByTagName("CD");
for (i = 0; i <x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}
</script>
</body>
</html>
O‘zingiz sinab ko‘ring »
JavaScript va XML DOM’dan foydalanish haqida ko‘proq ma’lumot uchun DOM bilan tanishuv bo‘limiga o‘ting.
Birinchi CD’ni HTML div elementida ko‘rsatish
Bu misol birinchi CD elementini id="showCD" bo‘lgan HTML elementida ko‘rsatish uchun funksiyadan foydalanadi:
Misol
displayCD(0);
function displayCD(i) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this, i);
}
};
xmlhttp.open("GET", "cd_catalog.xml", true);
xmlhttp.send();
}
function myFunction(xml, i) {
var xmlDoc = xml.responseXML;
x = xmlDoc.getElementsByTagName("CD");
document.getElementById("showCD").innerHTML =
"Artist: " +
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
"<br>Title: " +
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
"<br>Year: " +
x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue;
}
O‘zingiz sinab ko‘ring »
CD’lar o‘rtasida harakatlanish
Yuqoridagi misolda CD’lar o‘rtasida harakatlanish uchun next() va previous() funksiyalarini qo‘shing:
Misol
function next() {
// display the next CD, unless you are on the last CD
if (i < x.length-1) {
i++;
displayCD(i);
}
}
function previous() {
// display the previous CD, unless you are on the first CD
if (i > 0) {
i--;
displayCD(i);
}
}
O‘zingiz sinab ko‘ring »
CD bosilganda albom ma’lumotlarini ko‘rsatish
Oxirgi misol foydalanuvchi CD’ni bosganda albom ma’lumotlarini qanday ko‘rsatish mumkinligini ko‘rsatadi:
Misol
function displayCD(i) {
document.getElementById("showCD").innerHTML =
"Artist: " +
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
"<br>Title: " +
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
"<br>Year: " +
x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue;
}
O‘zingiz sinab ko‘ring »
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
