js獲取url參數值


效果:

代碼:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
window.onload = function(){
    document.getElementById("username").value = getUrlParam("username");
    document.getElementById("nickname").value = getUrlParam("nickname");
}

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = decodeURI(window.location.search).substr(1).match(reg);
    if (r != null){
        return decodeURI(unescape(r[2]));
    }
    return null;
}
</script>
<title>點擊獲取url參數值</title>
</head>
<body>
    <a href="getUrlParam.html?username=hello你好&nickname=world世界" target="_blank">點擊獲取url參數值</a><br />
    參數username的值:<input id="username" readonly/><br />
    參數nickname的值:<input id="nickname" readonly/>
</body>
</html>

 

打賞

免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2022 CODEPRJ.COM