Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get protocol, domain, and port from URL

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 
like image 725
yelo3 Avatar asked Aug 04 '11 12:08

yelo3


People also ask

How do I find the protocol of a URL?

The getProtocol() function is a part of URL class. The function getProtocol() returns the Protocol of a specified URL.

How can we get hostname and port information in JavaScript?

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.

How do I get the URL from the console?

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.

What is location protocol?

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.


1 Answers

const full = location.protocol + '//' + location.host; 
like image 124
Shef Avatar answered Sep 28 '22 07:09

Shef