简体   繁体   English

在html页面之间共享数据

[英]Share data between html pages

I want to send some data from one HTML page to another. 我想将一些数据从一个HTML页面发送到另一HTML页面。 I am sending the data through the query parameters like http://localhost/project/index.html?status=exist . 我通过类似http://localhost/project/index.html?status=exist的查询参数发送数据。 The problem with this method is that data remains in the URL. 这种方法的问题在于数据保留在URL中。 Is there any other method to send the data across HTML pages using JavaScript or jquery. 是否有其他方法可以使用JavaScript或jquery在HTML页面之间发送数据。

why don't you store your values in HTML5 storage objects such as sessionStorage or localStorage , visit HTML5 Storage Doc to get more details. 为什么不将值存储在诸如sessionStoragelocalStorage类的HTML5存储对象中,请访问HTML5存储文档以获取更多详细信息。 Using this you can store intermediate values temporarily/permanently locally and then access your values later. 使用此功能,您可以在本地临时/永久存储中间值,然后在以后访问您的值。

To store values for a session: 要存储会话的值:

sessionStorage.getItem('label')
sessionStorage.setItem('label', 'value')

or more permanently: 或更永久地:

localStorage.getItem('label')
localStorage.setItem('label', 'value')

So you can store (temporarily) form data between multiple pages using HTML5 storage objects which you can even retain after reload.. 因此,您可以使用HTML5存储对象(临时)在多个页面之间存储表单数据,甚至可以在重新加载后保留这些数据。

I know this is an old post, but figured I'd share my two cents. 我知道这是一篇旧文章,但想想我会分享我的两分钱。 @Neji is correct in that you can use sessionStorage.getItem('label') , and sessionStorage.setItem('label', 'value') (although he had the setItem parameters backwards, not a big deal). @Neji是正确的,因为您可以使用sessionStorage.getItem('label')sessionStorage.setItem('label', 'value') (尽管他的setItem参数是向后的,没什么大不了的)。 I much more prefer the following, I think it's more succinct: 我更喜欢以下内容,我认为它更简洁:

var val = sessionStorage.myValue

in place of getItem and 代替getItem

sessionStorage.myValue = 'value'

in place of setItem . 代替setItem

Also, it should be noted that in order to store JavaScript objects, they must be stringified to set them, and parsed to get them, like so: 另外,应该注意的是,为了存储JavaScript对象,必须对它们进行字符串化以进行设置,并进行解析以获取它们,如下所示:

sessionStorage.myObject = JSON.stringify(myObject); //will set object to the stringified myObject
var myObject = JSON.parse(sessionStorage.myObject); //will parse JSON string back to object

The reason is that sessionStorage stores everything as a string, so if you just say sessionStorage.object = myObject all you get is [object Object], which doesn't help you too much. 原因是sessionStorage将所有内容存储为字符串,因此,如果您只说sessionStorage.object = myObject您得到的只是[object Object],这对您没有太大帮助。

possibly if you want to just transfer data to be used by JavaScript then you can use Hash Tags like this 可能如果您只想传输供JavaScript使用的数据,则可以使用像这样的哈希标签

http://localhost/project/index.html#exist

so once when you are done retriving the data show the message and change the window.location.hash to a suitable value.. now whenever you ll refresh the page the hashtag wont be present 因此,一旦完成检索数据,则显示消息,并将window.location.hash更改为合适的值。现在,每当刷新页面时,将不会显示井号hashtag
NOTE: when you will use this instead ot query strings the data being sent cannot be retrived/read by the server 注意:当您将使用此查询字符串代替ot查询字符串时,服务器无法检索/读取正在发送的数据

Well, you can actually send data via JavaScript - but you should know that this is the #1 exploit source in web pages as it's XSS :) 好吧,您实际上可以通过JavaScript发送数据-但您应该知道这是网页中的#1漏洞利用源,因为它是XSS :)

I personally would suggest to use an HTML formular instead and modify the javascript data on the server side. 我个人建议改用HTML公式,并在服务器端修改javascript数据。

But if you want to share between two pages (I assume they are not both on localhost, because that won't make sense to share between two both-backend-driven pages) you will need to specify the CORS headers to allow the browser to send data to the whitelisted domains. 但是,如果要在两个页面之间共享(我想它们不在本地主机上都共享,因为在两个后端驱动的两个页面之间共享没有意义),则需要指定CORS标头,以允许浏览器将数据发送到列入白名单的域。

These two links might help you, it shows the example via Node backend, but you get the point how it works: 这两个链接可能会对您有所帮助,它通过Node后端显示了示例,但您可以理解它的工作原理:

Link 1 链接1

And, of course, the CORS spec: 而且,当然,CORS规范:

Link 2 连结2

~Cheers 〜干杯

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

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