Search code examples
javascriptphpmaxmind

How to get ip to information with maxmind javascript


I want to get user's ip, location, city, country etc with maxmind javascript but I got null there are any mistake by me?

also I have a question may I place maxmind javascript to my plugin for unlimited use?

My javascript here:

<script src="http://j.maxmind.com/app/geoip.js"></script>
<script>
    $("#country").html(document.write(geoip_country_code()));
    $("#countryname").html(document.write(geoip_country_name()));
    $("#city").html(document.write(geoip_city()));
    $("#region").html(document.write(geoip_region()));
    $("#regionname").html(document.write(geoip_region_name()));
</script>

HTML HERE

<div id="country">&nbsp;</div>
<div id="countryname">&nbsp;</div>
<div id="city">&nbsp;</div>
<div id="region">&nbsp;</div>
<div id="regionname">&nbsp;</div> 

Solution

  • Why are you using document.write inside a jQuery command? The following outputs my location correctly. As per the usage limits, you'll have to read their terms of service.

    $("#country").html( 'Country: ' + geoip_country_code() );
    $("#countryname").html( 'Country name: ' + geoip_country_name() );
    $("#city").html( 'City: ' + geoip_city() );
    $("#region").html( 'Region: ' + geoip_region() );
    $("#regionname").html( 'Region name: ' + geoip_region_name() );
    div:nth-of-type(odd) {
        background: #e0e0e0;
    }
    div:nth-of-type(even) {
        background: #a5a5a5;
    }
    div {
        padding: 5px;
        margin: 5px;
    }
    <script src="http://j.maxmind.com/app/geoip.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="country"></div>
    <div id="countryname"></div>
    <div id="city"></div>
    <div id="region"></div>
    <div id="regionname"></div>