Parse URL into Hostname and Path in JavaScript
If you have a string containing a URL, it can be useful to break it into its components, such as the hostname and the path. In JavaScript, there are a few ways to achieve this.
Modern Approach:
The most modern approach is to use the URL constructor:
new URL("http://example.com/aa/bb/")
This returns an object with properties for hostname and pathname, among others like port and searchParams.
Note:
new URL("/aa/bb/", location)
The above is the detailed content of How to Parse a URL into Hostname and Path in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!