views:

245

answers:

1

Hello,

I have two scripts that do what I want, but I need to combine them. Basically if there is a cookie for "US" then it let's the user browse the US site. However if no cookie exists then I want it to run the Geo redirect based on location. They both work on there own but I can not for the life of me get it to do the cookie part first then if no cookie exists run the geo redirect. Any Help would be massively huge. Thanks.

Here is my 2 separate scripts:

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"&gt;&lt;/script&gt;

<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>

Then I do body onload = ReadCookie()

A: 

Here's a much simpler way to get your cookie value:

var language = document.cookie.replace(/.*\bLanguage=(\w+)\b.*/, "$1");
if (language == "US") document.location = whatever;

Where/when do you actually call the "GetGeo" function?

Pointy
Or even: `if (/\bLanguage=US\b/.test(document.cookie)) ...`
Ates Goral
I don't think I'm calling the GetGeo function yet. It works on it's own if I call it in the body = onload, but I am wanting to combine it into the function ReadCookie in the instance that the cookie is empty. Because I need the script to read the cookie and if the US cookie exists then do that action, however if no cookie exists I want it to run the function GetGeo - make sense?
Chase