get array value from get method

2019-07-26 06:22发布

问题:

How to get separate values of array in javascript?

in one page:

var c=new Array(a); (eg: a={"1","2"}) window.location="my_details.html?"+ c + "_"; and in my_details.html :

my_details.htm:

var q=window.location.search;     
alert("qqqqqqqqqqqqq " + q);    
var arrayList = (q)? q.substring(1).split("_"):[];       
var list=new Array(arrayList);     
alert("dataaaaaaaaaaaa " +  decodeURIComponent(list)   + "llll " );  

But i am not able to get individual array value like list[0] etc
How to get it?

thanks
Sneha

回答1:

decodeURIComponent() will return you a String; you need to do something like:

var delim = ",",
    c = ["1", "2"];

window.location = "my_details.html?" + c.join(delim);

And then get it back out again:

var q = window.location.search,
    arrayList = (q)? q.substring(1).split("_"):[],       
    list = [arrayList];     
    arr = decodeURIComponent(list).split(delim);

This will use the value of delim as the delimiter to make the Array a String. We can then use the same delimiter to split the String back into an Array. You just need to make sure delim is available in the scope of the second piece of code.