Capturing visitor's IP in javascript?

Discussion in 'JavaScript' started by krishmk, Feb 24, 2011.

  1. #1
    I am trying to use json to caputure visitor's IP address

    $.getJSON("http://jsonip.appspot.com?callback=?",
        function(data){
    window.alert(data.ip);
      });
    Code (markup):
    The above code displays the IP address in alert box.
    However I want to capture the result in a javascript variable

    $.getJSON("http://jsonip.appspot.com?callback=?",
        function(data){
    var ip = data.ip;
      });
    Code (markup):
    but that doesn't work. Really need some help on how to capture the json results in a custom javascript variable
     
    krishmk, Feb 24, 2011 IP
  2. Buttondown

    Buttondown Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It should work just fine. Perhaps you should change the scope of your variable.

    
    var ip;
    $.getJSON("http://jsonip.appspot.com?callback=?",
        function(data){
    ip = data.ip;
    alert("My ip is " + ip);
      });
    
    Code (markup):
     
    Buttondown, Feb 27, 2011 IP