html怎么在location.href = "/views/list.html"后面传一个值过去,并且怎么在另一个页面
用js解析
var goods_first_id = $(this).attr("goods_first_id");
var first_name=$(this).text();
location.href = "member_mall_list.php?goods_first_id=" + goods_first_id+"&goods_first_name="+first_name;
封装一个js函数用于获取传过来的值,建议这个函数封装在公共的js里面:
var $_GET = (function(){
var url = window.document.location.href.toString();
var u = url.split("?");
if(typeof(u[1]) == "string"){
u = u[1].split("&");
var get = {};
for(var i in u){
var j = u[i].split("=");
get[j[0]] = j[1];
}
return get;
} else {
return {};
}
})();
var goods_first_name = $_GET['goods_first_name'];
var first_id = $_GET['goods_first_id'];
var titleName = decodeURIComponent(goods_first_name);//用于解码页面传过来的文字
这样就获取到页面传过来的值了。
location.href = "/views/list.html?myvalue=" + 123;
另一个页面
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
var context = "";
if (r != null)
context = r[2];
reg = null;
r = null;
return context == null || context == "" || context == "undefined" ? "" : context;
}
alert(GetQueryString("myvalue"));
方式一:
前台GET请求 : URL?p1=v1&p2=v2&p3=v3
后台直接用p1、p2、p3接收,或者用包含p1、p2、p3属性的对象接收。
方式二:
前台GET请求:URL/v1
后台取URL中的值即可,此方法,不在前台暴露参数名
location.href = "/views/list.html?name1=值1&name2=值2"
然后在list.html中:
var request = (function (){
var obj = {};
var arr = window.location.search.slice(1).split("&");
for (var i = 0, len = arr.length; i < len; i++) {
var nv = arr[i].split("=");
obj[decodeURIComponent(nv[0]).toLowerCase()] = decodeURIComponent(nv[1]);
}
return obj;
})();