0

Is it possible to send a JavaScript function back from an XMLHttpRequest to use at a later time? I'm seeing if there is a modular way of loading JavaScript. If anyone has a solution that does not include a library or using eval please tell me.

  • 1
    You can return the Javascript source code, and then create a ` – Barmar Jun 28 '16 at 16:58
  • 1
    Why don't you want to use `eval()`? – Barmar Jun 28 '16 at 16:59
  • Can't you just have all the functions predefined on the client's end, then send back the parameters? For example the server could return `{"method":"openWindow", "url":"..."}`. The client already has `openWindow` declared, so it finds the function and runs it with the given url. Other than that, if you're sending code over the network, you'll eventually use something similar to eval. – Dave Chen Jun 28 '16 at 17:00
  • If it's because you heard that it's dangerous, that's true for any method you use to execute code dynamically. If you need to execute the Javascript you get from AJAX, it `eval()` is no more dangerous than any other method. – Barmar Jun 28 '16 at 17:01
  • 1
    I think this questions has already been answered before: http://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript – BrianDiesel Jun 28 '16 at 17:03
  • @BrianDiesel. Yes and no it shows how to fetch javascript but it doesn't seem to work with function. Or at least I can't seem to get the javascript engine to recognize it as a function. It keeps returning string when I do console.log(typeof()); – Nathan T Thomas Jun 28 '16 at 17:21
  • @Barmar I'm not a beginner and its' not because of performance either. I can't get eval to work with var f = function sayGreeting(greet){alert(greet)} when it is in string form. Eval only seems to make the function active once and I can't seem to save it to a variable as a function. I've tried eval("var f = function sayGreeting(greet)"); and var f = eval("function sayGreeting(greet)"). I already know how to safely use the eval function. – Nathan T Thomas Jun 28 '16 at 17:24
  • Works for me: https://jsfiddle.net/d9946o6j/ – Barmar Jun 28 '16 at 17:28
  • Could you please add the code you are using to insert the script into the DOM? Are you setting the type as text/javascript? – BrianDiesel Jun 28 '16 at 17:40
  • @Barmar I remember the problem I had with eval. This executes the function. I don't need it executed I want to store it for later use as mentioned earlier. The new Function constructor return an anonymous function but I need to be able to add it and remove it later in an event listener. – Nathan T Thomas Jun 28 '16 at 22:42
  • `eval` just executes whatever you give it. If it's a function call, it executes the function. If it's a function definition it just defines the function, it doesn't execute it. If that's not what you're seeing, you're doing something wrong. You should update the question to show what you're trying and how it's not working. Look at my fiddle, it doesn't execute the function until I call it. – Barmar Jun 29 '16 at 05:08

0 Answers0