【问题标题】:How do I direct visitors to pages based on the country they're viewing from? (JavaScript)如何根据访问者查看的国家/地区将访问者引导至页面? (JavaScript)
【发布时间】:2012-12-29 20:07:12
【问题描述】:

我正在使用示例“我如何将来自多个国家/地区的用户分别重定向到多个页面?”中的代码,来自Geobytes 的免费 JavaScript。

如果来自英国或挪威的访问者正在查看我的网站,他会被重定向到专门为这些国家/地区制作的页面,但如果访问者来自任何其他国家/地区(澳大利亚、美国),他不会被重定向到这些国家/地区的页面国家; my site (under construction) 保持空白。

我希望能够将这些非英国非挪威用户发送到正确的站点。

我的示例代码:

 <head>
    <script language="Javascript" src="http://gd.geobytes.com/Gd?after=-1"></script>
    <script language="javascript">
    var UK="UK";
    var Norway="NO";

    if(typeof(sGeobytesLocationCode)!="undefined")
    {
       var sCountryCode=sGeobytesLocationCode.substring(0,2);

          if(UK.indexOf(sCountryCode)>=0)
          {
             // UK Visitors would go here
             document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=http://www.google.co.uk'>");
          }
          else if(Norway.indexOf(sCountryCode)>=0)
          {
             // Norway Visitors would go here

             document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=http://www.google.no'>");
          }



else
          {
             // World Visitors would go here
             document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=http://www.google.com/ncr'>");
          }
       }
  //  }
    </script>
    </head>

【问题讨论】:

  • 你的问题是?请提供更多信息......究竟什么不起作用?问题与geobytes API 有关吗?
  • 就像我在问题中问的那样,如果访问者是从英国或挪威查看我的网站,他会被重定向到专门为这些国家/地区制作的页面,但如果访问者来自任何其他国家/地区,则说他不是奥地利重定向到其他国家/地区的页面。
  • 所以这一切都取决于'geobytes' API?
  • 我不明白,是不是我的 If-else 语句有问题?请检查一下。
  • 这个 js 代码很糟糕/很奇怪。 if(UK.indexOf(sCountryCode)&gt;=0) - 为什么不是if (sCountryCode === 'UK')

标签: javascript html internationalization


【解决方案1】:

您的问题是,在您当前的网站 (winappleiphone.info) 中,您正在检查具有以下变量的国家/地区:

  • 英国、美国、加拿大、sPhilippineLocations、sKoreanLocations、sChineseLocations

在这些变量中,您只声明了 UK 和 USA 变量,但您尝试使用其他变量而不声明它们并分配任何导致 JavaScript 错误的值,因此您的脚本失败有时。

【讨论】:

  • huh????...我不认为是这种情况,并且(在原始代码中)我已经宣布了 28 个国家,包括英国和美国。为什么 IF 语句不导致任何错误,但同一行,而在 else 中则根本没有发生任何事情。
  • 看,你所面临的错误就像检查浏览器的错误控制台或查看 Firebug 的控制台(在 Firefox 中)......或其他浏览器中的其他相应开发人员工具一样简单。在大多数浏览器上打开该调试控制台的快捷方式是 F12 您可以看到报告的错误如下:(在英国/美国以外的机器上进行测试时...为此,您可能需要对其他国家/地区使用 VPN 等解决方案)错误:ReferenceError:加拿大未定义源文件:winappleiphone.info 行:46
  • 是的,但我确实宣布了这些国家中的每一个:(
  • 可能您的最新文件没有上传...但我希望现在您能够通过查看错误控制台/Firebug 或其他类似浏览器的工具来找到错误...我建议您访问您的站点并右键单击 >“查看页面源代码”...并将其与您本地主机上的源代码进行比较
  • @OmairIqbal:正如 jsbookmarklets.com 所说,你错过了加拿大。它没有定义。看看你的代码。但是你真的应该考虑使用 br3ts 版本。定义所有这些变量是不必要且丑陋的,并且会为您的代码引入大量冗余。
【解决方案2】:

试试这个代码

//* init location codes for redirecting
var finded = false,
    redirections = {
        UK: 'http://www.adworkmedia.com/go.php?camp=3135&pub=13435&id=7547&sid=',
        US: 'http://www.adworkmedia.com/go.php?camp=2907&pub=13435&id=7038&sid=',
        CA: 'French.htm',   // Canada
        PH: 'Philippine.htm',   // Philippines
        KO: 'Korean.htm',   // Korea
        CH: 'Chinese.htm'   // China
    };
//* Checking location code
if(typeof(sGeobytesLocationCode) != "undefined")
{
    var sCountryCode = sGeobytesLocationCode.substring(0,2);
    for(var i in redirections) {
        if(i == sCountryCode) {
            document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=" + redirections[i] + "'>");
            finded = true;
        }
    }
}
//* location code not finded - redirect to default page
if(!finded) {
    document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=World.htm'>");
}

如您所见 - 我可以简单地添加/删除新的重定向页面,而无需复制大量代码。我提供的某些代码可能不正确,如果需要,请自行修复

【讨论】:

    【解决方案3】:

    让页面变白

    typeof(sGeobytesLocationCode) == undefined 
    //It is actually undefined...
    

     sGeobytesLocationCode.substring(0,2) 
    

    抛出错误。

    所以放一个else语句来处理实际上是未定义的情况,并检查sGeobytesLocationCode的长度来确定它的长度实际上是0还是1.

     if((typeof(sGeobytesLocationCode)!= "undefined") && (sGeobytesLocationCode.length > 1))
     {
          //bla bla
     }
     else
     {
          //Go to default page instead of showing white
     }
    

    这些是基础知识,否则询问用户他们想进入哪个网站而不是显示白页 :)

    【讨论】:

      【解决方案4】:

      在 sGeobytesLocationCode 未定义的情况下,您不会重定向。此外,当您有很多要比较的位置时,您的 if else 语句将开始失控。 br3t 有一个很好的解决方案。您还应该确保 sGeobytesLocationCode 是长度为 2 或更大的字符串,并将其转换为大写(以防万一)。

          redirections = {
              UK: 'http://www.adworkmedia.com/go.php?camp=3135&pub=13435&id=7547&sid=',
              US: 'http://www.adworkmedia.com/go.php?camp=2907&pub=13435&id=7038&sid=',
              CA: 'French.htm',   // Canada
              PH: 'Philippine.htm',   // Philippines
              KO: 'Korean.htm',   // Korea
              CH: 'Chinese.htm'   // China
          };
      if(typeof(sGeobytesLocationCode) === 'string' && sGeobytesLocationCode.substring.length > 1)
      {
          var sCountryCode = sGeobytesLocationCode.substring(0,2).toUpperCase();
          for(var i in redirections) {
              if(i == sCountryCode) {
                  document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=" + redirections[i] + "'>");
                 return;
              }
          }
      }
      //* location code not redirected - redirect to default page
          document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=World.htm'>");
      

      【讨论】:

        【解决方案5】:

        您的逻辑没有考虑sGeobytesLocationCode 未定义的情况。此外,当sGeobytesLocationCode 不是字符串或少于两个字符时,可能会发生错误。下面的代码开始设置默认 url,假设用户不在英国或挪威。然后,仅当sGeobytesLocationCode 满足多个条件时,才会更改此 url。没有不会重定向用户的路径。我还添加了逻辑来检查并确保substringindexOf 中不会出现错误。此外,重定向的逻辑只发生在一个地方,而不是在每个 if-else 块中。

        <head>
            <script language="Javascript" src="http://gd.geobytes.com/Gd?after=-1"></script>
            <script language="javascript">
                var UK="UK";
                var Norway="NO";
        
                // default url, if country is not uk or norway
                var url = "http://www.google.com/ncr";
        
                if(typeof(sGeobytesLocationCode) == "string" && sGeobytesLocationCode.length >= 2)
                {
                    var sCountryCode = sGeobytesLocationCode.substring(0,2);
        
                    if (UK.indexOf(sCountryCode) >= 0)
                    {
                        // UK Visitors would go here
                        url = "http://www.google.co.uk";
                    }
                    else if (Norway.indexOf(sCountryCode) >= 0)
                    {
                        // Norway Visitors would go here
                        url = "http://www.google.no";
                    }
                }
        
                document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=" + url + "'>");
            </script>
        </head>
        

        注意:我提供的代码使用了您的确切代码,并进行了一些小改动以使您的案例正常工作。但是,您的代码可以写得更好。考虑以下几点:

        • 并非所有浏览器都支持indexOf。您可能需要提供一个 默认实现以支持所有浏览器。你可以 google这个,有几个地方给出代码。接着就,随即 说,indexOf 在这种情况下似乎是多余的,因为您已经使用过 substring 仅包含两个字母的国家/地区代码。考虑 取出indexOf 并检查国家代码是否 相等。

        • 为了提高代码的可维护性,您可能需要考虑 添加所有国家/地区代码(到目前为止“NO”和“UK”)及其 对应数组的url。这样你就可以遍历 数组并且只编写一个代码块来进行比较和 设置网址。你现在拥有它的方式,你需要一个 if 语句 您要检查的每个国家/地区代码的重复逻辑。那不是 现在太糟糕了,但是如果您计划将来添加国家/地区代码,它 会很快变丑。

        【讨论】:

          【解决方案6】:

          这似乎工作得很好,而且更紧凑。

          <head>
              <script language="Javascript" src="http://gd.geobytes.com/Gd?after=-1"></script>
              <script language="javascript">
                  var countrySuffix = {
                      //add more specific countries here if you want
                      "UK": 'http://www.google.co.uk',
                      "NO": 'http://www.google.no',
                      //The default location we'll go to if we're not in any of the above
                      //   locations
                      "DEFAULT": 'http://www.google.com/ncr'
                  };
          
                  var sCountryCode;
          
                  if(sGeobytesLocationCode && sGeobytesLocationCode.length > 1){
                      sCountryCode = sGeobytesLocationCode.substring(0,2);
                  }
          
                  var targetSite = countrySuffix[(sCountryCode || "DEFAULT").toUpperCase()] || countrySuffix.DEFAULT;
                  document.write("<META HTTP-EQUIV='Refresh' CONTENT='0; URL=" + targetSite + "'>");
              </script>
          </head>
          

          【讨论】:

            猜你喜欢
            • 2018-07-06
            • 1970-01-01
            • 2019-01-10
            • 2012-10-09
            • 2019-09-26
            • 1970-01-01
            • 2015-01-16
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多