Is this an alternative to eval()?

S

steveneill

Is this an alternative to eval(). Admittidly, I have done *very* little
testing (ran it a few times in Firefox), but it seemed to work
suprisingly well with simple expressions such as "alert('hello')"

function _eval(s) {
var h = document.getElementsByTagName("HEAD")[0];
var o = document.createElement("SCRIPT");

o.text = s;
h.appendChild(o);
h.removeChild(o);
}

Any thoughts?

Steve
 
R

Randy Webb

(e-mail address removed) said the following on 8/12/2005 5:00 PM:
Is this an alternative to eval(). Admittidly, I have done *very* little
testing (ran it a few times in Firefox), but it seemed to work
suprisingly well with simple expressions such as "alert('hello')"

function _eval(s) {
var h = document.getElementsByTagName("HEAD")[0];
var o = document.createElement("SCRIPT");

If the browser supports createElement......
o.text = s;
h.appendChild(o);
h.removeChild(o);
}

Any thoughts?

Why add it and then remove it?

It also depends on whether the browser executes the script. It should
but that is no guarantee.

But, why do you think you need an alternative to eval? There usually is
a better way to do things than eval, but it depends on what you are
trying to do.
 
S

steveneill

You need to remove the child node otherwise the HEAD element will
accumulate SCRIPT elementseach time the _eval() is called. The script
executes once its appended, so removing seems to have no effect on its
execution.

I'm curious... what browsers don't support createElement? I use FF and
IE.
 
R

Randy Webb

(e-mail address removed) said the following on 8/12/2005 5:32 PM:
You need to remove the child node otherwise the HEAD element will
accumulate SCRIPT elementseach time the _eval() is called. The script
executes once its appended, so removing seems to have no effect on its
execution.

That depends on what you are executing. If the code that you are
executing creates functions that are used in other parts of the page......
I'm curious... what browsers don't support createElement? I use FF and
IE.

Probably the one's that don't quote in Usenet :)

It doesn't really matter if you feature test for it:

if (document.createElement){
//browser supports it so use it.
}
 
P

philippe.laplanche

sorry but why is this alternative interesting ?


Randy said:
(e-mail address removed) said the following on 8/12/2005 5:32 PM:


That depends on what you are executing. If the code that you are
executing creates functions that are used in other parts of the page......


Probably the one's that don't quote in Usenet :)

It doesn't really matter if you feature test for it:

if (document.createElement){
//browser supports it so use it.
}
 
L

Lasse Reichstein Nielsen

Is this an alternative to eval().

Possibly. It depends on the browser support for DOM and for dynamically
adding script elements.

Is there any browser where this works and eval doesn't? I.e., do you
*need* an alternative?

The warnings we generally give against using eval apply just as much
to this.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Fri, 12 Aug 2005 14:00:13, seen in (e-mail address removed) posted :
Is this an alternative to eval(). Admittidly, I have done *very* little
testing (ran it a few times in Firefox), but it seemed to work
suprisingly well with simple expressions such as "alert('hello')"

function _eval(s) {
var h = document.getElementsByTagName("HEAD")[0];
var o = document.createElement("SCRIPT");

o.text = s;
h.appendChild(o);
h.removeChild(o);
}

It is not so in general, because eval is a function that returns a
value, and yours only returns undefined.

Also, it uses methods available only in more recent browsers.

There is in fact nothing at all wrong with using eval for the intended
purposes; the error is using eval as an addressing mechanism where there
is a more direct method (and in using eval where nothing is actually
needed instead).

See FAQ 4.40 & 4.39, and
<URL:http://www.merlyn.demon.co.uk/js-other.htm#eval>.
 
S

sneill

John,

You are off course quite correct in your assertions. I should have
perhaps added that my need for such an approach was to execute
functions rather than expressions that returned values. Furthermore, in
as much as I try hard to always avoid the eval() function, some
situations are unavoidable (complex expressions derived at runtime).

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top