Skip to content

Commit d46e949

Browse files
calderbuildclaude
andcommitted
fix(js): 修复 jQuery 异步加载导致的 $ is not defined 错误
- 添加 jqReady() 队列机制等待 jQuery 加载完成 - 将 $(document).ready() 替换为 jqReady() - 确保所有依赖 jQuery 的代码在加载后执行 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a01dca8 commit d46e949

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

_includes/footer.html

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,38 @@
2020
<!-- Critical JavaScript - Load via CDN with proper fallback -->
2121
<script>
2222
(function() {
23+
// Queue for callbacks waiting for jQuery
24+
window._jqReady = [];
25+
window.jqReady = function(fn) {
26+
if (window.jQuery) fn();
27+
else window._jqReady.push(fn);
28+
};
29+
2330
var jq = document.createElement('script');
2431
jq.src = 'https://cdn.jsdelivr.net/npm/jquery@3.6.4/dist/jquery.min.js';
2532
jq.integrity = 'sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=';
2633
jq.crossOrigin = 'anonymous';
27-
jq.onerror = function() {
28-
var fallback = document.createElement('script');
29-
fallback.src = '{{ "/js/jquery.min.js" | prepend: site.baseurl }}';
30-
document.head.appendChild(fallback);
31-
};
32-
document.head.appendChild(jq);
33-
})();
34-
</script>
35-
36-
<!-- Critical: Language Toggle Event Delegation (Must load before user interaction) -->
37-
<script>
38-
// Bind language toggle immediately after jQuery loads
39-
// This prevents race conditions with async script loading
40-
$(document).ready(function() {
34+
jq.onload = function() {
35+
// Execute queued callbacks
36+
window._jqReady.forEach(function(fn) { fn(); });
37+
window._jqReady = [];
38+
// Language toggle
4139
$(document).on('click', '.lang-btn[data-lang]', function(e) {
4240
e.preventDefault();
4341
var lang = $(this).data('lang');
44-
45-
// If switchLanguage is already defined, use it
4642
if (typeof window.switchLanguage === 'function') {
4743
window.switchLanguage(lang);
48-
} else {
49-
// Otherwise, store the click and wait for the script to load
50-
console.log('Language switch requested:', lang, '- waiting for script to load...');
51-
var checkInterval = setInterval(function() {
52-
if (typeof window.switchLanguage === 'function') {
53-
clearInterval(checkInterval);
54-
window.switchLanguage(lang);
55-
console.log('Script loaded, switching language to:', lang);
56-
}
57-
}, 100); // Check every 100ms
58-
59-
// Timeout after 5 seconds
60-
setTimeout(function() {
61-
clearInterval(checkInterval);
62-
}, 5000);
6344
}
6445
});
65-
});
46+
};
47+
jq.onerror = function() {
48+
var fallback = document.createElement('script');
49+
fallback.src = '{{ "/js/jquery.min.js" | prepend: site.baseurl }}';
50+
fallback.onload = jq.onload;
51+
document.head.appendChild(fallback);
52+
};
53+
document.head.appendChild(jq);
54+
})();
6655
</script>
6756

6857
<!-- Non-Critical JavaScript - Load Asynchronously -->
@@ -265,20 +254,17 @@
265254
{% if page.plchart %}
266255
<!-- jquery.tagcloud.js -->
267256
<script>
268-
// https://stackoverflow.com/questions/9975810/make-iframe-automatically-adjust-height-according-to-the-contents-without-using
269257
function resizeIframe(obj) {
270258
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
271259
}
272260

273-
$(document).ready(function () {
261+
jqReady(function () {
274262
var $chart = document.querySelector("#chart");
275-
$chart.onload = function () {
276-
resizeIframe($chart)
263+
if ($chart) {
264+
$chart.onload = function () { resizeIframe($chart); };
265+
window.addEventListener("resize", function() { resizeIframe($chart); });
277266
}
278-
window.addEventListener("resize", () => {
279-
resizeIframe($chart)
280-
});
281-
})
267+
});
282268
</script>
283269
{% endif %}
284270

@@ -472,7 +458,7 @@
472458
}
473459
}
474460

475-
$(document).ready(function () {
461+
jqReady(function () {
476462
var $searchPage = $('.search-page');
477463
var $searchOpen = $('.search-icon');
478464
var $searchClose = $('.search-icon-close');
@@ -491,7 +477,7 @@
491477
$searchClose.on('click', function (e) {
492478
e.preventDefault();
493479
$searchPage.removeClass('search-active');
494-
$body.attr('class', prevClasses); // from closure
480+
$body.attr('class', prevClasses);
495481
});
496482
$searchInput.focus();
497483
}

0 commit comments

Comments
 (0)