JS gets various parameters of the current page URL#
A: the Location#
The Location object contains information about the current URL.
The Location object is part of the Window object and is accessible through the window. Location property.
hash | Sets or returns the URL (anchor) starting with the hash sign (#). |
---|---|
host | Sets or returns the hostname and port number of the current URL. |
hostname | Sets or returns the host name of the current URL. |
href | Sets or returns the full URL. |
pathname | Sets or returns the path portion of the current URL. |
port | Sets or returns the port number of the current URL. |
protocol | Sets or returns the protocol for the current URL. |
search | Sets or returns from question mark (?) The starting URL (query section). |
example
Copyvar href = window.location.href;
Copy the code
Two: coding and decoding#
Correct encoding and decoding helps to process Chinese characters, while incorrect encoding will result in Chinese garbled characters.
function | describe |
---|---|
decodeURI() | Decodes the URI of an encoding. |
decodeURIComponent() | Decodes an encoded URI component. |
encodeURI() | Encode a string as a URI. |
encodeURIComponent() | Encode strings as URI components. |
escape() | Encodes a string. |
unescape() | Decodes the string encoded by escape(). |
Three: copy and use#
Most of the online examples are decoded using unescape(), but the test Chinese is still garbled, to decodeURI() can be.
Copy/ * * * * access to the URL parameter @ param parameter name * @ * / function returns parameter values GetQueryString (name) {var reg = new RegExp (" (^ | &) "+ name + "= (/ ^ & *) (& | $)"); var r = window.location.search.substr(1).match(reg); if (r ! = null) return decodeURI(r[2]); return null; }
Copy the code
When used, it is best to add a null value judgment:
Copyif (GetQueryString("sex")! =null&&GetQueryString("sex").length>0) { // }
Copy the code
Follow the public account on wechat
Author: Xue Qin
Source: www.cnblogs.com/yueshutong/…
This site uses “CC BY 4.0” creative sharing agreement, please indicate the author and source in the obvious position of the article.