You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Pages游戏集成localStorage失效:原因排查与修复方案求助

Absolutely fixable! Let's walk through the issues in your code and how to fix them, plus why these problems broke your game.

Critical Syntax Errors That Stopped Everything

Your code has a few invalid syntax mistakes that caused the entire script to fail (which is why the game wouldn’t run and document.write didn’t show anything):

  • Wrong comparison operator: You used !=== instead of !== in the first condition. JavaScript doesn’t have a "triple-not-equal" operator—this is a straight-up syntax error that halts script execution.
  • HTML entities instead of JS operators: In the second block, you wrote && (HTML’s encoded version of &&) and > (encoded >). These aren’t valid JavaScript; the browser treats them as plain text, not logical operators.
  • Missing semicolon: The line localStorage.setItem("highscore", myGameArea.frameNo) lacks a trailing semicolon, which can cause unexpected behavior in some JavaScript environments.

Bonus: The document.write Problem

Even if you fixed the syntax, using document.write after the page finishes loading (like when updating the highscore mid-game) will erase your entire page and replace it with the new text. That’s a terrible fit for a game UI—we’ll fix this by updating an existing element instead.

Fixed Code Example

Here’s the corrected version, plus a safer way to display highscores without breaking your game:

// First, set up a place to display highscores (add <div id="highscore-display"></div> to your HTML)
const highScoreDisplay = document.getElementById('highscore-display');
const savedHighScore = localStorage.getItem("highscore");

// Show initial highscore
if (savedHighScore !== null) {
  highScoreDisplay.textContent = `Your highscore is ${savedHighScore}`;
} else {
  highScoreDisplay.textContent = "Play the game to set a highscore";
}

// Your existing game code goes here...

// Update highscore when needed
if (testhighscore && savedHighScore > myGameArea.frameNo) {
  localStorage.setItem("highscore", myGameArea.frameNo);
  highScoreDisplay.textContent = `Your highscore is now ${localStorage.getItem("highscore")}`;
}

Why Your Original Code Broke

  1. The !=== syntax error threw a JavaScript exception, which stopped the entire script from running. That meant neither the highscore message nor your game code executed.
  2. The HTML entities (&amp;&amp;, &gt;) made the second condition unreadable to JavaScript—even if the first error was fixed, this block would never run as intended.
  3. Using document.write post-page-load would have wiped out your game UI once the highscore updated, which is not what you want for a playable game.

Quick Tips

  • Make sure you add the <div id="highscore-display"></div> element to your HTML where you want the highscore to appear.
  • Double-check that testhighscore is a valid boolean variable in your game code (it needs to be true for the highscore update to trigger).

内容的提问来源于stack exchange,提问作者Coder66

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:11:58