URL中會帶上參數,假如是?開頭的,那這個是會被加入到ajax請求中的,#(hash)相當於書簽錨點,用於定位頁面,不會加入到ajax請求中,所以有些時候,我們可以把一些參數放在#后面
如何獲取URL中的參數並解析出來?
let url = location.search; //獲取url中"?"符后的字串
let theRequest = new Object();
if (url.indexOf("?") != -1) {
let str = url.substr(1);
let strs = str.split("&");
for(let i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
直接theRequest.name,就可以調用了;
如何設置、獲取#后面的參數?
hash可讀、可寫,
寫:window.location.hash = '.....'
讀:window.location.hash
獲取到string后,然后在進行解析,解析的辦法可以參考上一條。
希望本文對你有所幫助