1、獲取url:
window.location.href;
2、獲取url中的文件名:
function getHtmlDocName() {
var str = window.location.href;
str = str.substring(str.lastIndexOf("/") + 1);
str = str.substring(0, str.lastIndexOf("."));
return str;
}
3、獲取url中的某個參數:
function getUrlParam(name) {
//構造一個含有目標參數的正則表達式對象
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
//匹配目標參數
var r = window.location.search.substr(1).match(reg);
//返回參數值
if (r != null) return unescape(r[2]);
//不存在時返回null
return null;
}
4、ECMAScript v3 已從標准中刪除了 unescape() 函數,並反對使用它,因此應該用 decodeURI() 和 decodeURIComponent() 取而代之。
綜上: javascript對參數編碼解碼方法要一致:
-
escape() → unescape()
-
encodeURI() → decodeURI()
-
encodeURIComponent() → decodeURIComponent()
