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.

4
  • is first script dynamic?? how are you getting ipaddres Commented Oct 9, 2014 at 15:36
  • No, its not. The first script is a static one. The second script is dynamic based on the IP address returned by the first script Commented Oct 9, 2014 at 15:37
  • do you have some ipaddress in some js variable Commented Oct 9, 2014 at 15:38
  • No I dont have. I need to retrive it fromt eh first script, save it in a variable and then use it in the second script. Commented Oct 9, 2014 at 15:39

4 Answers 4

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

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

7 Comments

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 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.
but how will the save the ip address retrieved by the script <script type="text/javascript" src="http://l2.io/ip.js"></script> in the variavle which you named as ipAddressFound?
Is not it possible to save it in a variable using the first script ?
@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).
|
1

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

$.getScript('http://<ip_address>:8080/MonarchDemo/..', function(){});

Comments

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.

Comments

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)

Comments

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.