Oma*_*bal 3 html javascript internationalization
我正在使用示例中的代码"我怎样才能将用户从多个国家/地区重定向到多个页面?",来自Geobytes的免费JavaScript .
如果来自英国或挪威的访问者正在查看我的网站,则会将其重定向到专门为这些国家/地区制作的网页,但如果访问者来自任何其他国家/地区(澳大利亚,美国),则不会将其重定向到这些国家/地区的网页; 我的网站(正在建设中)仍为空白.
我希望能够将这些非英国非挪威用户发送到正确的网站.
我的示例代码:
<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>
Run Code Online (Sandbox Code Playgroud)
您的案例中的问题是,在您当前的站点(winappleiphone.info)中,您正在检查具有以下变量的国家/地区:
在这些变量中,您只声明了UK和USA变量,但是您尝试使用其他变量而不声明它们并分配任何导致JavaScript错误的值,因此您的脚本有时会失败.
试试这个代码
//* 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'>");
}
Run Code Online (Sandbox Code Playgroud)
如您所见-我可以简单地添加/删除新的重定向页面,而无需复制大量的代码。我提供的某些代码可能不正确,请自行修复