Search code examples
javascriptgetlocationipbookmarklet

Bookmarklet - Var from external js


so I have this code:

var myip = "get.user.ip.from.anywhere";
var loop = true;
while (loop) {
    var ip = window.prompt("Insere o endereço de IP", myip);
    if (ip) {
        if (ip.match(/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i)) {
            var loop = false;
            var site = confirm("Escolhe um website.\nOK - IPLocation\nCancel - WhatsMyIp");
            if (site === true) {
                window.location = "http://www.iplocation.net/index.php?query=" + ip;                
            } else {
                window.location = "http://www.whatsmyip.org/ip-geo-location/?ip=" + ip;               
            }
        } else {
            alert("Insere um IP em condições, por favor.");
        }
    } else if (ip === null) {
        var loop = false;
    } else {
        alert("Insere um IP, por favor.");
        var loop = true;
    }
}

What I want is to create a var with a value from an external js.

I want to create a var called myip with the ipaddress from http://l2.io/ip.js?var=myip

How is that possible? And is that possible?


Solution

  • Just figured out how to create a var from an external source.

    var imported = document.createElement('script');
    imported.src = 'http://l2.io/ip.js?var=myip';
    document.head.appendChild(imported);
    

    Then I have the var myip to play with.