-4

したがって、この非常に単純なスイッチケースを取得しました。ローカル変数から値を関数にインポートします。主な機能は、フランス語から英語のエイリアスに切り替えて、値をJavaScriptに返すことです...

なんらかの理由でページの ALIAS がケースに表示されない場合は、デフォルトに移動して 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 に答える