繁体   English   中英

根据位置弹出(特别是英国)

[英]Pop-up based on location (Specifically the UK)

我有一个非常国际化的网站,但我需要专门为我们的英国客户制作弹出窗口。

我需要的是:

在页面加载:来自英国的用户?

如果是,则显示div。

其他

Div仍然隐藏着。

你可以使用freegeoip来做到这一点 既然您提到要使用纯JavaScript(而不是jQuery),那么您应该使用JSONP来获取国家/地区:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>UK localisation</title>
</head>
<body>

<div id="myDiv" style="display:none">
    <h1>Kittens</h1>
</div>

<script>
    function toggleDiv(content) {
        console.log(content.country_code);
        if(content.country_code === 'GB')   //Or GBR, or UK, I'm not sure.
        {
            document.getElementById('myDiv').style.display = "inline";
        }
        else
        {
            alert("You are not from UK, you are from " + content.country_code);
            document.getElementById('myDiv').style.display = "none";
        }
    }

    window.onload = function()
    {
        // create script element
        var script = document.createElement('script');
        // passing src with callback name
        script.src = 'http://freegeoip.net/json/?callback=toggleDiv';
        // insert script to document and load content
        document.body.appendChild(script);
    }
</script>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM