Home >Web Front-end >JS Tutorial >HTML URL address parsing

HTML URL address parsing

高洛峰
高洛峰Original
2016-11-03 13:38:491733browse

HTML URL address parsing

Through the location object of JavaScript, you can obtain the protocol, host name, port, anchor point, query parameters and other information in the URL.

Example

URL: http://www.akmsg.com/WebDemo/URLParsing.html#top?username=admin&pwd=123456

Parse results:

HTML URL address parsing

Code

console.log(
    'location.hash     :' + location.hash     + '\r\n' +
    'location.host     :' + location.host     + '\r\n' +
    'location.hostname :' + location.hostname + '\r\n' +
    'location.href     :' + location.href     + '\r\n' +
    'location.pathname :' + location.pathname + '\r\n' +
    'location.port     :' + location.port     + '\r\n' +
    'location.protocol :' + location.protocol + '\r\n' +
    'location.hash     :' + location.hash     + '\r\n' +
    'location.search   :' + location.search   + '\r\n' +
    'location.origin   :' + location.origin
)

Online example

Address: http://www.akmsg.com/WebDemo/URLParsing.html


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn