1. function loadQueryStringResources() {
  2. const params = new URLSearchParams(window.location.search);
  3. // Define resource paths and default values
  4. const resourceMap = {
  5. "script": params.get("script") || "default.js",
  6. "style": params.get("style") || "default.css",
  7. "data": params.get("data") || "default.json"
  8. };
  9. // Load script
  10. if (resourceMap.script) {
  11. const script = document.createElement("script");
  12. script.src = resourceMap.script;
  13. script.async = true; //ensure async loading
  14. document.head.appendChild(script);
  15. }
  16. // Load style
  17. if (resourceMap.style) {
  18. const style = document.createElement("link");
  19. style.rel = "stylesheet";
  20. style.href = resourceMap.style;
  21. document.head.appendChild(style);
  22. }
  23. //Load data (JSON)
  24. if (resourceMap.data) {
  25. const xhr = new XMLHttpRequest();
  26. xhr.open("GET", resourceMap.data, true);
  27. xhr.onload = function() {
  28. if (xhr.status >= 200 && xhr.status < 300) {
  29. const data = JSON.parse(xhr.responseText);
  30. //Process the data here, e.g., store it in a variable
  31. console.log("Data loaded:", data);
  32. } else {
  33. console.error("Error loading data:", xhr.status, xhr.statusText);
  34. }
  35. };
  36. xhr.onerror = function() {
  37. console.error("Error fetching data");
  38. };
  39. xhr.send();
  40. }
  41. }
  42. loadQueryStringResources();

Add your comment