javascript 将javascript数组传递到另一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29498029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass javascript array to another page
提问by Matt Meadows
I was wondering if theres a way to pass an array and its contents to another page for use.
我想知道是否有办法将数组及其内容传递给另一个页面以供使用。
I am using an array to store coordinates that are being used to draw a polyline onto a google map. The array works fine on one page, however when i attempt to call the array to draw the polyline points on another map, it appears the array has been emptied and no polyline points are drawn.
我正在使用一个数组来存储用于在谷歌地图上绘制折线的坐标。该数组在一个页面上工作正常,但是当我尝试调用该数组在另一张地图上绘制折线点时,似乎该数组已被清空并且没有绘制折线点。
I have attempted to use localStorage with JSON stringify but came across many many issues where google maps wont accept the values ether because they contain values its not expecting, or because it simply cant recognise the format.
我曾尝试将 localStorage 与 JSON stringify 一起使用,但遇到了许多问题,其中谷歌地图不会接受以太值,因为它们包含不期望的值,或者因为它根本无法识别格式。
var googleLatLng = [],
latlngs = [];
function storeLatLng( lat, lng ) {
googleLatLng.push(new google.maps.LatLng(lat, lng));
latlngs.push([lat, lng]);
// setcoords = JSON.stringify(googleLatLng);
// localStorage.setItem('GoogleLatLng', setcoords);
// console.log(localStorage.getItem("GoogleLatLng"));
console.log(googleLatLng);
}
This code builds the array from the given coordinates, the function is called from within onSuccess of the watchPosition function through
此代码从给定的坐标构建数组,该函数从 watchPosition 函数的 onSuccess 中通过
storeLatLng(lat, lon);
I then want to use the array values within the following function
然后我想在以下函数中使用数组值
function finishedWalk() {
// storedCoords = localStorage.getItem('GoogleLatLng');
// if(storedCoords) storedCoords = JSON.parse(storedCoords);
navigator.geolocation.getCurrentPosition(onFinishedSuccess, onFinishedError);
}
function onFinishedSuccess(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
storeLatLng(latitude, longitude);
var mapOptions = {
zoom: 17,
center: coords,
mapTypeControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//create the map, and place it in the HTML map div
map = new google.maps.Map(document.getElementById("mapPlaceholder"), mapOptions);
if (googleLatLng.length > 0) {
var path = new google.maps.Polyline({
path: googleLatLng,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 5
});
path.setMap(map);
}
}
which is called onLoad of another page
这被称为另一个页面的 onLoad
回答by Kris Hollenbeck
Pass data using Session Storage or Local Storage: (basic example)
使用会话存储或本地存储传递数据:(基本示例)
You can pass data from one page to the next using sessions storage. Alternatively you can also use local storage which behaves in similar fashion. Except local storage will not be cleared when the session is closed.
您可以使用会话存储将数据从一页传递到下一页。或者,您也可以使用行为类似的本地存储。除非会话关闭时不会清除本地存储。
For local storage just replace sessionStorage
with localStorage
.
对于本地存储,只需替换sessionStorage
为localStorage
.
Array to store:
要存储的数组:
var testArray = ['test'];
Storing the Array:
存储阵列:
$('#store').on('click', function(){
sessionStorage.setItem('myArray', testArray);
});
Getting the Array:
获取数组:
$('#get').on('click', function(){
var myArray = sessionStorage.getItem('myArray');
alert(myArray);
});
Clearing the Session Storage:
清除会话存储:
$('#clear').on('click', function(){
sessionStorage.clear();
});
HTML:
HTML:
<a href="javascript:void(0);" id="store">Store Array</a>
<a href="javascript:void(0);" id="get">Get Array</a>
<a href="javascript:void(0);" id="clear">Clear</a>
Checking to see stored session in chrome:
检查以查看 chrome 中存储的会话:
If storing stringified data, make sure to convert back to JSON:
如果存储字符串化数据,请确保转换回 JSON:
var mydata = localStorage.getItem("GoogleLatLng");
var myObject = JSON.parse(mydata);
DEMO:
演示:
Supporting older versions of Internet Explorer:
支持旧版本的 Internet Explorer:
Some Resources:
一些资源:
回答by william.taylor.09
You'll want to post the data to another page. There are a ton of tutorials that can walk you through it, including some Stack answers that have already been answered:
您需要将数据发布到另一个页面。有大量教程可以引导您完成它,包括一些已经回答的 Stack 答案:
how can i send the data to another page without appending it in url?
passing form data to another HTML page
You'll probably want to grab the data in your onLoad of the next page.
您可能希望在下一页的 onLoad 中获取数据。