javascript - 有没有一种方法可以根据人们的状态自动将他们重定向到不同的页面?

标签 javascript html redirect

我对此很陌生,一直在尝试,我想知道是否可以自动将人们从登陆页面重定向到包含其状态信息的页面。

喜欢 url.com/至 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>

我注意到我需要做什么来向这段代码添加更多状态,我确信它很简单,但就像我说的,我对此很陌生

谢谢

最佳答案

我建议使用 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;
}

关于javascript - 有没有一种方法可以根据人们的状态自动将他们重定向到不同的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3792460/

相关文章:

html - 停止悬停时 ul-li 的错过行为

javascript - Paypal 重定向到同一个弹出窗口

C# OnClientClick ="aspnetForm.target=' _blank';"

javascript - 避免导入 "regenerator-runtime/runtime"

Javascript v3 Google map 旋转 map

html - 如何调整悬停图像上的 CSS 列表

seo - 修改漂亮 URL 时管理 URL 重定向 - 最佳实践

javascript - AngularJS 中的 Jquery 鼠标 onclick

javascript - Uncaught TypeError : $(. ..).tooltip 不是 Laravel 应用程序中的函数(Bootstrap 4 预设)

javascript - 用户单击链接时 URL 路由不起作用