我使這個方法動態加載JavaScript函數。它包含.js
文件,但是當我調用一個方法時,該方法說明它是未定義的。以下兩個.js
文件是ajax_object.js
和user_manager.js
。Ajax Javascript方法不能調用
函數外部的警報被讀取和工作,但內部的警報沒有。
enter code here
//ajax_object.js
//Load a javascript library
function loadJavascript(src, url){
XMLHttpRequestObject=getXmlHttpRequestObject();
if(XMLHttpRequestObject){
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4){
if (XMLHttpRequestObject.status == 200 || XMLHttpRequestObject.status == 304) {
includeJavaScript(src, url, XMLHttpRequestObject.responseText);
}
}
}
}
XMLHttpRequestObject.open('GET', url, true);
XMLHttpRequestObject.send(null);
}//end LoadJavaScript
//Add Library to header
function includeJavaScript(src, fileUrl, xmlObject) {
if ((xmlObject != null) && (!document.getElementById(src))){
var documentHead = document.getElementsByTagName('HEAD').item(0);
var includeScript = document.createElement("script");
includeScript.language = "javascript";
includeScript.type = "text/javascript";
//includeScript.id = src;
includeScript.src=fileUrl.concat(src);
includeScript.defer = true;
includeScript.text = xmlObject;
documentHead.appendChild(includeScript);
}
}
//user_manager.js
//First alert is read
alert("Outside User Manager");
function selectUserManagerModuleType(){
XMLHttpRequestObject=getXmlHttpRequestObject();
//This doesn't work
//throws selectUserManagerModuleType undefined
alert("Inside The User Manager");
}
您能否確認,例如使用Firebug中的'net'選項卡,腳本真的被加載了嗎?使用net – 2010-04-04 16:40:55
,我得到這樣的: GET user_manager.js 304修改 螢火蟲也說明這一點: 」; – user293313 2010-04-04 17:16:31
我想知道如果將函數賦值給一個變量並以這種方式調用它會發生什麼...... – 2010-04-04 17:30:28