![]() |
Re: Workarounds for JavaScript "eval"
|
Re: Workarounds for JavaScript "eval"
|
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 |
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 ); } |
Re: Workarounds for JavaScript "eval"
I found that the following will work:
Code:
function init(){Thanks for the article, regardless. |
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 |
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. |
Re: Workarounds for JavaScript "eval"
Quote:
|
Re: Workarounds for JavaScript "eval"
Hi,
i would like to add to this. In JavaScript replace function is basically used to replace some string in an another string. In replace function you can see that we are passing the regular expression to replace, but if the data is dynamic and i want to replace it in runtime .The over all concept is that in replace function of JavaScript we can not pass a variable to replace, we have to pass a regular expression - if we want to use a variable to replace we have to convert it to regularexpression. thanks Eliza |
| All times are GMT +5.5. The time now is 13:46. |