Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run a JavaScript in JavaScript? [duplicate]

Tags:

javascript

Possible Duplicate:
Is It Possible to Sandbox JavaScript Running In the Browser?

I would like to give the user the ability to input JavaScript in a textarea like this:

<!DOCTYPE html>
  <html>
    <head>
      <script>
        function lapzwans()
        {
          var d = document.getElementById("area").value;
          document.getElementById("blahblah").innerHTML = d;
        }
      </script>
    </head>
  <body>
    <h1>Javascript in Javascript</h1>
    <p>This experiment attempts to launch a script from another script.</p>
    <p>Enter your script in the text area</p>
    <textarea type="text" id="area"></textarea>
    <button onclick="lapzwans()">Click here</button>
    <p id="blahblah"></p>
    <p id="deegroller">Make this text green</p>
  </body>
</html>

I have tried this, but I get no result.
Is it impossible to do this, or am I doing things wrong?
Thanks in advance.

like image 600
David Leegwater Avatar asked Sep 21 '26 13:09

David Leegwater


1 Answers

One possible answer:

eval(/* some JS here */);

Note though that what you are trying to do can be dangerous (in some cases) as you are allowing user-written arbitrary JS to be executed.

Related reading:

  • How evil is eval?
  • When is JavaScript's eval() not evil?
like image 137
Whymarrh Avatar answered Sep 24 '26 01:09

Whymarrh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!