简体   繁体   English

javascript将变量传递到另一个页面

[英]javascript pass a variable to another page

Ok, Here is what I want to do. 好的,这就是我想要做的。 I have a very complex database. 我有一个非常复杂的数据库。 I am reading from the database using php and store the data in a variable (Ajax). 我正在使用php从数据库中读取数据并将数据存储在变量(Ajax)中。 Now when I go to another page I loose the variable. 现在,当我转到另一页时,我松开了变量。

Here is what I know (not sure how correct I am): the scope of a variable is the function it is in. If not specified the scope will be the window object (which changes for every page). 这就是我所知道的(不确定我的正确性):变量的范围是其所在的函数。如果未指定,则范围将是窗口对象(每个页面都会改变)。 Is there any higher level than window? 有没有比窗口高的水平了? (something like session in php but in the client side) (类似于会话在PHP中,但在客户端)

There are many way to storage your variable such as: 有很多方法可以存储变量,例如:

  1. Use client cookie 使用客户端Cookie
  2. Use server session or cookie 使用服务器会话或cookie
  3. HTML5 storage HTML5存储
  4. Query string such as ?var1=blah&var2=blah 查询字符串,例如?var1=blah&var2=blah

Maybe you can use cookie. 也许您可以使用cookie。 Actually php sessions also utilize cookie as well. 实际上,php会话也利用cookie。 As far as I know the scope beyong window is cookie. 据我所知,beyong窗口的作用域是cookie。

Or you can use some HTML5 technologies, such as WebStorage , but that may break on some old browsers. 或者,您可以使用某些HTML5技术,例如WebStorage ,但是在某些旧的浏览器上可能会中断。

Normally I would avoid storing state in javascript and instead cache the result on the server. 通常,我会避免将状态存储在javascript中,而是将结果缓存在服务器上。 Some sort of application or user level cache might be recommended in this case, so that the other page may benefit from the db result. 在这种情况下,建议使用某种类型的应用程序或用户级缓存,以便其他页面可以从数据库结果中受益。 If it's per user then I would look into session. 如果是每个用户,那么我将调查会话。 Otherwise it might be better to store it at the application level in some sort of cache 否则,最好将其存储在应用程序级别的某种缓存中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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