js:app
/** * 獲取請求url中的全部參數的object對象 * @returns */ function getAllUrlParms(url) { var theRequest = new Object(); if (!url) url = location.href; if (url.indexOf("?") !== -1) { var str = url.substr(url.indexOf("?") + 1) + "&"; var strs = str.split("&"); for (var i = 0; i < strs.length - 1; i++) { var key = strs[i].substring(0, strs[i].indexOf("=")); var val = strs[i].substring(strs[i].indexOf("=") + 1); theRequest[key] = val; } } return theRequest; }
根據參數判斷url是否相同,並重構新的url:url
function checkUrlFun(oldUrl,paramObj,newUrl){ var returnUrl = ""; var updateFlag = false; if(!oldUrl || oldUrl == 'about:white'){ //頁面尚未加載過,利用newUrl拼接paramObj,而且返回 updateFlag = true; }else{ //頁面已經加載過 var obj = getAllUrlParms(oldUrl); if(paramObj && JSON.stringify(paramObj) != '{}'){ for(var key in paramObj){ //若是老的url裏面和paramObj裏面有參數不一樣,那麼須要構建新的url if(!obj[key] || obj[key] != paramObj[key]){ updateFlag = true; break; } } } } if(updateFlag){ var index = newUrl.indexOf("?"); if(index != -1){ newUrl = newUrl.substring(0,index); } var appendUrl = $.param(paramObj); newUrl += "?"+appendUrl; returnUrl = newUrl; } return returnUrl ; }
根據paramObj設置頁面的全局變量的值:code
/** * 修改頁面默認變量的值 * @returns */ function changeVariableValueFun(paramObj){ if(paramObj && JSON.stringify(paramObj) != '{}'){ for(var key in paramObj){ var val = key.replace("object.",""); window[val] = paramObj[key]; $('#'+val).val(paramObj[key]); } } }