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
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.