我是新手并且一直在玩,我想知道我是否可以自动将人们从着陆页重定向到一个页面,其中包含有关其状态的信息。
像 url.com/ to url.com/michigan.htm
我找到的当前代码
<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>
我注意到我需要做些什么来为这段代码添加更多状态,我确信它的晚餐很简单,但就像我说我是新手一样
由于
答案 0 :(得分:2)
我建议使用switch
语句,以便轻松添加任意数量的状态。此外,window.open
会打开一个新窗口,这可能很烦人,因此最好使用window.location
在同一窗口中重定向用户。
替换它:
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");
}
有这样的事情:
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;
}
答案 1 :(得分:0)
试试这个:
<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>