html頁面中js獲取當前url下指定參數的值

如當前url爲http://www.xxx.com/?name=itgirl&[email protected]

要想在html頁面下用js獲取name的值, 則示例如下

<script>
    alert(getUrlParameter('name'));

    // 根據參數名稱獲取value    
    function getUrlParameter(paramKey) {
        var sURLVariables, i, sParameterName, sPageURL = window.location.search.substring(1);
        if (sPageURL) {
            sURLVariables = sPageURL.split("&");
            for (i = 0; i < sURLVariables.length; i++) {
                sParameterName = sURLVariables[i].split("=");
                if (sParameterName[0] === paramKey) return sParameterName[1]
            }
        }
    }

</script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章