1

重定向到另一个页面(在同一个域中)后是否可以保留我的 javascript 全局变量?

我知道我可以使用 html5 localStorage/sessionStorage 或将其存储在服务器会话状态中(稍后再拉)。但是当使用复杂的或更多的全局变量时,我想知道是否有另一种快速的使用方法......

例如:

<html>
   <head>
      <title> first page </title>

      <script type="text/javascript">
          var myGlobal="this is my global variable";
            ...
      </script>
   </head>
   <body>
        ...
       <a href="secondPage.htm" ..> to the second page</a>
   </body>
</html>

在 secondPage.htm 中:

<html>
   <head>
      <title> second page </title>

      <script type="text/javascript">
          alert(myGlobal);    //I want it to be :  "this is my global variable";
            ...
      </script>
   </head>
   <body>
       ...
   </body>
</html>
4

1 回答 1

2

重定向到另一个页面(在同一个域中)后是否可以保留我的 javascript 全局变量?

不。

新页面 = 新的执行环境。

我知道我可以使用 html5 localStorage/sessionStorage 或将其存储在服务器会话状态中(稍后再拉)。但是当使用复杂的或更多的全局变量时,我想知道是否有另一种快速的使用方法......

这些几乎是您唯一的选择。少量数据可以存储在 cookie 中。

于 2013-01-18T11:39:17.793 回答