2012-11-02 142 views
1

好吧,這是情況。從外部文件調用外部JS函數

我有一個嵌入到頁面的語言切換鏈接,它將URL的字符串從-eng.shtml更改爲-fra.shtml以及Alias值。

現在基本上在頭我打電話給兩個腳本:

<script type="text/javascript" src="/js/langDB.js"></script> 
<script type="text/javascript" src="/js/langToggle.js"></script> 

LangToggle.js具有內langDB.js的功能,但是當編程到langDB.js函數被調用它完全不是那麼回事如預期的那樣,它的功能應該是將變量值從一個變爲另一個。

切換代碼:

function js_changeit(){ 

    //Get the current page full URL 
     var mainName = String(window.location); 

    //Base name 
     var slash = mainName.lastIndexOf("/"); 
     var dot = mainName.lastIndexOf("."); 
     var quest = mainName.lastIndexOf("?"); 
     var name = mainName.substring(slash+1,dot); 
     var ext = mainName.substring(dot,mainName.length); 

    //Remove the _f is it exists 
     var lang = name.substring(name.length-3,name.length); 

    //Detect the site sections, get the current site title and the site primary alias strings 
     var SiteSection = mainName.split("/"); 
     var currentAlias = SiteSection[3]; 
     var currentSite = SiteSection[2]; 

    //Split the url from Site to the end Alias section 
     var siteSectionAlias = "http://" + currentSite + "/" + currentAlias + "/"; 
     var SectionaAlias = mainName.split(siteSectionAlias) 
     var htmlFullDocFilename = SectionaAlias[1]; 

    //Extract the filename without the extension 
     var shtmlLastPos = htmlFullDocFilename.lastIndexOf(".shtml"); 
     var docTitle = htmlFullDocFilename.substring(0,shtmlLastPos-4);  

    //Alias Toggles, when an alias is detected in the conditional list, switch to the other. 


    langToggle(); 

    // Main Page 
     if (lang != "eng") { 
      window.open("http://" + currentSite + "/" + currentAlias + "/" + docTitle + "-eng" + ext, "_self"); 
     } else { 
      window.open("http://" + currentSite + "/" + currentAlias + "/" + docTitle + "-fra" + ext, "_self"); 
     } 
    } 

功能langDB.js:

function langToggle() { 
    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 
     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; 
    } 
} 

當過,我要點擊的語言肘杆IE會給我一個錯誤, 「currentAlias」 是不確定的,基本上這個變量的值似乎沒有加載到從外部腳本調用的函數中...

我不太確定我在做什麼錯...

+0

它在哪一行上給出錯誤? –

+0

我可能會建議一些閱讀:http://www.codeproject.com/Articles/182416/A-Collection-of-JavaScript-Gotchas - 請參閱範圍部分:)您在該功能中有很多本地範圍的變量似乎對你沒什麼作用,可能更適合作爲「對象屬性」 –

回答

4

當過,我要點擊的語言肘杆IE會給我一個錯誤,「currentAlias」是不確定的......

這是因爲currentAliasjs_changeit函數內的局部變量langToggle無法訪問js_changeit中的局部變量。

如果你的代碼確實需要訪問它,這些真的是單獨的文件,你必須有js_changeit把它放在全局命名空間(上window屬性):

window.currentAlias = currentAlias; 

...然後從那裏使用它。並且您需要確保js_changeitlangToggle之前運行,因此將代碼放在window上運行。

(我說的「全局變量」和「財產上window」互換,因爲所有的全局變量都在單一的性能[無名] JavaScript的全局對象,並在該對象是從全局變量window [window訪問瀏覽器是一個指向對象的屬性,它是屬性]。)

但是,如果langToggle需要訪問它,一些重構可能是適當的,尤其是您可以避免添加更多的全局符號。

不好意思,剛纔你的代碼看了一遍,看到js_changeit電話langToggle。所以更好的解決方案是js_changeitcurrentAlias作爲參數傳遞給langToggle。不需要全局變量。

因此改變這一行中js_changeit

langToggle(); 

到:

currentAlias = langToggle(currentAlias); 

,並更改langToggle所以它接受currentAlias作爲參數和返回更新後的值。

你要去哪裏誤入歧途的一點是,一個函數不從那裏的稱爲範圍繼承變量,它繼承了他們從那裏的定義範圍。所以currentAlias對於langToggle不存在,因爲它沒有在聲明langToggle的範圍內聲明。

讓我們來簡單的例子:

function foo() { 
    var answer = 42; 

    bar(); 
} 

function bar() { 
    console.log(answer); // <== Error, `answer` is not defined 
} 

bar稱爲foo,但不繼承foo的變量。如果foo想要的東西傳達給bar,它通常會在把它作爲一個參數:

function foo() { 
    var answer = 42; 

    bar(answer); 
} 

function bar(a) { 
    console.log(a); // This is fine 
} 

那麼,如果我們調用foobar將記錄「42」。

同樣,如果bar需要回溝通東西foo,它通常會做到這一點的返回值:

function foo() { 
    var answer = 42; 
    var b; 

    b = bar(answer); 
    console.log(b); 
} 

function bar(a) { 
    console.log(a); 
    return a * 2; 
} 

現在,如果我們調用foobar將記錄「42」和foo將記錄「84 」。

還有其他途徑foobar共享信息(對象的屬性,閉包),但如果你是新來這個東西,很多工作與現在越來越上。 :-)

+0

對不起,我對編碼很陌生。 我應該在哪裏輸入window.currentAlias = currentAlias; ?? – EricSP

+0

@ user1795138:其實,我只是注意到了一些東西並更新了答案。不需要全局變量,請參閱更新。 –

+0

好吧所以改變了你提到的代碼: currentAlias = langToggle(currentAlias); 和langToggle。js我改變了函數langToggle(){ 功能langToggle(currentAlias){ 現在如果我點擊切換它會改變網址tö「undefine」中的別名,如http:// internet/undefined/index-fra .shtml而不是http://internet/a-notre-sujet/index-fra.shtml – EricSP

0

那麼,你應該完全按照原來的錯誤信息。如果你看看你的langToggle函數,你立即嘗試引用currentAlias,但是它還沒有在函數的範圍中定義。

我看到你已經在函數js_changeit的作用域中定義了它,但聲明只能在該函數的作用域中使用,而不能在langToggle函數作用域中使用。