Skip to content

Commit e8b212c

Browse files
authored
Create pagination.js
1 parent 696d889 commit e8b212c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

pagination.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Function to parse XML string and extract links
2+
function parseSitemap(xmlString) {
3+
const parser = new DOMParser();
4+
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
5+
const links = xmlDoc.getElementsByTagName('loc');
6+
const linksArray = Array.from(links).map(link => link.innerHTML);
7+
return linksArray;
8+
}
9+
10+
// Function to display links for a specific page
11+
function displayLinks(links, page, pageSize) {
12+
const list = document.getElementById('sitemapLinks');
13+
list.innerHTML = '';
14+
15+
const startIndex = (page - 1) * pageSize;
16+
const endIndex = startIndex + pageSize;
17+
const paginatedLinks = links.slice(startIndex, endIndex);
18+
19+
paginatedLinks.forEach((link, index) => {
20+
const listItem = document.createElement('li');
21+
listItem.textContent = `${startIndex + index + 1}. ${link}`;
22+
list.appendChild(listItem);
23+
});
24+
}
25+
26+
// Function to create pagination buttons
27+
function createPaginationButtons(links, pageSize) {
28+
const pagination = document.getElementById('pagination');
29+
pagination.innerHTML = '';
30+
31+
const totalPages = Math.ceil(links.length / pageSize);
32+
for (let i = 1; i <= totalPages; i++) {
33+
const listItem = document.createElement('li');
34+
listItem.textContent = i;
35+
listItem.addEventListener('click', () => {
36+
displayLinks(links, i, pageSize);
37+
});
38+
pagination.appendChild(listItem);
39+
}
40+
}
41+
42+
// Fetch the sitemap.xml file
43+
const request = new XMLHttpRequest();
44+
request.onreadystatechange = function() {
45+
if (this.readyState === 4 && this.status === 200) {
46+
const sitemapXML = this.responseText;
47+
const links = parseSitemap(sitemapXML);
48+
const pageSize = 10; // Adjust the number of links per page
49+
createPaginationButtons(links, pageSize);
50+
displayLinks(links, 1, pageSize);
51+
} else if (this.readyState === 4) {
52+
console.error('Error fetching sitemap.xml');
53+
}
54+
};
55+
request.open('GET', 'sitemap.xml', true); // Replace with your sitemap file
56+
request.send();

0 commit comments

Comments
 (0)