如果地理位置IP来自特定国家则隐藏div

问题描述 投票:0回答:1

例如,如果人们从新西兰查看我们的网站 (www.1907water.com),我会尝试隐藏“ohi_widget”div。

这可能吗?如果可以,需要什么代码?

谢谢

我尝试了 Google 搜索、在线聊天和 Stack Overflow 搜索。我尝试了一种“解决方案”,但它在我们的网站上不起作用,可能是因为我必须自定义它,而且我不懂 javascript。

javascript jquery geolocation
1个回答
2
投票

您可以使用ipapi.co:

fetch('https://ipapi.co/json/')
  .then(response => response.json())
  .then(data => {
    // Check if the country is New Zealand and hide div
    if (data.country === 'NZ') {
      document.getElementById('myDiv').style.display = 'none';
    }
  })
  .catch(error => {
    console.error('Error fetching IP data:', error);
  });
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hide Div for NZ IPs</title>
</head>
<body>
  <div id="myDiv">
    This div will be hidden if your IP suggests you are in New Zealand.
  </div>
  <script src="script.js"></script>
</body>
</html>

使用我的个人美国 IP 和 Surfshark VPN 新西兰 IP 进行验证。

© www.soinside.com 2019 - 2024. All rights reserved.