0

Here is an api that gives ip address.

https://jsonip.com/

I want ip address in variable for my application. I can get it like that

window.onload = function () {
   var script = document.createElement("script");

   script.type = "text/javascript";

   script.src = "https://jsonip.com/?callback=DisplayIP";
   document.getElementsByTagName("head")[0].appendChild(script);
};
function DisplayIP(response) {
   document.getElementById("ipaddress").innerHTML = "Your IP Address is " + 
   response.ip;     
alert(response.ip);  // alerts  ip address
}

I have to use it with Sharpspring forms so the above method can't be integrated with sharpspring code. I want something like that

var ip = 'ip address';

For your easy understanding, here is the sharpspring form code

<script type="text/javascript">
    var ss_form = {'account': 'MzawMDEzNjI0BwA', 'formID': 'SzQ1MTAzSEzSNTG2NNQ1STJN1k1KMjfVNTIzSwbCJKMUS2MA'};
    ss_form.width = '100%';
    ss_form.height = '1000';
    ss_form.domain = 'app-3QNBWW1ZDA.marketingautomation.services';
   ss_form.hidden = {'field_3270188034': 'ip address'}; //here I want to use that ip
</script>
<script type="text/javascript" src="https://koi-3QNBWW1ZDA.marketingautomation.services/client/form.js?ver=1.1.1"></script>
3
  • 1
    Where is ss_form used later? Is it a hardcoded global variable read by the koi-... script? Commented Mar 2, 2019 at 9:16
  • ss_form is used in form.js file that is included at the end. it is SS provided code Commented Mar 2, 2019 at 9:18
  • Why don't you set the ss_form.hidden in the DisplayIP function? Commented Mar 2, 2019 at 9:21

1 Answer 1

1

Because the script require the use of document.write, I don't think it's possible in a single document. Rather, you can fetch the IP on the initial page load, then load the page again and synchronously create the ss_form object, and inject the form.js:

if (!sessionStorage.ip) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "https://api.ipify.org?format=jsonp&callback=DisplayIP";
  document.head.appendChild(script);

  window.DisplayIP = function DisplayIP(response) {
    sessionStorage.ip = response.ip;
    window.location.href = window.location.href;
  }
} else {
  const ip = sessionStorage.ip;
  sessionStorage.removeItem('ip');
  window.ss_form = {
    account: 'MzawMDEzNjI0BwA',
    formID: 'SzQ1MTAzSEzSNTG2NNQ1STJN1k1KMjfVNTIzSwbCJKMUS2MA',
    width: '100%',
    height: '1000',
    domain: 'app-3QNBWW1ZDA.marketingautomation.services',
    hidden: {
      'field_3270188034': ip
    }
  };
  const script = document.createElement('script');
  script.src = 'https://koi-3QNBWW1ZDA.marketingautomation.services/client/form.js?ver=1.1.1';
  document.head.appendChild(script);
}

(remove the current form.js from your HTML)

Sign up to request clarification or add additional context in comments.

7 Comments

form is not displayed by using above code. Form should be displayed and it should have a hidden field with ip address. you can check it on your end also. thanks
Do you mean the script isn't running, or what? Are there any errors? I don't have access to your development environment, so I can't see what's going on on your end
code is running. no errors shown. but form is not being displayed. you can test my above form code on your end. it will display a form.
I see, though there actually is an error: Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
I think there's no way to do this asynchronously - you have to get the value you need on one pageload, then initialize another pageload, see edit
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.