I need to extract the full protocol, domain, and port from a given URL. For example:
https://localhost:8181/ContactUs-1.0/contact?lang=it&report_type=consumer >>> https://localhost:8181
The getProtocol() function is a part of URL class. The function getProtocol() returns the Protocol of a specified URL.
If you only want to return the hostname value (excluding the port number), use the window. location. hostname method instead. This will return a string value containing the hostname and, if the port value is non-empty, a : symbol along with the port number of the URL.
Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc. The following example will display the current url of the page on click of the button.
The Location protocol property in HTML is used to return the protocol or set the protocol of the current URL. It returns a string which contains the protocol of the current URL, including the colon (:). Syntax: It returns the protocol property.
const full = location.protocol + '//' + location.host;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With