I would like to be able to dynamically load a Javascript file within a WSF. There is no DOM I don't think, but if there were, I could try something like:

function addJavascriptFile(filename) {
    var file = document.createElement('script')
    file.setAttribute("type", "text/javascript")
    file.setAttribute("src", filename)
}

After running a function, I could unload with something like:

 function removeJavascriptFile(filename) {
 var all = document.getElementsByTagName("script")
 for (var i = all.length; i >= 0; i--) {
     if (all[i] && all[i].getAttribute("src") != null && all[i].getAttribute("src").indexOf(filename) != -1)
         all[i].parentNode.removeChild(all[i])
     }
 }

Does anyone know if there is something equivalent without using the DOM?

有帮助吗?

解决方案

I found a way to do it, but hopefully someone will come by with a better solution. I'll answer my own question in case someone comes across the same problem.

Given there is no DOM, I simply eval(javascriptFileContents) the file contents and then call the function literal directly. For example, if I have a file wombat.js with a function (logger) {... inside:

var logger = ...
var fn = eval(wombatJsContents);
fn(logger);

It's not pretty and probably a little dangerous, but it works. I'm hopeful that someone will find a better way. In the mean time...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top