Can I check whether the port number is existing in a given URL string or not?
Like sometimes a user can type 202.567.89.254:8088
or http://202.567.89.254:8088/
or http://202.567.89.254
.
Out of all the above options, if the port number is existing, then do nothing otherwise append 8080
by default with an end slash 8080/
.
Is it possible in JavaScript?
Use in URLs Port numbers are sometimes seen in web or other uniform resource locators (URLs). By default, HTTP uses port 80 and HTTPS uses port 443, but a URL like http://www.example.com:8080/path/ specifies that the web browser connects instead to port 8080 of the HTTP server.
The getProtocol() function is a part of URL class. The function getProtocol() returns the Protocol of a specified URL.
The SCI protocol uses only HTTPS to secure for the credentials passed in each request. By default, these two protocols are on their standard port number of 80 for HTTP and 443 for HTTPS.
You can try to use the location object and use:
location.port
The HTMLHyperlinkElementUtils.port property is a USVString containing the port number of the URL.
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