Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载Javascript并在加载后进行检查_Javascript_Pageload - Fatal编程技术网

加载Javascript并在加载后进行检查

加载Javascript并在加载后进行检查,javascript,pageload,Javascript,Pageload,我正在使用以下JavaScript代码,并尝试在下载文件并将其添加到我的页面标题后查找: function loadjscssfile(filename, filetype) { if (filetype=="js"){ //if filename is a external JavaScript file var fileref=document.createElement('script') fileref.setAttribute("type

我正在使用以下JavaScript代码,并尝试在下载文件并将其添加到我的页面标题后查找:

function loadjscssfile(filename, filetype)
{
    if (filetype=="js"){ //if filename is a external JavaScript file
        var fileref=document.createElement('script')
            fileref.setAttribute("type","text/javascript")
            fileref.setAttribute("src", filename)
    }
    else if (filetype=="css"){ //if filename is an external CSS file
        var fileref=document.createElement("link")
            fileref.setAttribute("rel", "stylesheet")
            fileref.setAttribute("type", "text/css")
            fileref.setAttribute("href", filename)
    }
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
}



loadjscssfile("jquery.js","js");
加载图像后,我通常使用以下代码查找:

    document.getElementById("header_logo").src = "logo.png"; // load top logo

var header_logo = document.getElementById("header_logo"); 
    header_logo.onload = function(e) {  
    // do something here, the file has loaded
}
但是一旦我的JS被加载,我就不知道如何检查了

有什么想法吗


(我不能使用jQuery。)

您可以向
onload
事件添加一个函数:

function loadjscssfile(filename, filetype, onload) {
    //if filename is a external JavaScript file
    if (filetype == "js") { 
        var fileref = document.createElement('script');
        fileref.type = "text/javascript");
        fileref.onload = onload;
        fileref.src = filename);
        document.getElementsByTagName("head")[0].appendChild(fileref);
        return;
    }

    //if filename is an external CSS file
    if (filetype == "css") { 
        var fileref = document.createElement("link");
        fileref.rel = "stylesheet";
        fileref.type = "text/css";
        fileref.onload = onload;
        fileref.href = filename;
        document.getElementsByTagName("head")[0].appendChild(fileref);
        return;
    }
}



loadjscssfile("jquery.js","js", function() { alert(this.src); });

您可以向
onload
事件添加一个函数:

function loadjscssfile(filename, filetype, onload) {
    //if filename is a external JavaScript file
    if (filetype == "js") { 
        var fileref = document.createElement('script');
        fileref.type = "text/javascript");
        fileref.onload = onload;
        fileref.src = filename);
        document.getElementsByTagName("head")[0].appendChild(fileref);
        return;
    }

    //if filename is an external CSS file
    if (filetype == "css") { 
        var fileref = document.createElement("link");
        fileref.rel = "stylesheet";
        fileref.type = "text/css";
        fileref.onload = onload;
        fileref.href = filename;
        document.getElementsByTagName("head")[0].appendChild(fileref);
        return;
    }
}



loadjscssfile("jquery.js","js", function() { alert(this.src); });

在HTML4.01中,load事件仅由and元素支持,尽管许多img元素和一些脚本元素都支持它

在HTML5中,具有beforescriptexecute、afterscriptexecute和load事件,这些事件在脚本元素生命周期中的相关时间被调度。但是,在许多正在使用的浏览器中,这些事件的支持可能不可用,因此不值得依赖

查看脚本是否已加载的最可靠的方法是测试由要执行的最后一位代码赋值的变量的值,例如final语句,如:

var loadedScripts = loadedScripts || {};
loadedScripts.fooScript = true;
然后您可以测试它:

if (loadedScripts.fooScript) {
  // loaded

} else {
  // not loaded
}

在HTML4.01中,load事件仅由and元素支持,尽管许多img元素和一些脚本元素都支持它

在HTML5中,具有beforescriptexecute、afterscriptexecute和load事件,这些事件在脚本元素生命周期中的相关时间被调度。但是,在许多正在使用的浏览器中,这些事件的支持可能不可用,因此不值得依赖

查看脚本是否已加载的最可靠的方法是测试由要执行的最后一位代码赋值的变量的值,例如final语句,如:

var loadedScripts = loadedScripts || {};
loadedScripts.fooScript = true;
然后您可以测试它:

if (loadedScripts.fooScript) {
  // loaded

} else {
  // not loaded
}

我不必为setAttribute费心,只需像onload属性那样直接设置属性。但许多浏览器不会为脚本元素(例如IE 8)释放加载事件。@RobG同意,只是不想太过搅乱OP的代码。我不想麻烦setAttribute,只需直接设置属性,就像使用onload属性一样。但许多浏览器不会为脚本元素(如IE 8)释放加载事件。@RobG agree,只是不想过多地干扰OP的代码