简体   繁体   中英

document.location.href resets global variables

I am writing a game and use different html files but only one javascript file, which is included in every html file. I have my global variables in this file that are necessary for the game run. I have access and can change the variables from all over the html files which I am switching with , but for one thing I use javascript to change the page, because I need to differ either the game is over or a new round starts.

I use

if (...) {document.location.href = "end.html"}
else {document.location.href = "newround.html}

The changing actually works, but the thing is that after this change it resets ALL global variables in the JavaScript file, which is bad because I need some of those in the new rounds.. I cannot find anything helpful on the web, so please help me out, I really need this! :(

Thanks!

What Matt said is right. If you want to preserve state between pages:

  1. Put your changes in the URL parameters
  2. In Cookies
  3. Use the HTML5 local store
  4. Store server side (this may not work since you're serving HTML pages)

您可以将iframe用作游戏容器。

Personally, I had issues with this too. My workaround ended up being that I had to save the changed variables server-side. I utilize a session manager (Redis) to save the last saved variables. When the page directs to "end.html" or "newround.html", recall the last saved variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM