10 examples of 'how to get ip address and port number from url' in JavaScript

Every line of 'how to get ip address and port number from url' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
155function getPort(url) {
156 var hostPattern = /\w+:\/\/([^\/]+)(\/)?/i;
157 var domain = url.match(hostPattern);
158
159 var pos = domain[1].indexOf(":");
160 if(pos !== -1) {
161 domain[1] = domain[1].substr(pos + 1);
162 return parseInt(domain[1]);
163 } else if(url.toLowerCase().substr(0, 5) === "https") return 443;
164 else return 80;
165}
57function getProxyHostPort(url) {
58 var l = document.createElement('a');
59 l.href = url;
60 return {host: l.host.split(':')[0], port: l.port};
61}
21function extractHost(url) {
22 // Extract host from ':///', includes the port
23 return (/:\/\/([^/]+)\//).exec(url)[1];
24}
34function parse_url(url) {
35 var parser = document.createElement('a');
36 parser.href = url;
37 var new_url = parser.hostname;
38 if (new_url.indexOf("www.") === 0) {
39 new_url = new_url.substring(4, new_url.length)
40 }
41 return new_url
42}
57function getHost(url){
58 var parser = document.createElement('a');
59 parser.href = url;
60 return parser.host;
61}
81function getPort(hostUrl) {
82 var port
83
84 // check for specific port number
85 if (hostUrl != null && typeof hostUrl == 'string') {
86 var urlObject = url.parse(hostUrl, false, true)
87
88 // port number given
89 if (urlObject.port) {
90 var num = parseInt(urlObject.port)
91 if (!isNaN(num)) port = num
92 }
93
94 // https default port
95 if (!port && urlObject.protocol &&
96 urlObject.protocol == 'https:') {
97 port = 443
98 }
99 }
100
101 // default port
102 if (!port) port = 80
103
104 return port
105}
12export default function extractHostname(url) {
13 let hostname;
14
15 if (url.indexOf("://") > -1) {
16 hostname = url.split("/")[2];
17 } else {
18 hostname = url.split("/")[0];
19 }
20 hostname = hostname.split(":")[0];
21 hostname = hostname.split("?")[0];
22
23 return hostname;
24}
59function extractHostname(url) {
60 // https://stackoverflow.com/a/23945027
61 return url
62 .split("/")
63 [url.indexOf("//") > -1 ? 2 : 0].split(":")[0]
64 .split("?")[0];
65}
154function getProtoFromURL(){
155//Returns the protocol portion of the current URL
156 var url = document.URL;
157 var url_parts = document.URL.split("/");
158 var result =url_parts[0];
159 result = result.replace(":","");
160 return result;
161}
10function extractHostname(url) {
11 const [, hostname] = HOSTNAME_RE.exec(url) || [];
12 return hostname;
13}

Related snippets