JS 获取url,html文件名,参数值

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()

猜你喜欢

转载自blog.csdn.net/ruo_62/article/details/46421955