Question

I'm trying to add a js function dynamically so I read about $.getScript(). Correct me if I'm wrong, but it seems $.getScript() will just call a .js file that's already there in the public folder and will just sort of "load it" and make it available, but the js file itself being called is just a static js. What I'm trying to do is call a dynamic js script and the content of this js script changes considerably, so it has to come from the server.

  • I think this isn't possible with $.getScript() alone, or is it?
  • If not, I could make an ajax that returns the function syntax, but how do I then add it somehow to the current js or how do I create a js file for it and load that js?
  • I would possibly like to delete the new js later, but that's not a priority.

So can $.getScript() help with this or a combo of $.getScript() and .ajax?

Was it helpful?

Solution

Pekka's comment is correct - jquery doesn't care what the extension of the file is.

$.getScript("dynamicScript.php") will work just fine so long as that file outputs valid script (no script tags needed)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top