1. 程式人生 > >html--對URL傳參數進行解析

html--對URL傳參數進行解析

-- earch com turn indexof repl span 需要 lac

跳轉頁面需要傳參數到另外一個html頁面,跳轉鏈接可寫一個js的function

function doView(articleId) {

    window.location.href ="collectionArticleComment.jsp?articleId = 2576;
}
然而URL會被編碼(encodeURL):
collectionArticleComment.jsp?articleId%20=%202576
因此我們先將URL解碼(decodeURL)
進參數進行解析,用到如下方法:
function GetRequest() {
var url = location.search ;
url = decodeURI(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].replace(/\s/g,"")]=unescape(strs[i].split("=")[1].replace(/\s/g,""));
        }
}
return theRequest;
}
將返回一個對象
["articleId ":"2576"]





html--對URL傳參數進行解析