1瓣铣、循環(huán)
function getQuery() {
const url = decodeURI(location.search); // 獲取url中"?"符后的字串(包括問(wèn)號(hào))
let query = {};
if (url.indexOf("?") != -1) {
const str = url.substr(1);
const pairs = str.split("&");
for(let i = 0; i < pairs.length; i ++) {
const pair = pairs[i].split("=");
query[pair[0]] = pair[1];
}
}
return query ; // 返回對(duì)象
}
function getQueryVariable(name) {
const url = decodeURI(location.search); // 獲取url中"?"符后的字串(包括問(wèn)號(hào))
let query = {};
if (url.indexOf("?") != -1) {
const str = url.substr(1);
const pairs = str.split("&");
for(let i = 0; i < pairs.length; i ++) {
const pair = pairs[i].split("=");
if(pair[0] === name) return pair[1]; // 返回 參數(shù)值
}
}
return(false);
}
2答朋、正則表達(dá)式
function getQueryVariable(name) {
const reg = new RegExp("(^|&)" + name+ "=([^&]*)(&|$)", "i");
const result = window.location.search.substr(1).match(reg);
if ( result != null ){
return decodeURI(result[2]);
}else{
return null;
}
}
經(jīng)典前端面試題每日更新,歡迎參與討論棠笑,地址:https://github.com/daily-interview/fe-interview梦碗。
更多angular1/2/4/5、ionic1/2/3蓖救、react洪规、vue、微信小程序藻糖、nodejs等技術(shù)文章淹冰、視頻教程和開(kāi)源項(xiàng)目,請(qǐng)關(guān)注微信公眾號(hào)——全棧弄潮兒巨柒。
微信公眾號(hào)