';
return;
}
let html = '';
data.forEach(item => {
html += `${escapeHtml(item.name)}`;
});
// Add "View all results" link
html += `View all results`;
suggestionsContainer.innerHTML = html;
})
.catch(() => {
suggestionsContainer.innerHTML = '
Error loading suggestions
';
});
}, 200); // 200ms debounce delay
});
// Hide suggestions when clicking outside
document.addEventListener('click', function(e) {
if (!e.target.closest('.search-wrapper')) {
suggestionsContainer.style.display = 'none';
}
});
// Helper: get site code from URL
function getSiteCode() {
const path = window.location.pathname.split('/');
return path.length > 1 ? path[1] : 'shop';
}
// Helper: escape HTML to prevent XSS
function escapeHtml(unsafe) {
return unsafe.replace(/[&<>"]/g, function(m) {
if (m === '&') return '&';
if (m === '<') return '<';
if (m === '>') return '>';
if (m === '"') return '"';
return m;
});
}
});