简体   繁体   中英

javascript - Executing Code in code?

I just wanted to ask, it this possible to execute javascript code from user input? For example: User enters his Javascript code in <textarea id="test"> . Then with JavaScript i get his input with var userInput = $('#test').val(); . And is it possible to execute javascript code from that string? If it is possible, please tell me how.

~Regards

There are two ways to do this with the eval() function:

eval(userInput);

or by creating a function and then invoking it.

var fun = new Function(userInput);
fun.apply(window);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM