Javascript Multiple If.Else



我们花了很多时间为我们的网站使用搜索引擎优化,如果一个移动用户正在使用其中一个搜索引擎,我们试图将移动用户重定向到移动设计的网页,这些网页反映了在子文件夹/mobile/中设置的正常网页。

在主要的非移动页面上,变量被捕获:

<% ThisPage = Request.ServerVariables("URL") %>
<% ThisVAR = "?" & Request.Querystring %>

这些变量转到一个包含文件,以检查用户是否是移动用户,如果是,重定向到移动用户可能正在搜索的页面,以移动格式存在于我们的站点。如果用户在移动设备上,而我们没有他们正在搜索的页面的移动版本,它应该重定向到移动文件夹root。

<script type= "text/javascript">
if (screen.width <= 481)
{
    if (ThisPage = "/faqs.asp") { document.location = "../mobile/menu_faq.asp" }
    else {
    if (ThisPage = "/search.asp") { document.location = "../mobile/mobile_search.asp" }
else {
    if (ThisPage = "/mte_contacts.asp") { document.location = "../mobile/menu_contacts.asp" }
else {
    if (ThisPage = "/mte_history.asp") { document.location = "../mobile/menu_history.asp" }
    else {
    if (ThisPage = "/mte_locations.asp") { document.location = "../mobile/menu_locations.asp" }
else {
    if (ThisPage = "/mte_shipping.asp") { document.location = "../mobile/menu_shipping.asp" }
else {
    if (ThisPage = "/shop_discontinued.asp") { document.location = "../mobile/mobile_discontinued.asp" }
else {
    if (ThisPage = "/shop_category.asp") { document.location = "../mobile/mobile_category.asp" + ThisVAR }
else {
    if (ThisPage = "/shop_commodity.asp") { document.location = "../mobile/mobile_commodity.asp" + ThisVAR }
else { document.location = "../mobile/" }
}
}
}
}
}
}
}
}
}
</script>

我一直在寻找解决方案的javascript如果…我有其他问题,我找不到任何东西,纠正我的语法错误,我在我的代码。我得到重定向工作,但它不会去正确的移动页面,我不能得到正确的逻辑到位。任何帮助都会很感激。目前,在我的移动设备上测试去/mnt_history.asp文件,但它重定向到/mobile/menu_faq.asp

比起使用一堆if-else,查找表可能是更好的方法。它更紧凑,更容易在一个地方看到所有映射。

var mobileMapper = {
    "/faqs.asp"              : "../mobile/menu_faq.asp",
    "/search.asp"            : "../mobile/mobile_search.asp",
    "/mte_contacts.asp"      : "../mobile/menu_contacts.asp",
    "/mte_history.asp"       : "../mobile/menu_history.asp",
    "/mte_locations.asp"     : "../mobile/menu_locations.asp",
    "/mte_shipping.asp"      : "../mobile/menu_shipping.asp",
    "/shop_discontinued.asp" : "../mobile/mobile_discontinued.asp",
    "/shop_category.asp"     : "../mobile/mobile_category.asp" + ThisVAR ,
    "/shop_commodity.asp"    : "../mobile/mobile_commodity.asp" + ThisVAR 
};
if (screen.width <= 481) {
    document.location = mobilMapper[ThisPage] || "../mobile/";
}

最新更新