0

This is the URL:

http:// localhost:8888/index.html. 

How to get the port number from this URL using jQuery?

2

2 Answers 2

9

It is available in the location object:

location.port

Note that when no port is present in the URL, location.port will return an empty string.

If you need to get the port even when the implicit default port is used, try:

var port = location.port || (location.protocol === 'https:' ? '443' : '80');

This should suffice for pages served through http and https protocols.

2
  • Where's location.port mentioned on that MDN page?
    – j08691
    Commented May 19, 2014 at 13:38
  • @j08691 good point, I've replaced the link. window.location inherits it from the URLUtils interface. Commented May 19, 2014 at 13:38
0

You don't need jQuery for it.

window.document.location.port

Not the answer you're looking for? Browse other questions tagged or ask your own question.