根据url的属性名来取属性值赋值给js

1.方法一:js的正则表达式:请求路径:http://127.0.0.1/pec/jsp/member/refundOrder.jsp?status=4

<script>
    var status= getUrlAttribute('status');
    if(null!=status)
    {
        alert(status);
    }
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r != null) {
            return unescape(r[2]);
        }
        return null;
    }
</script>

2.方二:

function getUrlParam(parameName){
    //location.search是从当前URL的?号开始的字符串,即查询字符串
    var query = (location.search.length > 0 ? location.search.substring(1) : null);
    if(null!=query)
    {
        var args = new Object( );
        var pairs = query.split("&"); 
        for(var i = 0; i < pairs.length; i++) 
        {
            var pos = pairs[i].indexOf("="); 
            if (pos == -1) 
                continue; 
            var argname = pairs[i].substring(0,pos); 
            var value = pairs[i].substring(pos+1); 
            value = decodeURIComponent(value); 
            args[argname] = value; 
        }
        //根据键名获取值
        return args[parameName]; 
    }
    return null;
}

 

posted @ 2018-09-07 17:26  A小小高  阅读(616)  评论(0编辑  收藏  举报