简体   繁体   English

如何使用Javascript在两个页面之间传递数据

[英]How to pass data between two pages with Javascript

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. 我在SO上看到过几个与此类似的问题,但其中没有一个是我正在寻找的,所以希望这不会被视为重复。

I have a client-side Javascript/HTML5 web application built with jQuery Mobile. 我有一个使用jQuery Mobile构建的客户端Javascript / HTML5 Web应用程序。 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. 我发现性能可能非常慢,并且有人认为在DOM中进行过多的操作可能是原因。 My app does have several data-role="page" divs that could be bulking up the DOM in a single html page. 我的应用程序确实有几个data-role="page" div,可能会在单个html页面中填充DOM。 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. 我正在尝试将我的应用程序拆分为几个html页面以提高性能,但我希望这种体验对用户来说是无缝的。 This means I will need to pass Javascript variables between the physical html pages within my app. 这意味着我需要在我的应用程序中的物理html页面之间传递Javascript变量。

So far I've seen the following options in my searching: 到目前为止,我在搜索中看到了以下选项:

  1. Use a query string in the url going to the other pages. 使用URL中的查询字符串转到其他页面。 - 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. 使用ASP.Net或PHP等服务器端代码来处理回发数据。 - 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. 我不想将我的html页面转换为aspx或php文件。 Could I have a simple server side script that could embed the postback data into a regular html file? 我可以有一个简单的服务器端脚本,可以将回发数据嵌入到常规的html文件中吗?
  3. Use Cookies to store relevant data. 使用Cookie存储相关数据。 - I'm not to sure of this one either because the majority of my users are in enterprise environments that may limit cookie usage. - 我不确定这个是因为我的大多数用户都在可能限制cookie使用的企业环境中。

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? 如果这是最好的方法,有人可以指出我正确的方向来弄清楚如何做到这一点?

试用本地存储或会话存储http://www.w3schools.com/html/html5_webstorage.asp

Local Storage would be a way to go if you are HTML5 compliant. 如果您符合HTML5标准,本地存储将是一种方法。 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 像这样使用会话存储或JS

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. 您还将仅限于存储5兆的数据(我认为这是标准的)但如果您的表单需要很多信息,您可能还有其他问题。 :) :)

Check these out for more info 查看这些以获取更多信息

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

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

如果您只关心GET,那么您可以使用POST而不是GET,即用户看到冗长的查询字符串。

Use localStorage. 使用localStorage。 localStorage lets you store values in the browser. localStorage允许您在浏览器中存储值。

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

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