2011-01-30 188 views
0

我想写Opera用户JS来替换页面中表示的IP地址作为文本其IP位置。 Google搜索了一段时间后,我发现一项服务提供了我所需要的信息。如何用页面中的IP地址替换IP地址中的IP地址(例如“国家,地区,城市”)

此服务为HTTP查询返回包含所需信息元的页面的标题。
例如,此查询:http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=142.34.25.111
(请参阅页面的源代码)
在浏览器中,此请求正常工作。
我试图通过AJAX获取此文本(如XML),但遇到错误ReferenceError: Security violation在行xmlhttp.send();

所以,我的问题是:
- 是现有的任何做法通过JS来获得IP地址的IP位置(没有任何费用||注册)
- 我怎样才能更换,以修改一个页面的某些内容通过JS的另一个页面的内容?

PS。我在JS编程中很新。

<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> 

包括测试页。

好的,非常感谢!
可能是我的问题不够清楚。
让我们再试一次(我告诉过我自己):
我需要JS功能,如IPAddress2IPLocation(InIPAddress),它使用传递的参数返回格式为Country, Region, City的IP位置。

有人知道我的问题的答案吗?
在这个时候(上午3:50)我批准了任何建议:)
但是,无论如何,这不是我的生活 - 这只是有趣的关于JS。

+0

你为什么不把源代码,所以我们可以看到,如果有一个错误?显然你已经找到了一种方法去做你所要求的。 – Mauricio 2011-01-30 00:07:49

回答

1

ReferenceError: Security violation不要求您支付费用。你正在做Cross-site Scripting这是大多数浏览器的安全违规。

您可以使用代理服务器避免此错误。