• js解析url参数


    方法一:
     
    // var url = "https: //zhidao.baidu.com/question/1768422895052400180.html?fr=iks&word=slice&ie=gbk";
    var url = location.href;
    // 提取url中的解析字符串
    function UrlParamHash(url) {
    var params = [],
    h;
    var hash = url.slice(url.indexOf("?") + 1).split('&');
    console.log(hash);
    for (var i = 0; i < hash.length; i++) {
    h = hash[i].split("="); //
    params[h[0]] = h[1];
    console.log(h);
    }
    return params;
    }
    var params = UrlParamHash(url);
    console.log(params);

    var id = decodeURI(params['unit']);
    console.log("id:" + id);
     
    方法二
    // // 解析url字符串参数
    // var loc = location.href;
    // console.log(loc);
    // var n1 = loc.length; //地址的总长度
    // var n2 = loc.indexOf("="); //取得=号的位置
    // // var id = decodeURI(loc.substr(n2 + 1, n1 - n2)); //从=号后面的内容
     
    方法三

    //获取url中的参数
    //name-查询的变量名
    function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null)
    {return decodeURIComponent(r[2]); }
    return null; //返回参数值
    }

  • 相关阅读:
    HDU 2196 Computer
    HDU 1520 Anniversary party
    POJ 1217 FOUR QUARTERS
    POJ 2184 Cow Exhibition
    HDU 2639 Bone Collector II
    POJ 3181 Dollar Dayz
    POJ 1787 Charlie's Change
    POJ 2063 Investment
    HDU 1114 Piggy-Bank
    Lca hdu 2874 Connections between cities
  • 原文地址:https://www.cnblogs.com/ddqyc/p/10530361.html
Copyright © 2020-2023  润新知