JavaScript获取 当前 地址栏 url 参数值

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>JavaScript获取 地址栏 url 参数值</title>
</head>

<body>
    <button id="testId">获取当前 Url 参数值</button>
    <script>
        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) { return pair[1]; }
            }
            return (false);
        };
        document.getElementById('testId').addEventListener('click', function () {
            //假设当前地址为 http://www.luofenming.com/show.aspx?id=ART2020060400001 
            //则获取的值为ART2020060400001 
            alert(getQueryVariable("id"));
        });
    </script>
</body>
</html>

评论