How to pass data between two pages with Javascript

落花浮王杯 提交于 2019-12-11 23:34:37

问题


I've seen several other questions on SO that are similar to this, but none of them are really what I'm looking for, so hopefully this won't be seen as a duplicate.

I have a client-side Javascript/HTML5 web application built with jQuery Mobile. I am finding that performance can be quite slow and it was suggested that having too much going on in the DOM could be the cause. My app does have several data-role="page" divs that could be bulking up the DOM in a single html page. I'm trying to split my app into several html pages to improve performance, but I want the experience to be seamless for the user. This means I will need to pass Javascript variables between the physical html pages within my app.

So far I've seen the following options in my searching:

  1. Use a query string in the url going to the other pages. - I'm not sure I want my users seeing a what could be a rather large and confusing query string in the address bar.
  2. Use server side code like ASP.Net or PHP to handle postback data. - I'm open to this, but I'm not really sure how it would work. I don't want to convert my html pages to aspx or php files. Could I have a simple server side script that could embed the postback data into a regular html file?
  3. Use Cookies to store relevant data. - I'm not to sure of this one either because the majority of my users are in enterprise environments that may limit cookie usage.

Are there any other methods for accomplishing this? At this point, I'm leaning toward some sort of server side processing. If that is the best method, could someone point me in the right direction for figuring out how to do that?


回答1:


Try out Local Storage or Session Storage http://www.w3schools.com/html/html5_webstorage.asp




回答2:


Local Storage would be a way to go if you are HTML5 compliant. It will store values, reduce the calls to any server until you are actually ready to update all the info and the info will be present even when the browser is closed; use session storage or JS like this

window.onbeforeunload = function() {
  localStorage.removeItem(key);
  return '';
};

if you need to clear local storage of sensitive info on closing the browser.

Remember that anything you pass into local storage will come out as a string so you will need to convert it to the appropriate data type when you get the info out of storage.

You'll also be limited to storing 5 megs of data (I believe that is standard) but you probably have other issues if your form requires that much info. :)

Check these out for more info

http://msdn.microsoft.com/en-us/library/bg142799(v=vs.85).aspx

http://dev.w3.org/html5/webstorage/




回答3:


You could use a POST instead of GET if you're only concern with the GET is the user seeing lengthy querystrings.




回答4:


Use localStorage. localStorage lets you store values in the browser.



来源:https://stackoverflow.com/questions/19933893/how-to-pass-data-between-two-pages-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!