0

I am fetching a JS script from a server, and getting the following string back:

(function(e){function t(t){for(var r,a,o=t[0],c=t[1] .....

I want to save it as a function variable so I can call it (and pass an argument).

I tried wrapping the string with a return (as suggested here) and then use Function() constructor to call it in order get the original function, but it returns undefined.

How can I do this?

1
  • It looks like the code is wrapped in an IIFE, which probably means you can only save the return value to a variable, but not the function itself. Commented Dec 17, 2021 at 16:23

1 Answer 1

0

Did you try eval https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

eval is a function that evaluates even if it is a function in string

something like this

const yourVariable = eval("function fromServerResponse(){console.log('executed');}")
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I'm getting Uncaught (in promise) SyntaxError: Illegal return statement exception. Also, I prefer to not use eval if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.