如何从jquery中的url获取url参数?

时间:2018-02-28 07:28:49

标签: javascript jquery

我想从url获取参数。 以下是我的网址。 我想从我的网址获取“uid”参数。

http://localhost:8080/mydemoproject/#/tables/view?uid=71

3 个答案:

答案 0 :(得分:-1)

尝试使用RegEx解决这些问题

let myURL = window.location.href; // "http://localhost:8080/mydemoproject/#/tables/view?uid=71"
let myRegexp = /uid=(\d*)/i;
let match = myRegexp.exec(myURL);
if (match !== null) {
  console.log(match[1]); // 71
}

精简版本看起来像这样

let match = /uid=(\d*)/i.exec(window.location.href);
let uidValue = (match !== null ? match[1] : undefined);
console.log(uidValue); // 71

答案 1 :(得分:-1)

在页面加载时调用函数" get_param('uid');" 并将此代码粘贴到您的文档中。

<script type= "text/javascript" >

    function get_param(param) {
        var vars = {};
        window.location.href.replace(location.hash, '').replace(
        /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
        function (m, key, value) { // callback
            vars[key] = value !== undefined ? value : '';
        }
    );
    alert(vars[param]);
    }
</script>

答案 2 :(得分:-1)

试一试:

window.location.href.split("=")[1]