I have a textarea box that sends the information to Javascript via onclick(). I want to execute the code the user puts into the textarea. How can I do that?

var code = document.getElementById('code').value

That's part of the function that gets called via onclick. See that variable is called code. I want code the be executed as real Javascript code. How do I do that?

Not sure, but try this using eval function.

var code = document.getElementById('code').value;
eval(code);

Hey sorry man, but that wasn't what I was looking for. Here is my entire code...

<script>
function speedgo()
{

var code = document.getElementById('code').value;

var d1 = new Date();

//Code to be timed will go below.

eval(code);

//Code to be timed will go above

var d2 = new Date();

m1 = d1.getMilliseconds();
m2 = d2.getMilliseconds();
s1 = d1.getSeconds();
s2 = d2.getSeconds();

if(s1 > s2)
{
var minitotal=s1-s2;
var total=60-minitotal;
var s2 = s2+total;
}


militotal1=s1*1000+m1;
militotal2=s2*1000+m2;

var total=militotal2-militotal1;

var final22= total/1000;

alert("Total Seconds: "+final22);

}
</script>
<b>HTML</b>, <b>Javascript</b>, and <b>CSS</b>. Surround HTML with . Surround Javascript with &lt;script&gt;. Surround CSS with &lt;style&gt; tags. Happy coding for Speed.

<input onclick="speedgo()" type="button" value="Code Speed Go!" />

<textarea cols="50" id="code" rows="20" style="height: 409px; width: 675px;"></textarea>

You see the eval function doesn't literally execute the code. Any thing you have in mind that will execute work in this situation? PS I'm trying to track how long a Javascript script lasts. So the user inserts the Javascript code into the textarea and after executing the code, the program alerts how long it took to complete that code... Please help!

eval executes java script code.
Your code works for me.
For that you have to directly enter js code in textarea as shown in below code.
no 'script' tag.
You only want JS code to execute?

<script>
function speedgo()
{

var code = document.getElementById('code').value;

var d1 = new Date();

//Code to be timed will go below.

eval(code);

//Code to be timed will go above

var d2 = new Date();

m1 = d1.getMilliseconds();
m2 = d2.getMilliseconds();
s1 = d1.getSeconds();
s2 = d2.getSeconds();

if(s1 > s2)
{
var minitotal=s1-s2;
var total=60-minitotal;
var s2 = s2+total;
}


militotal1=s1*1000+m1;
militotal2=s2*1000+m2;

var total=militotal2-militotal1;

var final22= total/1000;

alert("Total Seconds: "+final22);

}
</script>
<b>HTML</b>, <b>Javascript</b>, and <b>CSS</b>. Surround HTML with . Surround Javascript with &lt;script&gt;. Surround CSS with &lt;style&gt; tags. Happy coding for Speed.

<input onclick="speedgo()" type="button" value="Code Speed Go!" />

<textarea cols="50" id="code" rows="20" style="height: 409px; width: 675px;">
var a = 10;
var b = 20;
alert('a:'+a+' ,b:'+b);
alert('summation is a+b :'+ (parseInt(a) + parseInt(b)));
</textarea>

As vibhadevit says, eval() should do the job.

It should be executed inside a try{...} catch(error){...} block because the user input is not guaranteed to be good JavaScript.

Airshow

Wait so did the script give you a time on how fast it executed and did your code execute completely?

eval executes java script code.
Your code works for me.
For that you have to directly enter js code in textarea as shown in below code.
no 'script' tag.
You only want JS code to execute?

<script>
function speedgo()
{

var code = document.getElementById('code').value;

var d1 = new Date();

//Code to be timed will go below.

eval(code);

//Code to be timed will go above

var d2 = new Date();

m1 = d1.getMilliseconds();
m2 = d2.getMilliseconds();
s1 = d1.getSeconds();
s2 = d2.getSeconds();

if(s1 > s2)
{
var minitotal=s1-s2;
var total=60-minitotal;
var s2 = s2+total;
}


militotal1=s1*1000+m1;
militotal2=s2*1000+m2;

var total=militotal2-militotal1;

var final22= total/1000;

alert("Total Seconds: "+final22);

}
</script>
<b>HTML</b>, <b>Javascript</b>, and <b>CSS</b>. Surround HTML with . Surround Javascript with &lt;script&gt;. Surround CSS with &lt;style&gt; tags. Happy coding for Speed.

<input onclick="speedgo()" type="button" value="Code Speed Go!" />

<textarea cols="50" id="code" rows="20" style="height: 409px; width: 675px;">
var a = 10;
var b = 20;
alert('a:'+a+' ,b:'+b);
alert('summation is a+b :'+ (parseInt(a) + parseInt(b)));
</textarea>

Yes i get 1.something seconds to execute my code..

Wait so did the script give you a time on how fast it executed and did your code execute completely?

I get that, but how come a basic while loop doesn't work. I tried declaring a variable then doing a long while loop. This is what doesn't work below...

If you place this in the textarea it will not execute correctly.

var i = 0;
while(i != 100000)
{
i++;
}

Yes i get 1.something seconds to execute my code..

While loop is also working in my browser.
Check this link for my output.
click here

That link doesn't work. On the page there is no picture. Also the while loop that I gave you should take around 10 seconds.

attached image....

How you know it will take 10 sec?
it is depending on what code is there in while loop.
If its simple, it may execute faster.

Yes, but now try that while loop on a whole separate page. Still in Javascript notice that the while loop will take a while to execute.

On the page there is no picture. Also the while loop that I gave you should take around 10 seconds.

I know this isn't central to the topic but you can't rely on loops to provide delay in JavaScript.

First, it would be inefficient, and second the length of the delay would depend on the speed of the client computer.

For a reliable delay, use the JavaScript function setTimeout().

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.