thevan
thevan

Reputation: 10364

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

This is the URL:

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

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

Upvotes: 0

Views: 7863

Answers (2)

Fabrício Matté
Fabrício Matté

Reputation: 70169

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.

Upvotes: 9

P Lysenius
P Lysenius

Reputation: 1133

You don't need jQuery for it.

window.document.location.port

Upvotes: 0

Related Questions