2010-09-25 3 views
0

Je suis nouveau à ce sujet et j'ai joué, je me demandais si je pouvais rediriger automatiquement les gens de la page de destination à une page qui aurait des informations sur leur état.Est-il possible de rediriger automatiquement les utilisateurs vers une page différente en fonction de leur état?

Comme url.com/ à url.com/michigan.htm

Le code actuel que j'ai trouvé

<script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet"></script> 
<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> 

Je constate que ce que je dois faire pour ajouter plus États à ce code, je suis sûr que son souper facile, mais comme je l'ai dit que je suis nouveau à ce

Merci

+0

Java n'est pas la même chose que JavaScript. J'ai corrigé l'étiquette dans votre question. – casablanca

Répondre

2

Je suggère d'utiliser une déclaration switch pour le faire ea sy pour ajouter autant d'états que vous voulez. En outre, window.open ouvre une nouvelle fenêtre, ce qui peut être gênant, il est donc préférable d'utiliser window.location pour rediriger l'utilisateur dans la même fenêtre.

Remplacer ceci:

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"); 
} 

avec quelque chose comme ceci:

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; 
} 
0

essayez ceci:

<script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet"></script> 
<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> 
Questions connexes