我如何根据访问者所在的国家引导他们访问网页';从何处重新审视?(JavaScript)
我使用的代码来自示例“如何将多个国家的用户分别重定向到多个页面?”,这是一个免费的JavaScript 如果来自英国或挪威的访问者正在查看我的网站,他会被重定向到专门为这些国家制作的页面,但如果访问者来自任何其他国家(澳大利亚、美国),他不会被重定向到这些国家的页面;仍为空白 我想能够发送这些非英国非挪威用户到正确的网站 我的示例代码:我如何根据访问者所在的国家引导他们访问网页';从何处重新审视?(JavaScript),javascript,html,internationalization,Javascript,Html,Internationalization,我使用的代码来自示例“如何将多个国家的用户分别重定向到多个页面?”,这是一个免费的JavaScript 如果来自英国或挪威的访问者正在查看我的网站,他会被重定向到专门为这些国家制作的页面,但如果访问者来自任何其他国家(澳大利亚、美国),他不会被重定向到这些国家的页面;仍为空白 我想能够发送这些非英国非挪威用户到正确的网站 我的示例代码: <head> <script language="Javascript" src="http://gd.geobytes.com/G
<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>
var UK=“UK”;
var Norway=“否”;
if(类型(sGeobytesLocationCode)!=“未定义”)
{
var scontrycode=sGeobytesLocationCode.substring(0,2);
如果(英国索引of(搜索代码)>=0)
{
//英国游客会来这里
文件。填写(“”);
}
else if(Norway.indexOf(scontrycode)>=0)
{
//挪威游客会来这里
文件。填写(“”);
}
其他的
{
//世界各地的游客都会来这里
文件。填写(“”);
}
}
// }
您的问题是,在当前站点(winappleiphone.info)中,您正在检查具有以下变量的国家/地区:
- 英国、美国、加拿大、sPhilippineLocations、sKoreanLocations、sChineseLocations
在这些变量中,您只声明了UK和USA变量,但您尝试使用其他变量,而不声明它们并分配任何值,这会导致JavaScript错误,因此脚本有时会失败。页面也会变为白色
typeof(sGeobytesLocationCode) == undefined
//It is actually undefined...
或
抛出一个错误
因此,将else语句置于实际未定义的大小写,并检查sGeobytesLocationCode的长度,以确定其长度实际上是0还是1
if((typeof(sGeobytesLocationCode)!= "undefined") && (sGeobytesLocationCode.length > 1))
{
//bla bla
}
else
{
//Go to default page instead of showing white
}
这些是基本信息,或者询问用户想要进入哪个站点,而不是显示一个白色页面。:)试试下面的代码
//* 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'>");
}
//*用于重定向的初始位置代码
var find=false,
重定向={
英国:'http://www.adworkmedia.com/go.php?camp=3135&pub=13435&id=7547&sid=',
美国:'http://www.adworkmedia.com/go.php?camp=2907&pub=13435&id=7038&sid=',
CA:'French.htm',//加拿大
PH:'Philippine.htm',//菲律宾
KO:'Korean.htm',//Korea
CH:'Chinese.htm'//中国
};
//*检查位置代码
if(类型(sGeobytesLocationCode)!=“未定义”)
{
var scontrycode=sGeobytesLocationCode.substring(0,2);
for(重定向中的var i){
if(i==scontrycode){
文件。填写(“”);
finded=true;
}
}
}
//*未找到位置代码-重定向到默认页面
如果(!find){
文件。填写(“”);
}
如您所见,我可以简单地添加/删除新的重定向页面,而无需复制大量kBs的代码。我提供的一些代码可能不正确,请自行修复,如果需要如果sGeobytesLocationCode未定义,则不进行重定向。另外,当你有很多地方要比较时,你的if-else语句就会开始失控。br3t有一个很好的解决方案。您还应该确保sGeobytesLocationCode是长度为2或更大的字符串,并将其转换为大写(以防万一)
重定向={
英国:'http://www.adworkmedia.com/go.php?camp=3135&pub=13435&id=7547&sid=',
美国:'http://www.adworkmedia.com/go.php?camp=2907&pub=13435&id=7038&sid=',
CA:'French.htm',//加拿大
PH:'Philippine.htm',//菲律宾
KO:'Korean.htm',//Korea
CH:'Chinese.htm'//中国
};
if(typeof(sGeobytesLocationCode)=='string'&&sGeobytesLocationCode.substring.length>1)
{
var scontrycode=sGeobytesLocationCode.substring(0,2).toUpperCase();
for(重定向中的var i){
if(i==scontrycode){
文件。填写(“”);
返回;
}
}
}
//*位置代码未重定向-重定向到默认页面
文件。填写(“”);
当sGeobytesLocationCode
未定义时,您的逻辑无法解释这种情况。此外,当sGeobytesLocationCode
不是字符串或少于两个字符时,可能会发生错误。下面的代码首先设置默认url,假设用户不在英国或挪威。然后,仅当sGeobytesLocationCode
通过多个条件时,才会更改此url。没有不重定向用户的路径。我还添加了逻辑来检查并确保substring
和indexOf
中不会出现错误。此外,重定向逻辑只发生在一个位置,而不是在每个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>
var UK=“UK”;
var Norway=“否”;
//默认url,如果国家不是英国或挪威
变量url=”http://www.google.com/ncr";
if(typeof(sGeobytesLocationCode)=“string”和&sGeobytesLocationCode.length>=2)
{
var scontrycode=sGeobytesLocationCode.substring(0,2);
如果(英国索引of(搜索代码)>=0)
{
//英国游客会来这里
url=”http://www.google.co.uk";
}
else if(Norway.indexOf(scontrycode)>=0)
{
//挪威游客会来这里
url=”http://www.google.no";
}
}
文件。填写(“”);
注意:我提供的代码使用了您的确切代码,并进行了一些小的更改,以使您的案例能够正常工作。但是,您的代码可以编写得更好。考虑以下事项:
- 并非所有浏览器都支持
indexOf
。您可能需要提供一个
默认实现,以支持所有浏览器。你
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'>");
<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>
<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>