开发者

How to replace IP addresses represented in the page as text with its IP location (as "Country, Region, City" for example)

I'm trying to write Opera user JS for replacing IP addresses represented in the page as text with its IP location. After googling for a while I found a service which provides exactly the information I needed.

This service for a HTTP query returns the header of the page with meta containing the needed info.

As example, this query: http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=142.34.25.111

(see source of the page)

In the browser this request works fine.

I tried to get this text (as XML) via AJAX, but encountered an error ReferenceError: Security violation at line xmlhttp.send();.

So, my questions is:

- Is existing any practice to obtain IP location from IP address via JS (without any fee || registration)

- How I can to replace some content of the one page to modified content of the another page via JS?

PS. I am very new in JS programming.

  <html>
  <head>
  <script type="text/javascript">
  function loadXMLDoc()
  {
  var xmlhttp;
  if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
  else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
          //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
          var x = xmlhttp.responseXML.documentElement.documentElement;
          var i, s, xx;
          s = '';
          for (i=0; i < x.length; i++)
          {
            xx = x[i].documentElement;
            if (xx.attributes[0] == 'country') s = s + xx.attributes[1].textContent;
            if (xx.attributes[0] == 'region') s = s + ', ' + xx.attributes[1].textContent;
  开发者_运维知识库          if (xx.attributes[0] == 'city') s = s + ', ' + xx.attributes[1].textContent;
          }
          document.getElementById("myDiv").innerHTML = s;
        }
    }
  xmlhttp.open("GET","http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=142.34.25.111",false);
  xmlhttp.send();
  }
  </script>
  </head>
  <body>

  <div id="myDiv"><h2>Let AJAX change this text</h2></div>
  <button type="button" onclick="loadXMLDoc()">Change Content</button>

  </body>
  </html>  

Test page included.

Ok, Thanks a lot to all!

May be my question is not clear enough.

Lets try again (I told this to myself):

I need JS function like IPAddress2IPLocation(InIPAddress) which returns IP location in format Country, Region, City using passed parameter.

Is anybody know answer for my question?

At this time (3:50 AM here) I approved for any suggestions :)

But, in any case, it is not extremely bout my life - it is just interesting about JS.


Javascript security! Fear not, there are ways to work around the same origin policy.

Getting around same origin policy in javascript without server side scripts


ReferenceError: Security violation is not asking you to pay a fee. You are doing Cross-site Scripting which is a security violation on most browsers.

You can avoid this error using a proxy server.


This is probably due to accessing another domain from Ajax. For security reasons, only requests to the same domain name can be made.

The code http://www.codeproject.com/KB/aspnet/aspxcode_net.aspx describes the basics for building ip to location translation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜