1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Obtaining Your Visitor's IP Address

Join the Discussion

Questions? Comments?

JavaScript itself has no way of reading the IP address of the local computer and so for JavaScript to obtain that information we need to use a different language to obtain the information. Once possible way of doing this is to call Java to get the information for you.

if (java && java.net)
ip = ''+java.net.InetAddress.getLocalHost().getHostAddress();
else ip = 'unknown';

The problem with doing this is that not all browsers support accessing Java this way from within JavaScript (for example Internet Explorer and Opera do not even though such calls have been a part of JavaScript since JavaScript was first added to Netscape 2). Even on browsers that do support it you can't test the script without first uploading to a web server first as access to the Java object is not available for local files. Even in browsers that do support the call you are reliant on both JavaScript and Java being enabled in order for the command to work. This is therefore not the ideal way to obtain the IP address. Fortunately there are better ways.

The better ways of obtaining the IP address in JavaScript rely on server side processing and so if you have no access to add server side processing to your pages at all then the Java option is really the only one available to you despite its limitations. You do not need much in the way of server side processing capability to be able to always get the IP address and so few people should be in the position of having the Java option as their only choice. Even with just Server Side Includes (SSI) you can obtain the IP address using the following code:

ip = '<!--#echo var="REMOTE_ADDR"-->';

You just need to make sure that the file that contains that code gets parsed for server side includes either by using a .shtml file suffix or by configuring the file suffix you do use appropriately.

The code using PHP is even simpler provided that the file either has a .php suffix or is parsed for PHP.

ip = "<?php echo $_SERVER['REMOTE_ADDR']?>";

The ASP version is somewhat longer:

ip = '<%= Request.ServerVariables("REMOTE_ADDR")%>';

This has been somewhat simplified for those who have upgraded to .NET

ip = '<%= Request.UserHostAddress>';

Java is one language that can run server side as well as client side. Earlier we looked at the client side code for accessing the IP address but there is a simpler way to use Java to get the IP address for JavaScript if your file can be parsed as JSP.

ip = '<%=request.getRemoteAddr()%>';

The final option I am going to illustrate is how to do it using ColdFusion.

ip = '<cfoutput>#cgi.remote_addr#</cfoutput>';

Of course if you are using some other language on the server there is probably a way you can use that to provide the IP address to JavaScript too.

See More About
Related Video
Link a Website to an Email Address in HTML

©2017 About.com. All rights reserved.