Skip to content

Commit 63e35df

Browse files
committed
fix jquery not resolving if element is not present
1 parent 1fe6012 commit 63e35df

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

assets/js/main.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,27 @@ const breadcrumbs = window.location.pathname
1313
.replace(".html", "")
1414
.split("-");
1515

16+
/**
17+
* Asynchronously loads an HTML fragment into the given element.
18+
*
19+
* If the element matching `selector` exists, its content will be replaced
20+
* with the contents of the file at `url`. The returned Promise resolves
21+
* when the load succeeds, and rejects if the request fails. If no matching
22+
* element is found, the Promise resolves immediately with no action taken.
23+
*
24+
* @param {string} selector - jQuery selector for the target element.
25+
* @param {string} url - Path or URL of the HTML file to load.
26+
* @returns {Promise<void>} Promise that resolves when the section is loaded.
27+
*/
1628
function loadSection(selector, url) {
1729
return new Promise((resolve, reject) => {
18-
$(selector).load(url, function (response, status) {
30+
const $el = $(selector);
31+
if ($el.length === 0) {
32+
resolve(); // nothing to do
33+
return;
34+
}
35+
36+
$el.load(url, function (response, status) {
1937
if (status === "error") {
2038
reject(new Error(`Failed to load ${url}`));
2139
} else {

0 commit comments

Comments
 (0)