js獲取url傳遞參數
阿新 • • 發佈:2017-09-05
-c set turn cape 傳遞參數 regexp location size char
果凍棟吖
<head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> window.onload = function() { function GetURLlist(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");var r = window.location.search.substr(1).match(reg); if(r != null) return unescape(r[2]); return null; } function GetUrlParam(paraName) { var url = document.location.toString(); var arrObj = url.split("?"); if(arrObj.length > 1) { var arrPara = arrObj[1].split("&"); var arr; for(var i = 0; i < arrPara.length; i++) { arr = arrPara[i].split("=");if(arr != null && arr[0] == paraName) { return arr[1]; } } return ""; } else { return ""; } } // 調用方法 console.log(GetURLlist("a")); console.log(GetURLlist("b")); console.log(GetURLlist("c")); // 調用方法 console.log(GetUrlParam("a")); console.log(GetUrlParam("b")); console.log(GetUrlParam("c")); } </script> </head> <body> </body>
設置或獲取對象指定的文件名或路徑。
alert(window.location.pathname)
設置或獲取整個 URL 為字符串。
alert(window.location.href);
設置或獲取與 URL 關聯的端口號碼。
alert(window.location.port)
設置或獲取 URL 的協議部分。
alert(window.location.protocol)
設置或獲取 href 屬性中在井號“#”後面的分段。
alert(window.location.hash)
設置或獲取 location 或 URL 的 hostname 和 port 號碼。
alert(window.location.host)
設置或獲取 href 屬性中跟在問號後面的部分。
alert(window.location.search)
轉自:js獲取url傳遞參數
js獲取url傳遞參數