-4

所以我得到了这个非常简单的 switch case,从一个局部变量我将值导入到函数中,它的主要功能是从法语切换到英语别名并将值返回到我的 javascript...

如果由于某种原因页面的别名在这种情况下没有计算出来,它应该转到默认值并触发我的 jsbreak 函数,但是在我的情况下,即使检测到别名并将变量切换为新值,它仍然会去默认并执行代码...

function langToggle(currentAlias) {
    switch(currentAlias) {
    //Switch the variable from English to French and vice versa depending on the current page's URL string when the toggle js link is clicked
        //If ENGLISH switch the variable to French
        //If ENGLISH switch the variable to French
        case "about-us": currentAlias = "a-notre-sujet"; break;
        //If FRENCH switch the variable to French
        case "a-notre-sujet": currentAlias = "about-us"; break;
        /* -------------------------------------[ See the first two comments ]---------------------------------- */
        case "facilities-and-security": 
            currentAlias = "installations-et-securite"; break; 
        case "installations-et-securite": 
            currentAlias = "facilities-and-security"; break;
        /* -------------------------------------[ See the first two comments ]---------------------------------- */
        case "offenders": 
            currentAlias = "delinquants"; break; 
        case "delinquants": 
            currentAlias = "offenders"; break;
        /* -------------------------------------[ See the first two comments ]---------------------------------- */
        case "you-and-csc": 
            currentAlias = "scc-et-vous"; break; 
        case "scc-et-vous": 
            currentAlias = "you-and-csc"; break;
        /* -------------------------------------[ See the first two comments ]---------------------------------- */
        case "connecting": 
            currentAlias = "etablir-des-liens"; break; 
        case "etablir-des-liens": 
            currentAlias = "connecting"; break;
        /* -------------------------------------[ See the first two comments ]---------------------------------- */
        case "resources": 
            currentAlias = "ressources"; break; 
        case "ressources": 
            currentAlias = "resources"; break;
        /*--------------------------------------[ See the first two comments ]---------------------------------- */
        case "international-transfers": 
            currentAlias = "transferements-internationaux"; break; 
        case "transferements-internationaux": 
            currentAlias = "international-transfers"; break;
        /* -------------------------------------[ See the first two comments ]---------------------------------- */
        case "educational-resources": 
            currentAlias = "ressources-pedagogiques"; break; 
        case "ressources-pedagogiques": 
            currentAlias = "educational-resources"; break;
        /* -------------------------------------[ See the first two comments ]---------------------------------- */
        case "cfp": 
            currentAlias = "pfc"; break; 
        case "pfc": 
            currentAlias = "cfp"; break;
        default: alert("the no matching alias");
    }
    //Return the value of the updated Alias to the language toggle script
    return currentAlias;
}

这是为那些说它可能被窃听的人调用切换脚本的 OTHER 函数?

function jsabort(){
    throw new Error('This is not an error. This is just to abort javascript');
}   
function js_changeit(){ 
    var mainName = String(window.location);
    var dash = mainName.lastIndexOf("-");
    var slash = mainName.lastIndexOf("/");  
    var dot = mainName.lastIndexOf(".");
    var name = mainName.substring(slash+1,dot);
    var ext = mainName.substring(dot,mainName.length);
    var lang = name.substring(name.length-3,name.length);
    var urlSection = mainName.split("/");
    var currentAlias = urlSection[3];
    var currentSite = urlSection[2];
    var urlUntilEndAlias = "http://" + currentSite + "/" + currentAlias + "/";
    var mainUrlSplittedAtAlias = mainName.split(urlUntilEndAlias);
    var mainUrlSplittedAtAliasLastSlash  = mainUrlSplittedAtAlias[1];
    if (mainName === "http://internet/index-eng.shtml" || mainName === "http://internet/index-fra.shtml" ) {
        if (lang != "eng") {
            window.open("http://" + currentSite + "/" + "index" + "-eng" + ext, "_self");
        } else if (lang != "fra") {
            window.open("http://" + currentSite + "/" + "index" + "-fra" + ext, "_self");
        }
    } else {
    var lastDash = mainUrlSplittedAtAliasLastSlash.lastIndexOf("-");
    var subSectionUntilEndFilename = mainUrlSplittedAtAliasLastSlash.substring(0,lastDash);
    var UpdatedAlias = langToggle(currentAlias);
    langToggle();
        if (lang != "eng") {
            window.open("http://" + currentSite + "/" + UpdatedAlias + "/" + subSectionUntilEndFilename + "-eng" + ext, "_self");
        } else if (lang != "fra") {
        window.open("http://" + currentSite + "/" + UpdatedAlias + "/" + subSectionUntilEndFilename + "-fra" + ext, "_self");
        }
    }
}
4

2 回答 2

0

就像评论中提到的@Jon 一样,您应该使用查找对象来解决这个问题:

var lookup = {
    "about-us":        "a-notre-sujet",
    "a-notre-sujet":   "about-us",
    "offenders":       "delinquants",
    // ...
};

var langToggle = lookup[ currentAlias ];

if(!langToggle) {
    alert("the no matching alias");
}
于 2012-11-06T14:07:52.390 回答
0

我遇到了同样的问题(即使在这种情况下,解决方案不应该使用带字符串的开关,而是使用 jAndy 建议的表格)。

因此,如果没有语法错误或一些未放置的中断,您应该(可以?)执行以下操作:

function langToggle(currentAlias) {
    var myKey = '' + currentAlias;
    switch(myKey) {
       case 'XPTO':
           //do xpto
           break;
       case 'YPTO':
           //do ypto
           break;
    }
}

在我的情况下,我确定我正在传递字符串,所以这不仅仅是将参数转换为字符串,而是创建一个新的字符串对象。这种方式对我有用。我想给出一个更好的解释为什么它有效但我没有它:) 希望这对任何人都有帮助。

如果有人知道更好的理由,请分享!

于 2013-03-05T18:16:43.557 回答