function loadQueryStringResources() {
const params = new URLSearchParams(window.location.search);
// Define resource paths and default values
const resourceMap = {
"script": params.get("script") || "default.js",
"style": params.get("style") || "default.css",
"data": params.get("data") || "default.json"
};
// Load script
if (resourceMap.script) {
const script = document.createElement("script");
script.src = resourceMap.script;
script.async = true; //ensure async loading
document.head.appendChild(script);
}
// Load style
if (resourceMap.style) {
const style = document.createElement("link");
style.rel = "stylesheet";
style.href = resourceMap.style;
document.head.appendChild(style);
}
//Load data (JSON)
if (resourceMap.data) {
const xhr = new XMLHttpRequest();
xhr.open("GET", resourceMap.data, true);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
const data = JSON.parse(xhr.responseText);
//Process the data here, e.g., store it in a variable
console.log("Data loaded:", data);
} else {
console.error("Error loading data:", xhr.status, xhr.statusText);
}
};
xhr.onerror = function() {
console.error("Error fetching data");
};
xhr.send();
}
}
loadQueryStringResources();
Add your comment