0

This is the URL:

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

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

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
thevan
  • 10,052
  • 53
  • 137
  • 202

2 Answers2

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.

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
0

You don't need jQuery for it.

window.document.location.port
P Lysenius
  • 1,133
  • 1
  • 13
  • 25