JS 获取IP地址、IP归属地

2,493 阅读1分钟

最近互联网开始都增加了展示用户ip来源,大势所趋 公司部分业务同步了该需求

*浏览器打开 https://pv.sohu.com/cityjson?ie=utf-8 显示当前用户的ip地址

image.png

示例:(复制代码直接可用)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://statics.jcpeixun.com/common/jquery-1.11.3.js"></script>
  <script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>
</head>

<body>
  <p></p>//显示ip
  <script>
    GetIPAll();
    //获取IP地址,IP归属地
    function GetIPAll() {
      let iPAddress = returnCitySN["cip"];//IP地址:
      let iPAttach = returnCitySN["cip"] + returnCitySN["cname"];//IP归属地
      $("p").html(iPAttach)
    }
  </script>
</body>
</html>