execute javascript from textarea

前端 未结 3 1196
无人及你
无人及你 2021-01-12 07:40

I\'m not entirely sure if this is possible, but I\'m trying to create a mini faux editor in a browser that runs javascript on the page. Here\'s what I\'ve been trying to do

3条回答
  •  孤独总比滥情好
    2021-01-12 08:14

    Thnx @Prodigy && @JayC !!!

    such a simple solution, I did not know such a function existed :)

    using the eval() function did the trick!!!

    HTML

    
    
    
    

    javascript

    var cnsl = document.getElementById('cnsl');
    
    function run() {    
        return eval(cnsl.value); // << did the trick ;)
    }
    
    //canvas
    var ctx;
    
    function setup() {
        var canvas = document.getElementById('canvas');
            ctx = canvas.getContext('2d');
    }
    setup();​
    

提交回复
热议问题