Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2006, 12:14 AM
Authorized User
 
Join Date: May 2006
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default convert string to function

hi i have a var which takes a value based on a split, this value is nothing the but the name of a function to be called.

consider the code
Code:
var searchFn = "fnCallCal";

searchFn(name)
now the thing is the above will not work because it si a string so i need to know how to convert this from string to an function call.

thanks

 
Old June 23rd, 2006, 02:12 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you code is in a browser then:
Code:
window[searchFn](name);
should do.
If not then use the Function object:
Code:
var fn = new Function("term", "return " + searchFn + "(term);");
fn(name);
--

Joe (Microsoft MVP - XML)
 
Old May 26th, 2007, 07:47 AM
Registered User
 
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

there is a simple way to do this:

Code:
var searchFn = "fnCallCal";

// searchFn(name)
// the eval() method returns a function objet, so you can use the result as a function

eval(searchFn)(name);
Cheers,
Pascal.

 
Old September 19th, 2008, 03:25 AM
KM KM is offline
Registered User
 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
My requiremnet is same in QTP .
I need to call different functions based on the an input..means.. if input is x call functx(j) is input is y call functy(j) so i tried these methods..it would nt work

then i tried
"var searchFn = "fnCallCal";

// searchFn(name)
// the eval() method returns a function objet, so you can use the result as a function

eval(searchFn)(name);"

and it worked ... but when if you need to pass parameters it is not taking it the way wriiten in code..


here is wat i coded

searchfn= "test"&i&"(j)

eval(searchfn)

so it called test1(j)
test2(j)

..

thanks a lot fellas for this code..as i was in a real fix when i found this and it helped a lot... :)
hope this helps others too....:)

-KM






Similar Threads
Thread Thread Starter Forum Replies Last Post
Know how to convert a range to a string? rstober Excel VBA 5 September 12th, 2009 08:49 AM
convert to string prafullaborade XML 7 May 29th, 2008 02:59 PM
Datpart and convert function gregalb SQL Server 2000 4 February 16th, 2007 01:39 AM
convert string to date deeptibg SQL Server DTS 1 December 22nd, 2005 08:47 PM
Query String Cast or Convert function hoffmann Access 6 February 25th, 2004 01:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.