請求頁面時js自動獲取地址欄上的參數
需求就是使用騰訊雲點播,然后需要預覽視頻跳轉到另一個頁面去查看,騰訊那邊的接口是需要獲取到資源id才能夠去點播視頻.我是使用get請求去跳轉到另一個頁面就需要直接從地址欄自動獲取該參數就可以了
例如 http://localhost:8080/xxx?id=37019259256594858934
就會取出 37019259256594858934
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]]=decodeURI(strs[i].split("=")[1]);
}
}
return theRequest;
}
//調用
var Request = new Object();
Request = GetRequest();
var fileID;
fileID = Request['resPid'];
console.log("fileID:"+fileID)