js获取url传值
例如:https://www.baidu.com/?id=123
1.方法1
function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if(url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } } return theRequest; };
GetRequest("id")
2.方法2
function getQueryString(key) { var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)"); var result = window.location.search.substr(1).match(reg); return result ? decodeURIComponent(result[2]) : null; }
getQueryString("id")
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216810.html原文链接:https://javaforall.net
