location.href pass array and get array in next webPage(location.href传值数组,以及接收数组)
index1.html
<!DOCTYPE html> <html> <head> </head> <body> <button type="button" onclick="buttonAction();">button</button> </body> <script type="text/javascript"> var array = []; array.push(1); array.push(22); array.push(33); function buttonAction(){ document.location.href = "index2.html?array="+array; } </script> </html>
index2.html
注意:这里接收到的array其实是个string型,元素以“,”分隔,所以用split把他重新组合
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <script src="js/common/jquery-1.7.1.min.js"></script> <script src="js/li-common/li-jquery-common-1.2.js"></script> </head> <body> </body> <script type="text/javascript"> var array = LI_getQueryString("array"); array = array.split(","); var length = array.length; for(var i=0;i<length;i++) alert(array[i]); </script> </html>
这里是需要引入我自己封装的一个js库,叫做js-jquery-common-1.2.js。如果你没有的话,那就直接用下面这个方法吧
/* * url?name=wangba 通过这个方法可以取得url所带参数值 * 如url = 192.168.1.106:8080/dw/stores.html?storeId=dwdw123j3n3 var storeId = LI_getQueryString("storeId"); * */ function LI_getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return decodeURI(r[2]); return null; }