-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.js
More file actions
70 lines (63 loc) · 2.6 KB
/
header.js
File metadata and controls
70 lines (63 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Cache-busting by appending a timestamp to the request URL
const cacheBustedUrl = `/header.html?v=${new Date().getTime()}`;
// Load the header partial with cache-busting
fetch(cacheBustedUrl)
.then(response => response.text())
.then(data => {
document.getElementById('header-placeholder').innerHTML = data;
// Example: Attach event listener to search bar
const searchBar = document.getElementById('search-bar');
if (searchBar) {
searchBar.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
const query = searchBar.value.trim();
console.log(`Search query: ${query}`);
}
});
}
let jsonData = [];
fetch("search.json")
.then(response => {
if (!response.ok) {
throw new Error("Failed to load JSON file");
}
return response.json();
})
.then(data => {
jsonData = data; // Store the JSON data
})
.catch(error => {
console.error("Error loading JSON:", error);
});
// Handle search input
searchBar.addEventListener("keypress", (event) => {
if (event.key === "Enter") {
const query = searchBar.value.trim().toLowerCase(); // User's search query
if (query) {
const results = searchInJson(query, jsonData); // Search function
displaySearchResults(results);
}
}
})
.catch(error => {
console.error('Error loading header:', error);
});
// Search function
function searchInJson(query, data) {
return data.filter(item => item.name.toLowerCase().includes(query));
}
// Display results
function displaySearchResults(results) {
const resultsContainer = document.getElementById("search-results");
resultsContainer.innerHTML = ""; // Clear previous results
if (results.length === 0) {
resultsContainer.innerHTML = "<p>No results found</p>";
return;
}
results.forEach(result => {
const resultItem = document.createElement("div");
resultItem.innerHTML = `<a href="${result.url}">${result.name}</a>`;
resultsContainer.appendChild(resultItem);
});
}
});