Skip to content

Commit 8d0e6fe

Browse files
hackall360claude
andcommitted
Fix: Execute visitor tracking scripts immediately
Scripts at end of body run after DOMContentLoaded has fired, so the event listener never triggers. Changed to IIFE pattern to execute immediately when script loads. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 424d62f commit 8d0e6fe

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

ai/ai-init.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* ai-init.js - AI page initialization and visitor tracking
33
*/
44

5-
// Load and auto-refresh visitor count for demo page
6-
document.addEventListener('DOMContentLoaded', function() {
5+
(function() {
6+
// Load and auto-refresh visitor count for demo page
77
var countElement = document.getElementById('visitorCount');
88
if (!countElement || typeof VisitorTracking === 'undefined') {
99
return;
@@ -12,9 +12,8 @@ document.addEventListener('DOMContentLoaded', function() {
1212
var currentCount = null;
1313

1414
// Function to fetch and update visitor count
15-
async function updateVisitorCount() {
16-
try {
17-
var count = await VisitorTracking.getVisitorCount('demo');
15+
function updateVisitorCount() {
16+
VisitorTracking.getVisitorCount('demo').then(function(count) {
1817
if (count !== null) {
1918
// Only update if count has changed or is first load
2019
if (currentCount !== count) {
@@ -28,13 +27,13 @@ document.addEventListener('DOMContentLoaded', function() {
2827
currentCount = '0';
2928
}
3029
}
31-
} catch (error) {
30+
}).catch(function(error) {
3231
console.error('Failed to load visitor count:', error);
3332
if (currentCount === null) {
3433
countElement.textContent = '0';
3534
currentCount = '0';
3635
}
37-
}
36+
});
3837
}
3938

4039
// Initial load
@@ -43,4 +42,4 @@ document.addEventListener('DOMContentLoaded', function() {
4342
// Auto-refresh every 5 minutes (300,000 ms)
4443
setInterval(updateVisitorCount, 300000);
4544
console.log('Visitor count auto-refresh enabled (every 5 minutes)');
46-
});
45+
})();

home-init.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* home-init.js - Home page initialization and visitor tracking
33
*/
44

5-
// Load total visitor count
6-
document.addEventListener('DOMContentLoaded', function() {
5+
(function() {
6+
// Load total visitor count
77
var countElement = document.getElementById('totalVisitorCount');
88
if (!countElement || typeof VisitorTracking === 'undefined') {
99
return;
@@ -22,4 +22,4 @@ document.addEventListener('DOMContentLoaded', function() {
2222
console.error('Failed to load total visitor count:', error);
2323
countElement.textContent = '0';
2424
});
25-
});
25+
})();

0 commit comments

Comments
 (0)