简体   繁体   English

将javascript数组传递给另一个页面

[英]Pass javascript array to another page

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的另一页

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: DEMO:

http://jsfiddle.net/mdktpmw2/ http://jsfiddle.net/mdktpmw2/

Supporting older versions of Internet Explorer: 支持旧版Internet Explorer:

Some Resources: 一些资源:

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? 如何将数据发送到另一个页面而不将其附加到URL?

passing form data to another HTML page 将表单数据传递到另一个HTML页面

You'll probably want to grab the data in your onLoad of the next page. 你可能想要在下一页的onLoad中获取数据。

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

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