0

I am trying to get customer ip address from my site(visualforce) page, trying to run the following javascript but unable to get the ip address. Any suggestion/improvements are appreciated, Thank you guys.

<script type="application/javascript">
    var ipa;
    function getIP(json) {
        ipa = json.ip;
    }
    function setIp(){
        document.getElementById('00Nc0000003r0ly').value = ipa;
    }
</script>
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"> </script>
    <body onload="setIp()">
    <input  id="00Nc0000003r0ly" maxlength="20" name="00Nc0000003r0ly" size="20" type="hidden"></input> 

2 Answers 2

0

By the time the page loads, the IP address probably isn't visible yet. I would simply set the value in the getIP function instead:

function getIP(json) {
    ipa = json.ip;
    document.getElementById('00Nc0000003r0ly').value = ipa;
}
1
  • Thank you!! @sfdcfox done changes accordingly, not sure why I am not getting the ip address yet.
    – Laxman
    Commented Apr 2, 2019 at 14:11
0

Look into using an Apex Controller and the following statement.

String ipAddress = ApexPages.currentPage().getHeaders().get('True-Client-IP');

Here is a link to a simple example that uses the above code. It will get you what you are looking for.

Example of using True-Client-IP

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .