简体   繁体   English

在HTML5中,我可以直接从服务器将数据存储到“本地存储”吗?

[英]In HTML5, can I store data to the 'localstorage' directly from server?

I have some large chunk of data that I want to send to the client and allow the user to edit it and send back to server, like a online editor. 我有一些大数据要发送给客户端,并允许用户对其进行编辑并发送回服务器,例如在线编辑器。 I want to leverage the HTML5 local storage feature. 我想利用HTML5本地存储功能。 But it seems it only works on the client side with JS. 但似乎它仅在JS的客户端上有效。

Want I want to do is: 我想要做的是:

sent from server
<localStorage id="mydata">LARGE CHUNK OF DATA</localStorage>

in my JS:
localStorage["mydata"] = "new data"

What I am doing now is: 我现在正在做的是:

sent from server
<div id="mydata" style="display: none;">BLAHHHHHH</div>

localStorage by definition is for storing storage locally . localStorage根据定义用于本地存储存储。 So you absolutely cannot control that from the server side without any sort of communication from client <-> server. 因此,如果没有客户端<->服务器的任何形式的通信,您绝对无法从服务器端进行控制。

Your server should either: 您的服务器应:

  1. When preparing for the HTML5 page, call some JavaScript that has your data. 在准备HTML5页面时,请调用一些包含您的数据的JavaScript。 Server controls that HTML, so you can do whatever you want. 服务器控制该HTML,因此您可以执行任何操作。
  2. Let the client send an XHR request to get data from the server, and then you can manipulate the localStorage object locally. 让客户端发送XHR请求以从服务器获取数据,然后您可以在本地操作localStorage对象。
  3. If you want realtime, use messaging, like WebSockets (Socket.IO or equivalent) 如果需要实时,请使用消息传递,例如WebSockets(Socket.IO或同等版本)

I hope this helps! 我希望这有帮助!

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

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