如何两个页面的JavaScript之间传递数据 [英] How to pass data between two pages with Javascript

查看:157
本文介绍了如何两个页面的JavaScript之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的类似于此的SO其他一些问题,但他们都不是真的是我要找的,所以希望这不会被视为重复。

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.

我有使用jQuery Mobile建立了一个客户端JavaScript / HTML5的Web应用程序。我发现,表现可以说是相当缓慢的,有人认为已经在DOM太多的事情可能是原因。我的应用程序确实有几个数据角色=页面,可以在一个单一的HTML页面膨胀起来的DOM div的。我想我的应用程序分割成若干HTML页面以提高性能,但我想的经验是无缝的用户。这意味着我需要通过我的应用程序中的物理HTML页面的JavaScript之间的变量。

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. 在URL去其他页面使用查询字符串。 - 我不知道我希望我的用户看到什么可以在地址栏中输入一个相当大的和混乱的查询字符串

  2. 使用服务器端的code像ASP.Net或PHP来处理回传数据。 - 我接受这一点,但我真的不知道它是如何工作的。我不希望我的HTML页面转换为ASPX或php文件。我可以有一个简单的服务器端脚本可以嵌入回发数据到一个常规的HTML文件?

  3. 使用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?

推荐答案

本地存储将是很长的路要走,如果你是兼容HTML5。它将存储值,降低了呼叫到任何服务器,直到你实际上是准备更新所有的信息和信息将是present即使在关闭浏览器;使用会话存储或类似这样的JS

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.

您也将被限制在5存储数据兆的(我相信这是标准),但你可能有其他的问题,如果你的表格要求很多信息。 :)

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/

这篇关于如何两个页面的JavaScript之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆