如果傳遞的參數是:
<a href="${pageContext.request.contextPath}/productdisplay/productDisplay_productDisplayUI.action?pkId=${pkId}&name=${name}" style="color:white; margin-top:10px; margin-bottom:10px;">${name}</a>
獲取url參數的js如下:
/** 初始化加載結束 */
function getUrlVars(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?')+1).split('&');
for(var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
如果url中有中文,則取參數的時候會有中文亂碼,得用decodeURI()方法解碼,方式如下:
//獲得所屬分類的pkId
var params = getUrlVars();
//獲得此中分類的id
var parentId = params[params[0]];
//獲得分類的名稱
var productName = decodeURI(params[params[1]]);