0

I am using the below code for retrieving the host ip address:

<script type="text/javascript" src="http://l2.io/ip.js"></script>

I need to retrieve the ip address from the above url and construct another URL something like this:

<script type="text/javascript" src="http://<above_ip_address>:8080/MonarchDemo/.."></script>

Both the scripts are present inside the <body> tag of the html file like this:

<html>
    <body>
        <script type="text/javascript" src="http://l2.io/ip.js"></script>
        <script type="text/javascript" src="http://<ip_address>:8080/MonarchDemo/.."></script>
    </body>
</html>

Running this html file, the first script is displaying the correct IP address but I am not able to replace that IP address in the second script. Please guide.

user182944
  • 7,897
  • 33
  • 108
  • 174

4 Answers4

3

I'm not sure I understand the question exactly, but instead of trying to replace a static script tag, why not dynamically create one?

<script type="text/javascript" src="http://l2.io/ip.js?var=ipAddressFound"></script>

var scr = document.createElement("script");

scr.type = "text/javascript";
scr.src = 'http://' + ipAddressFound + ':8080/MonarchDemo/..'; // Use the IP found above
scr.innerHTML = null;
document.body.appendChild(scr);

Credit to this answer for the var creation through script: Get client IP address via third party web service

Community
  • 1
  • 1
What have you tried
  • 11,018
  • 4
  • 31
  • 45
  • Thanks for the guide. agree with this. But the first script is returning me the ip address, how to replace that in the above code you posted? – user182944 Oct 09 '14 at 15:36
  • @user182944 In the above code there is a variable `ipAddressFound` - That would hold the value returned by the first script. Sorry if I'm missing something. – What have you tried Oct 09 '14 at 15:37
  • but how will the save the ip address retrieved by the script `` in the variavle which you named as `ipAddressFound`? – user182944 Oct 09 '14 at 15:40
  • Is not it possible to save it in a variable using the first script ? – user182944 Oct 09 '14 at 15:41
  • @user182944 Notice the "var=" at the end of the script now? – What have you tried Oct 09 '14 at 15:42
  • Ok so what I can understand is: the query string passed here `var=ipAddressFound` in the `src` of the `script` is stored in a variable and the same variable is used later on for dynamically retrieving the IP address right? – user182944 Oct 09 '14 at 15:47
  • 1
    @user182944 Almost. The script determines the IP address of the user, that IP address is then stored in a variable called `ipAddressFound`, and at this point you can do what you want with the IP (in this case use it to load another script). – What have you tried Oct 09 '14 at 16:06
1

You can use jQuery to load your script take a look here

$.getScript('http://<ip_address>:8080/MonarchDemo/..', function(){});
soerium
  • 573
  • 4
  • 12
1

have you tried to load it within an ajax call following the execution of your first script?

check out http://api.jquery.com/jquery.getscript/

try something like:

var getIP = function () {
    var ipadress;
    // code for IP-Adress retrieval here
    return ipadress;
}

// setup the URL
var url = getIP() + ":8080/MonarchDemo/..."
$.getScript( url, function( data, textStatus, jqxhr ) {
    // do want you want to do
    // i.e.
    // console.log( data ); // Data returned
    // console.log( textStatus ); // Success
    // console.log( jqxhr.status ); // 200
    // console.log( "Load was performed." );
});

As an alternative you could just instert the second script block with javascript (document.write ...) on runtime.

1

you will have to dynamically generate the second script tag ..

   var dynamicScript = document.createElement('script');
   var scriptUrl = ":8080/MonarchDemo/..";

   var scripts = document.getElementsByTagName("script");

   //fetch the source and parse out the domain ..

   var domain = scripts[0].getAttribute('src').replace('http://', '').replace('https://', '').split(/[/?#]/)[0];

   dynamicScript.setAttribute('src', "//" + domain + scriptUrl);

   document.body.appendChild(dynamicScript)
Birgit Martinelle
  • 1,869
  • 1
  • 12
  • 9