【问题标题】:Hide page until after async call returns隐藏页面直到异步调用返回
【发布时间】:2015-03-18 22:46:56
【问题描述】:

我们使用MaxMind asynchronous geoip2 service 来确定用户所在的国家/地区。我们根据服务调用中的国家/地区代码重定向到另一个站点。但是,我们遇到了一个问题,即页面在重定向发生在一两秒内之前部分或完全加载,这会在重定向到特定国家/地区的网站之前显示我们的主要美国网站。

我们希望在异步调用返回其结果之前阻止页面显示。这是我们的代码:

<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script type="text/javascript">
    var onSuccess = function (location) {
        var country_code = location.country.iso_code;

        if(country_code  == "UK")
        {
            <!--
            window.location = "http://www.ourcompany.co.uk"
            //-->
        }
        else if(country_code  == "GB")
        {
            <!--
            window.location = "http://www.ourcompany.co.uk"
            //-->
        }
        // more ELSE IF statements for other countries....
    };

    var onError = function (error) { };

    // Asynchronous call to the MaxMind server
    geoip2.country(onSuccess, onError);
</script>

【问题讨论】:

  • 我认为这是不可能的,防止页面加载的唯一方法是window.stop(),但它会取消加载,所以只有刷新才能恢复它。这些事情通常由入口页面处理,然后您可以记住用户在 cookie 中的选择以供将来访问

标签: javascript asynchronous geoip maxmind


【解决方案1】:

不确定您想要阻止页面加载是什么意思,但也许隐藏页面会做到这一点?像这样使用 css:

body {
    display: none;
}

【讨论】:

  • 谢谢@KJ Price。我会改写这个问题。
  • 理想情况下,您应该使用 javascript 应用此功能,否则任何在禁用 js 的情况下访问该网站的人都将看不到任何内容,无论他们是否会被重定向
猜你喜欢
  • 1970-01-01
  • 2017-08-30
  • 2021-08-14
  • 1970-01-01
  • 2018-10-02
  • 2012-05-17
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多