【问题标题】:how to keep javascript global variable when redirect to another page重定向到另一个页面时如何保留javascript全局变量
【发布时间】:2013-01-18 19:53:52
【问题描述】:

重定向到另一个页面后是否可以保留我的 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>

【问题讨论】:

  • sessionstorage、localstorage 还是 cookie 怎么样?

标签: javascript


【解决方案1】:

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

没有。

新页面 = 新执行环境。

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

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

【讨论】:

    猜你喜欢
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多