8

can anyone tell me how to get host IP and port number on which the web application is running using javascript (e.g. 127.0.0.1:8080)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
pankaj
  • 1,643
  • 4
  • 22
  • 35

3 Answers3

8

I'm afraid it's not possible to directly obtain the IP address via Javascript. It's not exposed in the window.location object.

Part of the reason for that is that subsequently accessing address:port is not semantically the same as accessing hostname:port - they are technically different URLs.

If what you're actually after is the host portion of the URL from which the current webapp was downloaded, you need:

window.location.hostname
window.location.port

The latter could be blank if the "default" port is being used, so you would also need to read:

window.location.protocol

and check whether it's http: (i.e. port 80) or https: (port 443).

You can also use:

window.location.host

which will contain both the hostname and the port as colon-separated strings, with the same caveat as above that the :port section will be omitted if the content was accessed via the "default" port for the protocol.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • thanks for rply. when i use window.location.hostname, it displays the hostname like localhost, but i need actual ip address like 127.0.0.1 – pankaj Sep 26 '12 at 11:11
  • @user1455802 I did wonder, as your question was confusing as to which you wanted (you said "address", but specified "localhost"). I'm afraid you can't get the IP address directly from Javascript. – Alnitak Sep 26 '12 at 11:12
  • actually the problem is when my application is deployed at some ip eg. 127.16.1.247, my websocket works fine. But when I provide this IP a hostname like sample.something.com, websocket does not work Auction.initialize = function() { Auction.connect('ws://' + window.location.host + '/HTML5/bidforanything/'); }; – pankaj Sep 26 '12 at 11:20
  • web application is using the default port 8085. So using window.location.host, it is coming blank. I dont want to hard code 8085 in javascript. How can i get port number?..plz help – pankaj Sep 26 '12 at 11:39
  • @user1455802 how is 8085 the "default"? – Alnitak Sep 26 '12 at 11:51
5
document.location.host      // localhost:1234
document.location.hostname  // localhost
document.location.port      // 1234
Eric Lennartsson
  • 579
  • 3
  • 11
  • 1
    thanks for rply. when i use document.location.host, it displays the hostname like localhost, but i need actual ip address like 127.0.0.1 – pankaj Sep 26 '12 at 11:10
  • There is no way to look up the ip using only javascript. You could, however, use ajax to poll some kind of webservice: http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript – Eric Lennartsson Sep 26 '12 at 11:16
  • From the example the user provided, I think document.location.host will work. I'm assuming that 127.0.0.1:8080 is the url that they are using and trying to get; rather than determining the ip the url resolves to. – Chris McCowan Apr 22 '19 at 18:19
0

In addition to the answers by Eric Lennartsson and Alnitak, you can also use

window.location.protocol

to get the http protocol that has been used.

window being a global variable you can omit window while referring location.

All combined you can get the total IP address by running

var url = location.protocol + '//' + location.host;
console.log(url);

Head over to the Mozilla Docs for more info.