get array value from get method

2019-07-26 06:40发布

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条回答
smile是对你的礼貌
2楼-- · 2019-07-26 07:21

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.

查看更多
登录 后发表回答