views:

40

answers:

2

I am new to this and have been playing around, I was wondering if I could auto redirect people from the landing page to a page that would have information about their state.

Like url.com/ to url.com/michigan.htm

The current code that I've found

    <script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&amp;variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet"&gt;&lt;/script&gt;
<script language="Javascript">
if(typeof(sGeobytesLocationCode)=="undefined"
   ||typeof(sGeobytesCode)=="undefined"
   ||typeof(sGeobytesInternet)=="undefined")
{
   // Something has gone wrong with the variables, so set them to some default value,
   // maybe set a error flag to check for later on.
   var sGeobytesLocationCode="unknown";
   var sGeobytesCode="unknown";
   var sGeobytesInternet="unknown";
}
if(sGeobytesCode=="MI")
{
   // Visitors from Michigan would go here
   window.open("http://www.url.com/michigan.htm" , "_self");
}else if(sGeobytesInternet=="US")
{
   // Visitors from The United States would go here
   window.open("http://www.url.com/selectstate.htm");
}
</script>

I'm note sure what I need to do to add more states to this code, I'm sure its supper easy but like I said I am new to this

Thanks

+2  A: 

I would suggest using a switch statement to make it easy to add as many states as you want. Also, window.open opens a new window, which might be annoying, so it's better to use window.location to redirect the user in the same window.

Replace this:

if(sGeobytesCode=="MI")
{
   // Visitors from Michigan would go here
   window.open("http://www.url.com/michigan.htm" , "_self");
}else if(sGeobytesInternet=="US")
{
   // Visitors from The United States would go here
   window.open("http://www.url.com/selectstate.htm");
}

with something like this:

switch (sGeobytesCode) {
case 'MI': window.location = 'http://www.url.com/michigan.htm'; break;
case 'AA': window.location = 'something'; break;
case 'BB': window.location = 'something else'; break;
...
default:
  if(sGeobytesInternet=="US")
  {
     // Visitors from The United States would go here
     window.location = 'http://www.url.com/selectstate.htm';
  }
  break;
}
casablanca
A: 

try this:

<script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&amp;variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet"&gt;&lt;/script&gt;
<script language="Javascript">
    if (typeof(sGeobytesLocationCode) == "undefined" || typeof(sGeobytesCode) == "undefined"
            || typeof(sGeobytesInternet) == "undefined") {
        // Something has gone wrong with the variables, so set them to some default value,
        // maybe set a error flag to check for later on.
        var sGeobytesLocationCode = "unknown";
        var sGeobytesCode = "unknown";
        var sGeobytesInternet = "unknown";
    }
    var statePageMap = {
        "MI": "michigan.htm",
        "OH": "ohio.htm",
        "IN": "indiana.html"
    };
    var statePage = statePageMap[sGeobytesCode];
    if (statePage != undefined) {
        // Visitors from Michigan would go here
        window.open("http://www.url.com/" + statePage, "_self");
    } else if (sGeobytesInternet == "US") {
        // Visitors from The United States would go here
        window.open("http://www.url.com/selectstate.htm");
    }
</script>
shams