好吧,這是情況。從外部文件調用外部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」 是不確定的,基本上這個變量的值似乎沒有加載到從外部腳本調用的函數中...
我不太確定我在做什麼錯...
它在哪一行上給出錯誤? –
我可能會建議一些閱讀:http://www.codeproject.com/Articles/182416/A-Collection-of-JavaScript-Gotchas - 請參閱範圍部分:)您在該功能中有很多本地範圍的變量似乎對你沒什麼作用,可能更適合作爲「對象屬性」 –