gaoming13

发现 浏览器单个选项卡刷新,页面里的全局变量没有销毁

  •  
  •   gaoming13 · Jul 9, 2021 · 1811 views
    This topic created in 1794 days ago, the information mentioned may be changed or developed.

    试了几个浏览器,全局变量都没有销毁,除非新开选项卡。

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
    </head>
    <body>
      <script>
        if (name === undefined) {
          name = 0;
        } else {
          name++;
          // 选项卡刷新一次,name 累加 1 次
          console.log(name);
        }
      </script>
    </body>
    </html>
    

    这个机制跟 sessionStorage 有点相似,但为啥要这个机制呢,好奇怪😄

    3 replies    2021-07-09 13:16:31 +08:00
    starsky007
        1
    starsky007  
       Jul 9, 2021   ❤️ 1
    google chrome - How to clear JavaScript variables on page reload? - Stack Overflow
    https://stackoverflow.com/questions/33304229/how-to-clear-javascript-variables-on-page-reload
    oott123
        2
    oott123  
       Jul 9, 2021
    这是 name 啊,换个别的试试。。
    Tdy95
        3
    Tdy95  
       Jul 9, 2021 via Android
    window.name tab 页名称,不会销毁
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2895 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 05:45 · PVG 13:45 · LAX 22:45 · JFK 01:45
    ♥ Do have faith in what you're doing.