首页 > 技术文章 > JavaScript------获取url地址中的参数

tianhengblogs 2016-12-28 10:22 原文

转载:
http://www.cnblogs.com/babycool/p/3169058.html

//javascript对参数编码解码方法要一致:

escape()   unescape()
encodeURI()   decodeURI()  //中文编码
encodeURIComponent()    decodeURIComponent()  

$(document).ready(function () {
    //获取地址中的参数(name是字符串)
    function getParameter(name) {
        //正则表达式
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        //location:window的对象
        //search:获取url中的query部分
        //substr:从指定位置开始截取字符串(去掉"?"号)
        var r = window.location.search.substr(1).match(reg);
        //unescape() 函数可对通过 escape() 编码的字符串进行解码。
        if (r != null) return unescape(r[2]); return null;
    }

    var str = getParameter("bookId");
    alert(str);
});

 

推荐阅读