Reputation: 8067
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.
Upvotes: 0
Views: 1751
Reputation: 1889
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)
Upvotes: 1
Reputation: 11138
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
Upvotes: 3
Reputation: 110
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.
Upvotes: 1
Reputation: 583
You can use jQuery to load your script take a look here
$.getScript('http://<ip_address>:8080/MonarchDemo/..', function(){});
Upvotes: 1