JavaScriptURL的处理

hyx 2019-09-03 10:13:12 85次 0
 //var location=window.location;
    console.log(window.location);
    console.log(window.location.href);
    console.log(window.location.href.split("?"));
    console.log(window.location.hash);
    console.log(window.location.host);
    console.log(window.location.search);
    var canshu = window.location.search;
    var loc = canshu.substring(canshu.lastIndexOf('=')+1, canshu.length);
    console.log(loc);

    //返回的是对象形式的参数
    function getUrlArgObject(){
        var args=new Object();
        var query=location.search.substring(1);//获取查询串
        var pairs=query.split(",");//在逗号处断开
        for(var i=0;i<pairs.length;i++){
            var pos=pairs[i].indexOf('=');//查找name=value
            if(pos==-1){//如果没有找到就跳过
                continue;
            }
            var argname=pairs[i].substring(0,pos);//提取name
            var value=pairs[i].substring(pos+1);//提取value
            args[argname]=unescape(value);//存为属性
        }
        return args;//返回对象
    }
    var k=getUrlArgObject();
    console.log(k);
    function getUrlArgStr(){
        var q=location.search.substr(1);
        var qs=q.split('&');
        var argStr='';
        if(qs){
            for(var i=0;i<qs.length;i++){
                argStr+=qs[i].substring(0,qs[i].indexOf('='))+'='+qs[i].substring(qs[i].indexOf('=')+1)+'&';
            }
        }
        return argStr;
    }
    var h=getUrlArgStr();
    console.log(h);
    function getUrlArgStr1(){
        var q=location.search.substr(1);
        var qs=q.split('&');
        var argStr=[];
        if(qs){
            for(var i=0;i<qs.length;i++){
                var g=qs[i].substring(0,qs[i].indexOf('='));
                argStr[g]=qs[i].substring(qs[i].indexOf('=')+1);
            }
        }
        return argStr;
    }
    var i=getUrlArgStr1();
console.log(i);
console.log(i['c']);

发表评论

注:*为必填

回复 的评论
*
选择
*
*