Hi I found how to get client ip by answer here: Can I perform a DNS lookup (hostname to IP address) using client-side Javascript?
But I don't understand how to use it.
This is what I have:
var user;
if ($('#user-id').length) {
user = $('#user-id').text();
} else {
http://jsonip.appspot.com/?callback=getip
function getip(json){
user = json.ip;
}
}
I don't understand how to include the url and how to use the getip function.
I need to set the user to the ip address in the else.
Thanks!
Using jQuery, you could make a JSONP call:
$.getJSON('http://jsonip.appspot.com/?callback=?',
function(data){
alert(data.ip);
});
Probably easier to understand - an alternative, without jQuery, would be:
<script type="text/javascript">
function getip(data){
alert(data.ip);
}
</script>
<script type="text/javascript" src="http://jsonip.appspot.com/?callback=getip">
</script>
Note that when you include http://jsonip.appspot.com/?callback=getip as a script in your HTML you get valid JavaScript as response:
getip({"ip": "147.234.2.5", "address":"147.234.2.5"});
This line executes your function with the proper parameter. Note that the function's name is given to it by query string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With