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

时间:2022-05-18 21:10:05

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. 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.

我有一个使用jQuery Mobile构建的客户端Javascript / HTML5 Web应用程序。我发现性能可能非常慢,并且有人建议在DOM中进行太多操作可能是原因。我的应用程序确实有几个data-role =“page”div,可能会在单个html页面中填充DOM。我正在尝试将我的应用程序拆分为几个html页面以提高性能,但我希望这种体验对用户来说是无缝的。这意味着我需要在我的应用程序中的物理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. - 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. 在url中使用查询字符串转到其他页面。 - 我不确定我是否希望我的用户在地址栏中看到可能是一个相当大且令人困惑的查询字符串。
  3. 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?
  4. 使用ASP.Net或PHP等服务器端代码来处理回发数据。 - 我对此持开放态度,但我不确定它是如何起作用的。我不想将我的html页面转换为aspx或php文件。我可以有一个简单的服务器端脚本,可以将回发数据嵌入到常规的html文件中吗?
  5. 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.
  6. 使用Cookie存储相关数据。 - 我不确定这个是因为我的大多数用户都在可能限制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?

有没有其他方法可以实现这一目标?在这一点上,我倾向于某种服务器端处理。如果这是最好的方法,有人能指出我正确的方向来弄清楚如何做到这一点?

4 个解决方案

#1


1  

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

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

#2


1  

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

如果您符合HTML5标准,本地存储将是一种方法。它将存储值,减少对任何服务器的调用,直到您实际准备好更新所有信息,即使浏览器关闭,信息也会出现;像这样使用会话存储或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/

#3


0  

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

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

#4


0  

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

使用localStorage。 localStorage允许您在浏览器中存储值。

#1


1  

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

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

#2


1  

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

如果您符合HTML5标准,本地存储将是一种方法。它将存储值,减少对任何服务器的调用,直到您实际准备好更新所有信息,即使浏览器关闭,信息也会出现;像这样使用会话存储或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/

#3


0  

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

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

#4


0  

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

使用localStorage。 localStorage允许您在浏览器中存储值。