Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Eval to evaluate javascript function

Status
Not open for further replies.

gorgered

Programmer
Dec 13, 2005
24
US
Hi,

We are currently using some UI framework called UIX. This UI will write all the JavaScript functions on the generated HTML page. What this UI is doing is it is writting some javascript functions on to the page which I want to use in my javascript.

On the HTML page it writes function as a string this is just an example
Code:
  function _form0Test(form0){
   // it will return true or flase;
  }

I am able to get the function as string now my question is how do I evaluate that function, below is my code this does't work.

Code:
  function myTest(form0)
  var func = window["_from0Test"];
  if(eval(func)){
    // do some thing
  }else{
    // do some thing else
  }

The only problem is I cannot change the UI framework and I have to use this function only.

Thanks.
 
Let's assume you copied the code that was generated and pasted it in here. If not, we're chasing problems that don't exist and missing those that do.

Anyway, there's an error in:
Code:
var func = window["_from0Test"];

because your function is named _f[red]or[/red]0Test.

Lee
 
Hello Gorge,

I am facing the same problem , did you find any solution to evaluate the function in eval..Can you please share your findings.
 
Lets say you get a Ajax response and in it there are autogenerated javascript functions which you need to be made availabel in a global context, for which they have to evaled, how would you do that??
 
The solution I found to eval'ing a function definition and getting it to work right is in the WAY it is defined. Instead of this:
Code:
function myFunc() { ... }
define it like this:
Code:
myFunc = function() { ... }
Then just call it in the usual way.

For some reason the normal function defininition doesn't work with eval, but creating an anonymous function and assigning it to a variable does.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top