简体   繁体   English

如何将数据从一个页面传递到另一个页面html

[英]How to pass data from one page to another page html

I have a page1.html which contains set of data我有一个page1.html包含一组数据

{'key1':'value1','key2':'value2','key3':'value3'}

and a link和一个链接

<a target='_blank' href='page2.html'>Click To Navigate</a>

which will navigate to page2.html .它将导航到page2.html

I would like to ask how can I pass all the data above in page1.html to page2.html so that I can use them in page2.html .我想问一下如何将上面page1.html中的所有数据传递给page2.html以便我可以在page2.html中使用它们。

What I tried to use is localStorage , but I can store only 1 set of data at a time, so if in my page1.html have many set of data, same key but different value they will be overlapped when I store them in localStorage, then I can not pass all these set of data to the page2.html .我尝试使用的是localStorage ,但我一次只能存储一组数据,所以如果在我的page1.html中有很多数据集,相同的键但不同的值,当我将它们存储在 localStorage 中时,它们会重叠,那么我不能将所有这些数据集传递给page2.html

Anyone got an idea?有人有想法吗? Thank you so much太感谢了

You can do it using Javascript, there's no restriction of which kind of object you can pass.您可以使用 Javascript 来完成,您可以传递哪种对象没有限制。 For instance, if you have several key-value objects:例如,如果您有多个键值对象:

var firstData = {
    'key1' : 'value1',
    'key2' : 'value2'
};

and

var secondData = {
    'key1' : 'value3',
    'key2' : 'value4'
};

you could enclose them using a Javascript array:您可以使用 Javascript 数组将它们括起来:

// This is on page1.html
var myData = [ firstData, secondData ];

and pass that array using localStorage.并使用 localStorage 传递该数组。

Javascript on page1.html page1.html 上的 Javascript

// Set the variable
localStorage.setItem( 'objectToPass', myData );

Javascript on page2.html page2.html 上的 Javascript

// Get the variable
var myData = localStorage['objectToPass'];
localStorage.removeItem( 'objectToPass' ); // Clear the localStorage
var firstData = myData[0];
var secondData = myData[1];
alert('firstData: ' + firstData + '\nsecondData: ' + secondData);

If you want to use javascript to do this, you can do the following (explains it really well) - https://www.boutell.com/newfaq/creating/scriptpass.html如果您想使用 javascript 来执行此操作,您可以执行以下操作(解释得非常好) - https://www.boutell.com/newfaq/creating/scriptpass.html

Also, you can use HTML5 to pass objects from a page to another page of your choise.此外,您可以使用 HTML5 将对象从一个页面传递到您选择的另一个页面。 In order to do this, you would create a session like this:为此,您将创建一个这样的会话:

sessionStorage.setItem('key', 'value');

And then to read the session you would do this:然后要阅读会话,您将执行以下操作:

sessionStorage.getItem('key');

This is a really good example on a webpage on how this can be achieved: http://demos.w3avenue.com/html5-unleashed-tips-tricks-and-techniques/sample-09-sessionstorage-demo.html这是关于如何实现这一点的网页上的一个非常好的示例:http: //demos.w3avenue.com/html5-unleashed-tips-tricks-and-techniques/sample-09-sessionstorage-demo.html

You can use the localstorage with JSON method ( JSON.parse ) to parse a JSON string and construct the JavaScript value or object described by the string.您可以使用带有 JSON 方法 ( JSON.parse ) 的 localstorage 来解析 JSON 字符串并构造字符串描述的 JavaScript 值或对象。 The reverse operation is accomplished by the other method ( JSON.stringify ) to convert a JavaScript object or value to a JSON string.反向操作由另一种方法 ( JSON.stringify ) 完成,以将 JavaScript 对象或值转换为 JSON 字符串。

javascript on the first page:第一页上的javascript:

var data = {'key1':'value1','key2':'value2','key3':'value3'};
localStorage.setItem("object_name",JSON.stringify(data));

javascript on the second page. javascript 在第二页上。

var data = localStorage.getItem("object_name");
localStorage.clear(); //clean the localstorage
var value1 = JSON.parse(data).key1;
var value2 = JSON.parse(data).key2;

So now value1 and value2 in the second page contain the value of value1 and value2 of the first page.所以现在第二页的 value1 和 value2 包含了第一页的 value1 和 value2 的值。

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

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