【发布时间】:2010-01-26 19:50:37
【问题描述】:
我有两个脚本可以做我想做的事,但我需要将它们组合起来。基本上,如果有一个“美国”的 cookie,那么它让用户浏览美国网站。但是,如果不存在 cookie,那么我希望它根据位置运行地理重定向。他们都在那里自己工作,但我不能在我的一生中让它先做 cookie 部分,然后如果不存在 cookie 则运行地理重定向。任何帮助都会非常巨大。谢谢。
这是我的 2 个单独的脚本:
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="Javascript" type="text/javascript">
<!--
function ReadCookie() {
var NameOfCookie="Language";
if(document.cookie.length > 0)
{
begin = document.cookie.indexOf(NameOfCookie+"=");
if(begin != -1)
{
// our cookie was set.
// The value stored in the cookie is returned from the function
begin += NameOfCookie.length + 1;
end = document.cookie.indexOf(";",begin);
if(end == -1) end = document.cookie.length;
language=(document.cookie.substring(begin,end));
if (language==="US")document.location.href='http://www.site.com';
}
}
}
function SetCookie(cookieName,cookieValue) {
var today = new Date();
var expire = new Date();
var nDays=365
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
//-->
</script>
<script language="Javascript" type="text/javascript">
function GetGeo() {
var country = geoip_country_code();
if(country=="GB")
{
window.location = "http://uk.site.com"
}
else if(country=="FR")
{
window.location = "http://fr.site.com"
}
}
</script>
然后我做 body onload = ReadCookie()
【问题讨论】:
标签: javascript redirect function cookies