0

Had this closed once as a duplicate, yet the so-called duplicate DID NOT actually address my whole question.

I have found this script that, when run inline, returns your IP.

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

http://l2.io/ip.js Has nothing more than a line of code that says

document.write('123.123.123.123');

(But obviously with the user's IP address)

I want to use this IP address as a return string for a function DEFINED EXTERNALLY, BUT STILL ON MY DOMAIN. That is, I have a "scripts.js" that contains all the scripts I wish to use, and I would like to include it in that list as a local function that calls to the 12.io function, but javascript won't allow the <> tags, so I am unsure as to how to do this.

I.e.

function getIP() {
    return (THAT SCRIPT'S OUTPUT);
}

This is the topic this was supposedly a duplicate of, and it is very similar.

Get client IP address via third party web service

However, this DOES NOT address defining as a forwarded script it in my own script file.

Community
  • 1
  • 1
Aidan
  • 27
  • 6
  • Hm, I'm tempted to close this as a duplicate again - the answer basically is **read [the docs](http://l2.io/) of the site you are using**. – Bergi Jun 06 '14 at 03:20

1 Answers1

2

As you seem to want your site's users' IP to be used, you'll need to have client-side code to have their browsers make the request to http://l2.io/ip.

You will need AJAX. Using jQuery's AJAX API:

$.get("http://l2.io/ip.js")
    .done(function(ip) {
        // do whatever you like with the ip...
    })
    .fail(function(err) {
        console.log(err);
    }); 

According to Bergi (and with reason), the data should never leave the callback to avoid synchronism problems. It's advisable to use promises.

gchiconi
  • 624
  • 1
  • 7
  • 17
  • 1
    Simply don't load `http://l2.io/ip.js` but [`http://l2.io/ip`](http://l2.io/ip) - there's no need to extract the IP address. – Bergi Jun 06 '14 at 03:14
  • I don't know the site; I'm using what the OP gave me! Thanks, anyway. – gchiconi Jun 06 '14 at 03:15
  • It's basically described in the duplicated he linked :-) – Bergi Jun 06 '14 at 03:18
  • Gah, that was edited after I answered, or while I was. – gchiconi Jun 06 '14 at 03:20
  • Ok, only the relevant part's left. – gchiconi Jun 06 '14 at 03:21
  • Btw, try not to make `ip` a global variable. [ajax is asynchronous](http://stackoverflow.com/q/14220321/1048572), and this assignment might lead the OP on the wrong path. – Bergi Jun 06 '14 at 03:22
  • No point setting ip as a private variable to the anonymous function. Unless you mean declaring it inside the `done` function as `window.ip`... – gchiconi Jun 06 '14 at 03:24
  • Irregardless of whether that variable is global or module-scoped, the rule of thumb is that the value should never ever leave a callback. You might want to return a promise. – Bergi Jun 06 '14 at 03:28