js 获取url多个参数
生活随笔
收集整理的這篇文章主要介紹了
js 获取url多个参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、js獲取單個參數
js獲取url傳遞里面的參數
- url="http://t.html?id=151";
- var url = window.location.href;
- var id = url.substr(url.indexOf("=") + 1, url.length);
- ?
- id 獲取的值為151
?
2、js獲取url多個參數
?- 傳值:window.location.href = 'RegOk.htm?email='+email+'&name='+username+'';
- ?
- function GetRequest() {
- var url = location.search; //獲取url中"?"符后的字串
- var theRequest = new Object();
- if (url.indexOf("?") != -1) {
- var str = url.substr(1);
- alert(str);
- strs = str.split("&");
- for (var i = 0; i < strs.length; i++) {
- theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
- }
- }
- return theRequest;
- }
- var request = new Object();
- request = GetRequest();
- var email = request['email'];
- var name = request['name'];
- alert(email);alert(name);
- document.getElementById("emil").innerHTML = email;
- document.getElementById("nae").innerHTML = name;
?
?
?
其他參數獲取介紹:
- //設置或獲取對象指定的文件名或路徑。
- 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多个参数的全部內容,希望文章能夠幫你解決所遇到的問題。