简体   繁体   English

如何将javascript变量/对象从一页传递到另一页?

[英]How to pass javascript variable/object from one page to another?

I want to pass javascript object/ variable from page to another. 我想将javascript对象/变量从页面传递到另一个。 so that i can use data for some fields, i want this values to be hidden and not visible while i my passing value from one page to another I have already seen many examples of pass via http parameters, i dont want that and also session variables manage on server side, cause nothing has to be manage on sever side for that page. 这样我就可以将数据用于某些字段,我希望这些值隐藏起来并且不可见,而我将值从一页传递到另一页时,我已经看到了许多通过http参数传递的示例,但我不希望这样做,而且还需要会话变量在服务器端进行管理,因为该页面无需在服务器端进行任何管理。

i want to pass value as a hidden field or value to next page. 我想将值作为隐藏字段或值传递给下一页。 how to pass it to new page when i open new page via 当我通过打开新页面时如何将其传递到新页面

window.location="website/nextpage.jsp";

Also a note, i am beginner, so please sorry if the question seems to vague. 另外请注意,我是初学者,所以如果问题不清楚,请对不起。

You can: 您可以:

  • Use hashes in the url by reading the window.location.hash . 通过阅读window.location.hash在URL中使用哈希。 Similar to GET requests, but does not need the server for passing. 与GET请求类似,但不需要服务器进行传递。 However, the parameters are visible in the url. 但是,参数在url中可见。

  • Using cookies . 使用cookie Have JS "plant" the cookies on the previous page and read them on the receiving page. 让JS在前一页上“植入” cookie,并在接收页面上阅读它们。

  • You could use DOM storage as well. 您也可以使用DOM存储 Similar routine as cookies, plant and read. 与cookie,植物和读取类似的例程。

Assuming you do not want to use session variables, cookies or local storage: 假设您不想使用会话变量,cookie或本地存储:

You can not "hide" a parameter so that your website user will not be able to see it. 您不能“隐藏”参数,以便您的网站用户无法看到它。

If you submit data via a POST request - you can use hidden form elements. 如果您通过POST请求提交数据,则可以使用隐藏的表单元素。

<form method="post">
<input type="hidden" name="state" value="{YOUR PARAMETER}" />

If you use window.location - you will have to do it with a GET request 如果您使用window.location-您将必须通过GET请求进行操作

window.location="website/nextpage.jsp/param/{YOUR PARAMETER}";

我不特别了解JSP,但是使用带有POST方法的表单会隐藏(标准)用户的数据。

why dont you use html5's localStorage and sessionStorage for the purpose. 为什么不为此目的使用html5的localStorage和sessionStorage? for more help visit here 如需更多帮助,请访问此处

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

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