0

Is there any way to run a script as a string in JavaScript like this?

// core.jslib
mergeInto(LibraryManager.library, {
  myCallback: function (str, arg) {
    runScript(str, arg); // I want a function like this
  }
});
// execute script
<script>
  var script = "function(str){ window.alert(UTF8ToString(str)); }";
  myCallback(script, "Hello, World");
</script>

I was wondering if it is possible to run a script in this way.

Spiegel
  • 1
  • 2
  • 1
    Why do you want to do this? You can, but there is (almost) always a better way to do this. – gen_Eric Aug 16 '22 at 15:52
  • I'm using that function of UNITY. This is needed to send a message from C# to JavaScript. https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html – Spiegel Aug 16 '22 at 15:58
  • Yes, but _why_ do you need to send a sting containing function code? Why can't you have the function (maybe inside an object) and use that? – gen_Eric Aug 16 '22 at 17:41

0 Answers0