Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Web Development > HTML/DHTML - JavaScript/VBScript

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Workarounds for JavaScript "eval"

By pradeep pradeep is offline

On 17th September, 2008
Cool Workarounds for JavaScript "eval"

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Workarounds for JavaScript "eval" link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

  • Oops, No Similar Articles found for Workarounds for JavaScript "eval" but you can check out the Recent Articles.
  • Also see more articles by pradeep.
Introduction
One Javascript function that gets used such frequently than it should to be is "eval". "eval" evaluates the content of a text string passed to it and then executes it as if it were normal Javascript code..

Actually, we should never use eval for any purpose in our code, be it dynamic variable access, or dynamic code execute or the likes there is always a workaround. "eval" is usually used by beginners and amateurs most of the times to access some dynamic variable i.e. in a situation where a variable is a part of another varibale name. Example: You have variable str1,str2...strn, so to access them you may write:

Code: JavaScript
var myStr = eval('str'+i); // where i is our counter variable
 

Solution to Dynamic Variable Access

For those who don't know all global variables are held in the window object, so we can bypass "eval" to access the require variable like this:

Code: JavaScript
var myStr = window['str'+i]; // where i is our counter variable
 

This much better, faster and safer way to access a dynamic varibale.

Dynamic Code Execution

Sometimes some experienced programmers may still use eval, of course for something quite complicated than accessing a dynamic variable. There also we can avoid using eval, by creating a function with the code to execute and then calling the function. Normally we would do this.

Code: JavaScript
var myCode = 'alert("Howdy?");';
 eval(myCode);

Now let's try the non-eval approach,

Code: JavaScript
var myCode = 'alert("Howdy?");';
 var myFucn = new Function(myCode);
 myFucn();

The alternative method may produce code a bit longer than the eval one, but the later one is better and easier to understand, and it should also run faster.
Old 10-02-2008, 10:41 PM   #2
shabbir
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 10,943
Thanks: 35
Thanked 167 Times in 139 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Workarounds for JavaScript "eval"


shabbir is offline   Reply With Quote
Old 10-17-2008, 10:15 AM   #3
shabbir
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 10,943
Thanks: 35
Thanked 167 Times in 139 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Workarounds for JavaScript "eval"


shabbir is offline   Reply With Quote
Old 12-03-2008, 11:45 AM   #4
amitvjog
Newbie Member
 
Join Date: Dec 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
amitvjog is on a distinguished road
Smile

Re: Workarounds for JavaScript "eval"


Hi,
Can you please explain the reasons behind not using 'eval', or just that beginners are using it we should not use it?

Regards,
Amit Jog
amitvjog is offline   Reply With Quote
Old 12-04-2008, 12:43 AM   #5
vol7ron
Newbie Member
 
Join Date: Dec 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
vol7ron is on a distinguished road

Re: Workarounds for JavaScript "eval"


I know this may not be a debugging forum, but I'm trying to get something similar to work. Notice the "
function (replace){}" at the bottom. Do you know of any way to add an event to run code stored in a variable?

So given the below, I want getObjById('outputID').innerHTML='hello';
to run when the first link is clicked. Thank you for any help/explanations that can be shed on this subject.

attachEventHandlers(obj, eventType, fn, useCapture){
if (obj.addEventListener) {
obj.addEventListener(eventType, fn, useCapture);
return true;
} else if (obj.attachEvent) {
var val = obj.attachEvent("on"+eventType, fn);
return val;
} else {
try {
obj.eval("on" + eventType) = fn;
} catch (err) {
return;
}
}
}
function init(){
replace = "getObjById('outputID').innerHTML='hello';";
attachEventHandlers(document.links[0]
, 'click'
, function (replace){}
, false
);
}
vol7ron is offline   Reply With Quote
Old 12-04-2008, 12:51 AM   #6
vol7ron
Newbie Member
 
Join Date: Dec 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
vol7ron is on a distinguished road

Re: Workarounds for JavaScript "eval"


I found that the following will work:
Code:
function init(){
   replace = "getObjById('outputID').innerHTML='hello';";
   replace = new Function(replace);
   attachEventHandlers(document.links[0]
      , 'click'
      , replace
   , false
   );
}
I'm just not sure I understand why I couldn't use new Function(replace) or function(replace) instead...hmm

Thanks for the article, regardless.
vol7ron is offline   Reply With Quote
Old 12-04-2008, 01:04 AM   #7
vol7ron
Newbie Member
 
Join Date: Dec 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
vol7ron is on a distinguished road

Re: Workarounds for JavaScript "eval"


Nevermind, using new Function(replace), instead of function(replace){} worked! thank you for the article! I've done this before, just have forgot after all this time and didn't feel like going through old code.

Amit,
My example should be a good reason of when not to use eval. I think eval executes at runtime, or, at the least it is quirky. When trying to late-bind an event, eval() did not do what I wanted it to do. It ran the code at page load, rather than when the event was triggered. Instead, using the new Function() did exactly what needed to be done when late-binding an event.

This is just my example, I'm sure there are other pros/cons though.

Hope that helps,
vol7ron
vol7ron is offline   Reply With Quote
Old 06-30-2009, 04:04 PM   #8
gkumar
Banned
 
Join Date: Jun 2009
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
gkumar will become famous soon enough

Re: Workarounds for JavaScript "eval"


You will have to write an eval yourself. You will have to parse the string and invoke the right operators.
Here's a link to get you started.
The Tamarin project has a ECMAScript parser written in ES4. Try this as well.
"You can even write the entire javascript inside as3, so that you do not need to touch the actual html page." Do you have links / tutorials? – okoman
Both AS and JS are based on the same ECMAScript standard. So, if you pass a string of AS3 to a container, and use JS's eval on this string, it should work just fine.
gkumar is offline   Reply With Quote
Old 07-06-2009, 08:21 PM   #9
David Michael
Go4Expert Member
 
Join Date: Jul 2009
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
David Michael is on a distinguished road

Re: Workarounds for JavaScript "eval"


Quote:
Can you please explain the reasons behind not using 'eval', or just that beginners are using it we should not use it?
I think they use it...try to read the whole code.
David Michael is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

 

All times are GMT +5.5. The time now is 05:09 AM.