1. Get the entire URL string
To get the parameters in the URL, first we need to get the entire URL string. We use: http://www.zhihua.com/wap/tmpl/member/member.html? Token = Zhihua as an example.
① Get (or set) the protocol part of the URL: window.location.protocol
- //window.location.protocol set or get the protocol part of URL
- var test = window.location.protocol;
- alert(test);
- //Return to pop up: http:
② Get (or set) the host part of the URL: window.location.host
- //window.location.host set or get the host part of the URL
- var test = window.location.host;
- alert(test);
- //Back to pop up: www.zhihua.com
③ Get (or set) the port number associated with the URL: window.location.port
- //window.location.port sets or gets the port number associated with the URL
- var test = window.location.port;
- alert(test);
- //Return pop-up: empty character (if the default port 80 is used (even if: 80 is added), the return value is not the default 80 but empty character)
④ Get (or set) the path part of the URL, that is, the file address: window.location.pathname
- //window.location.pathname sets or gets the path part of the URL (that is, the file address)
- var test = window.location.pathname;
- alert(test);
- //Return to pop up: / wap/tmpl/member/member.html
⑤ Get (or set) the part of the URL property following the question mark: window.location.search
- //window.location.search sets or gets the part of the attribute that follows the question mark
- var test = window.location.search;
- alert(test);
- //Return to pop-up:? Token = Zhihua
⑥ Gets (or sets) the segment in the URL property after the pound sign "ා": window.location.hash
- //window.location.hash sets or gets the segment after the pound sign in the attribute
- var test = window.location.hash;
- alert(test);
- //Return pop-up: empty character (because not in url)
⑦ Get (or set) the entire URL string: window.location.href
2. Get the parameter value in the URL
- //window.location.href set or get the entire URL string
- var test = window.location.href;
- alert(test);
- //Back to pop up: http://www.zhihua.com/wap/tmpl/member/member.html? Token = Zhihua
After the URL string is obtained, the parameter data information in the URL string is obtained. Here are several ways to get parameters:
① Get parameter value by comparing with regular expression
- function getQueryString(name){
- var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
- var r = window.location.search.substr(1).match(reg);
- if (r!=null) return r[2]; return '';
- }
② Split split method
- function GetRequest() {
- var url = location.search; //Get the string after the "? Character in the url
- var theRequest = new Object();
- if (url.indexOf("?") != -1) {
- var str = url.substr(1);
- 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 id=Request["id"];
- //var parameter 1, parameter 2, parameter 3, parameter N;
- //Parameter 1 = Request ['parameter 1'];
- //Parameter 2 = Request ['parameter 2'];
- //Parameter 3 = Request ['parameter 3'];
- //Parameter N = Request ['parameter N'];
③ How to get a single parameter
- function GetRequest() {
- var url = location.search; //Get the string after the "? Character in the url
- if (url.indexOf("?") != -1) { //Judge whether there are parameters
- var str = url.substr(1); //Start with the first character because the 0 is the? To get all strings except the question mark
- strs = str.split("="); //Use the equal sign to separate (because you know that there is only one parameter, use the equal sign to separate directly. If there are multiple parameters, use the & sign to separate, and then use the equal sign to separate)
- alert(strs[1]); //Pop up the first parameter directly (if there are more than one parameter, you need to loop)
- }
- }