JavaScript parameter values to use to obtain the url

Today will need to obtain the parameters from the url on the Internet to find a few JavaScript method, mark down.
 
Url specified number of portions may be used to acquire: such http://www.mystuff.com.cn/aboutus/
location     objects containing information of the current URL, the entire URL string href attribute
protocol       comprising a first part of the URL string such as http:
host        with the URL hostname: port number of the string portion as //www.mystuff.com.cn/aboutus/.
hostname    contains the host name of the URL string as http://www.mystuff.com.cn
port        contains the URL port number that may exist in the string.
pathname     the URL of "/" section later. As ~ list / index.htm
hash            string following the "#" (CGI parameter)
search      string after the "?" No. (CGI parameters) 
 
The first method: split Split using (for a single parameter)
GetRequest function () { 
   var url = location.search; // Get the url string after the symbol "?" 
   IF (url.indexOf () = -1 "?"!) {// determines whether the parameter 
      var str = url.substr (1); // starting from the first character is a number 0 because of an overview of all strings all except a question mark? 
      STRs str.split = ( "="); // separated by an equal sign (because Therefore we know that only one parameter directly separated into equal sign if there are multiple parameters to use & separated by an equal sign and then separated) 
      Alert (STRs [. 1]); // pop up directly the first parameter (if there are multiple parameters also circulated) 
   } 
}

  

The second method: using split Split (s parameters)
GetRequest function () { 
    var url = location.search; // Get the url string after the symbol "?" 
    var = theRequest new new Object (); 
    IF ( "?"! url.indexOf () = -1) { 
        var = url.substr STR (. 1); 
        STRs = str.split ( "&"); 
        for (var I = 0; I <strs.length; I ++) { 
            theRequest [STRs [I] .split ( "=" ) [0]] = unescape (STRs [I] .split ( "=") [. 1]); 
        } 
    } 
    return theRequest; 
} 
// this call: 
var = the Request new new Object (); 
the Request GetRequest = (); 
/ / 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 [ 'parameters N'];

  

The third method: using regular expressions

the getQueryString function (name) { 
    var new new REG = the RegExp ( '(^ | &)' + name + '= ([^ &] *) (& | $)', 'I'); 
    var R & lt = the window.location. search.substr (1) .match (REG); 
    IF (= R & lt null!) { 
        return unescape (R & lt [2]); 
    } 
    return null; 
} 
// this call: 
Alert (GetQueryString ( "parameter name 1")) ; 
Alert (GetQueryString ( "parameter name 2")); 
Alert (GetQueryString ( "parameter name 3"));

  

Reference blog:

https://www.cnblogs.com/jing1208/p/6252408.html

https://blog.csdn.net/zhang__ao/article/details/78614291

https://www.cnblogs.com/imhurley/p/3847812.html

 

Guess you like

Origin www.cnblogs.com/miaolyou/p/11029626.html