关闭

url解析函数

标签: 函数jsurl
145人阅读 评论(0) 收藏 举报
分类:
function parseURL(url){
        var a = document.createElement("a");
        a.href = url;
        return {
            source:url,
            protocol:a.protocol.replace(":",""),
            host:a.hostname,
            port:a.port,
            query:a.search,
            params:(function(){
                var p = {},
                    query = a.search,
                    paramStr = query.substring(1),
                    paramArr = paramStr.split("&");
                paramArr.forEach(function(x){
                    var arr = x.split("=");
                    p[arr[0]] = arr.length===2?arr[1]:null;
                });
                return p;
            })()
        }
    }
    console.log(parseURL("http:www.101test.com/reportDetail/187077?paperId=40712&candidateId=151434"));

执行结果:

host: "www.101test.com"
params: Object
    candidateId: "151434"
    paperId: "40712"
__proto__: Object
port: ""
protocol: "http"
query: "?paperId=40712&candidateId=151434"
source: "http:www.101test.com/reportDetail/187077?paperId=40712&candidateId=151434"
__proto__: Object
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6999次
    • 积分:405
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类