Getting port from url string using Javascript

I need a function in javascript that will take a URL as a parameter and return the port of that URL in the following form:

  • If there is http

    or https

    (port 80/443) it will not show up in the url structure, but I want them to return anyway.
  • If there is another port, I want it returned.

Example:

function myFunction(url){
    something here
    ...
    return port
}

      

I've seen that this can be done easily using some additional libraries, but I don't want to use it. I have not worked with js since then and I would really appreciate if someone can also explain his solution.

+3


source to share


3 answers


From what I get, you don't want to use it location

as the url to subtract the port, just any string as the url. Well, I came up with this for such a case. This function accepts any string (but you can pass a URL location

to it anyway, and it works the same):

function getPort(url) {
    url = url.match(/^(([a-z]+:)?(\/\/)?[^\/]+).*$/)[1] || url;
    var parts = url.split(':'),
        port = parseInt(parts[parts.length - 1], 10);
    if(parts[0] === 'http' && (isNaN(port) || parts.length < 3)) {
        return 80;
    }
    if(parts[0] === 'https' && (isNaN(port) || parts.length < 3)) {
        return 443;
    }
    if(parts.length === 1 || isNaN(port)) return 80;
    return port;
}

      



  • It gets the base url from the string.
  • It splits the base url into parts, into ':'

    .
  • It tries to parse only part of the digits (the last element of the array parts

    ) into an integer.
  • If the URL starts with 'http'

    AND , the port is not a number, or the length of the array of URL parts is less than 3 (meaning that the URL does not imply a port), it returns the default HTTP port.
  • The same goes for 'https'

    .
  • If the length was 1

    , it means that neither protocol nor port was provided. In this case, or in case the port is not a number (and, again, no protocol is provided), return the HTTP

    default port .
  • If it goes through all of these tests, it just returns the port it was trying to parse into an integer at the beginning of the function.
+5


source


Here is a regex based solution (regex is not bullet proof):



var urls = [
  "http://localhost/path/",
  "https://localhost/",
  "http://localhost:8080",
  "https://localhost:8443/path",
  "ftp://localhost/"
];
var i;
for (i = 0; i < urls.length; i++) {
  console.log(urls[i], getPortFromURL(urls[i]));
}

function getPortFromURL(url) {
  var regex = /^(http|https):\/\/[^:\/]+(?::(\d+))?/;
  var match = url.match(regex);
  if (match === null) {
    return null;
  } else {
    return match[2] ? match[2] : {http: "80", https: "443"}[match[1]];
  }
}
      

<!-- nothing here, see console -->
      

Run code


+1


source


hope the following code helps

function getPort(){
var port==location.port;
if(port=="")
{
if(location.protocol=="http:")
  return "80";
else
  return "443";
return port;
}

      

-1


source







All Articles