473,322 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Is this an alternative to eval()?

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

Aug 12 '05 #1
7 2248
st********@gmail.com 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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 12 '05 #2
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.

Aug 12 '05 #3
st********@gmail.com 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.
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 13 '05 #4
sorry but why is this alternative interesting ?
Randy Webb wrote:
st********@gmail.com 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.
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly


Aug 13 '05 #5
st********@gmail.com writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 13 '05 #6
JRS: In article <11**********************@g14g2000cwa.googlegroups .com>
, dated Fri, 12 Aug 2005 14:00:13, seen in news:comp.lang.javascript,
st********@gmail.com 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>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 13 '05 #7
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

Aug 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
9
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select...
3
by: McKirahan | last post by:
I said I wouldn't use "eval()" anymore but I need help to do it. Below is some stripped-down code (4 lines; watch for word-wrap) extracted from USGA.COM that preloads images: main_nav_home_F1...
7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
24
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
3
by: BUX | last post by:
I have to get public variable value using variable name. Evals("myPublicVariable") do nor work How can I do? thank you
3
by: Vik Rubenfeld | last post by:
I'm working on integrating the a javascript wysiwyg editor (Xinha) with my blog software (ExpressionEngine, aka EE). EE has extensions now so it's easy to get the Xinha header code into the head...
7
by: Cramer | last post by:
In addition to Web.config, I have a few configuration values that I store in a sql server database. I would like to read them from the database only once per Application.Start (there is no need to...
4
by: spamme | last post by:
Hi, As you can see from the following code, I am trying to manipulate a large number of layers. I have read many times in the past how the "Eval" function should be used very sparingly as it can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.