From 6086cedf2b8f73d272119139031d476fc0f7312b Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 12 Jan 2024 22:00:08 +0200 Subject: [PATCH] Use XHR to load HTML templates --- public/script.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/public/script.js b/public/script.js index 7ac39b3b7..3251c47fc 100644 --- a/public/script.js +++ b/public/script.js @@ -533,13 +533,22 @@ $(document).ajaxError(function myErrorHandler(_, xhr) { } }); -function getUrlSync(url, cache = true) { - return $.ajax({ - type: 'GET', - url: url, - cache: cache, - async: false, - }).responseText; +/** + * Loads a URL content using XMLHttpRequest synchronously. + * @param {string} url URL to load synchronously + * @returns {string} Response text + */ +function getUrlSync(url) { + console.debug('Loading URL synchronously', url); + const request = new XMLHttpRequest(); + request.open('GET', url, false); // `false` makes the request synchronous + request.send(); + + if (request.status >= 200 && request.status < 300) { + return request.responseText; + } + + throw new Error(`Error loading ${url}: ${request.status} ${request.statusText}`); } const templateCache = new Map();