geoPlugin Home

Javascript Web Service

The javascript web service 1) allows you to directly program your front-end web pages to deliver dynamic geo-localized pages using the javascript functions provided by geoPlugin.

To access this service, add the following Javascript call (usually within the <head> block of your pages). Note: this service will only work when embedded in web pages - no server-side calls will work.

 
<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>



Important notice regarding use of the Javascript Web Service.

As with the other web services, use of the Javascript API is strictly controlled by lookup limits as explained in the Acceptable Use Policy.

Use of the Javascript web service more than the allowed lookup limit may result in our advert placement on your pages, or our advert pages in popunder/popup placements, through manipulation of the Javascript in up to 2% of subsequent lookups.
Use of the Javascript web service implies that you understand and accept these limitations.

If you deploy code to third parties that makes use of the geoPlugin Javascript API, you are solely responsible for notifying your users of the limitations in using the geoPlugin Javascript API.



To see the javascript that geoPlugin outputs, view the HTML source of this page

Once added, you will have geo-localized javascript functions available to your entire page.
These functions are as follows:

geoplugin_request()

geoplugin_status()

geoplugin_credit()

geoplugin_city()

geoplugin_region()

geoplugin_areaCode()

geoplugin_dmaCode()

geoplugin_countryCode()

geoplugin_countryName()

geoplugin_continentCode()

geoplugin_latitude()

geoplugin_longitude()

geoplugin_currencyCode()

geoplugin_currencySymbol()

geoplugin_currencyConverter()
    function geoplugin_currencyConverter(amt, symbol) { 
    	 var converted = amt * 1
    	 if (symbol == false) { return Math.round(converted * 100)/100; } 
    	 else { return '' + Math.round(converted * 100)/100; } 
     }


You can call any of these javascript functions in your scripts to output the relevant geo-localized information about your visitor.


Javascript Currency Converter

One things that you will notice comparing the Javascript web service with the other services, is that the geoplugin_currencyConverter Javascript function calculates the actual converted amount, and does not simply give the exchange rate.

This is because in the early days, our users' feedback on the Javascript functions were that where this was most being used was to dynamically provide the price of products in the visitor's currency, and as such a built-in function that took a simple price and output the visitor's currency symbol and local price was much welcomed.

So to dynamically output a local price, simply add the amount you wish in the function call. eg

 
Product A costs $800 
<script language="Javascript">if (geoplugin_currencyCode() != 'USD') {document.write('('+geoplugin_currencyConverter(800)+')');}</script>

Will output


Product A costs $800


The geoplugin_currencyConverter() takes two values:

  1. The amount to convert (numbers only)
  2. Optional boolean value whether to show the local currency symbol in the output (default: true)



So, for example if you don't want to convert anything, but simply require the exchange rate, call geoplugin_currencyConverter(1, false); and the exchange rate will be returned.


Like all calls to any of geoPlugin's web services, the default base_currency is USD ($US).
Thus, if your base currency is NOT $US, then you must add the variable base_currency=XXX to the call to geoplugin.net
eg

http://www.geoplugin.net/javascript.gp?base_currency=EUR

Now geoplugin_currencyConverter will output the exchange rate of one Euro for your visitor.

The base_currency value must be a valid ISO 4217 3-letter code.




See also

1) This product includes GeoLite data created by MaxMind, available from http://maxmind.com/
 
 
Bookmark with:    Digg    StumbleUpon    Del.icio.us    Technorati


Acceptable Use Policy    Contact geoPlugin