How can get a list of the functions in a javascript file?

后端 未结 4 1212
终归单人心
终归单人心 2021-01-20 14:18

I\'m trying to cobble together a php style include function for javascript. The route I\'m taking is through a XMLHttpRequest. The included file will load, but the functions

4条回答
  •  余生分开走
    2021-01-20 14:43

    It's simple enough to use the DOM to add the required script:

    function include(jsFile)
    {
        var sEl  = document.createElement("script");
        sEl.type = "text/javascript";
        sEl.src  = jsFile;
        document.getElementsByTagName("head").appendChild(sEl);
    }
    

    Browsers will even download this file asynchronously, and script elements have an onload event that works in most (all?) popular browsers so you can also add a callback function if you like. The obvious downside is that functions wouldn't become available to the current script until the file is downloaded and parsed.

提交回复
热议问题