2012-11-06 53 views
-5

所以我得到了這個非常簡單的開關的情況下,從一個局部變量我進口值到函數,它的主要功能是從法語切換到英文別名和值回到我的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"); 
     } 
    } 
} 
+7

開關壞了,或者你的代碼有問題?我知道我將我的錢放在哪裏... –

+2

@MitchWheat:哈哈 - 讓我的一天... – jAndy

+4

爲什麼你使用'switch'這個?用作地圖的普通對象只需要少於一半的代碼量。 – Jon

回答

0

@喬恩一樣在評論中提到的,你應該使用查找對象來解決這個問題:

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"); 
} 
+0

我一直在編寫javascript約3天,感謝您的提示,將試圖。 – EricSP

+0

不起作用: -/ – EricSP

+0

一般來說,該聲明沒有那麼有用。你能否以更準確的方式重新定義? – jAndy

0

我有同樣的問題(即使這種情況下的解決方案不應該使用一個帶有字符串的開關,但是像jAndy所建議的那樣是一張桌子)。

所以,如果沒有一個語法錯誤或錯過放在休息,你應該(可能?)做這樣的事情:

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

在我來說,我一直認爲我是路過字符串,所以這不僅僅是將參數轉換爲字符串,而是創建一個新的字符串對象。這種方式對我有用。我想給出一個更好的解釋,爲什麼它的作品,但我沒有它:)希望這可以幫助任何人。

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

+0

更新:不完全確定,但在我的情況下,這可能與JavaScript的(UTF16)編碼有關,步驟是我初始化myKey變量可能會修復一些奇怪的字符,我從服務器發送,我不能「看到「在文本編輯器上。有一天我將代碼與以前的版本進行比較,我發現其中一種情況。 –