JavaScript 獲取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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM