简体   繁体   中英

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. But it seems it only works on the client side with 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 . 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. Server controls that HTML, so you can do whatever you want.
  2. Let the client send an XHR request to get data from the server, and then you can manipulate the localStorage object locally.
  3. If you want realtime, use messaging, like WebSockets (Socket.IO or equivalent)

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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