This is a migrated thread and some comments may be shown as answers.

Passing a variable from C# to Javascript

1 Answer 109 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 25 Feb 2013, 12:28 AM
Hi,

I am attempting to pass a variable from a function in C# to my Javascript via InvokeScript, with the following code
String name = ".username";
Actions.InvokeScript("buildPath(name)");
however when I investigate the name variable on the javascript side, it is listed as an empty string. I've spent a good amount of time looking but have yet to see any hint that I am doing something wrong.

Any assistance would be highly appreciated.

1 Answer, 1 is accepted

Sort by
0
Boyan Boev
Telerik team
answered on 26 Feb 2013, 09:11 AM
Hello Peter,

To invoke a Javascript function with a variable as parameter should first escape the string (name in your case) and then call it as a variable. Your code should look like:

String name = "'.username'";
Actions.InvokeScript("buildPath("+name+")");

Here is a short video that demonstrates it against a sample html application. 

Hope this helps.
All the best,
Boyan Boev
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
Peter
Top achievements
Rank 1
Answers by
Boyan Boev
Telerik team
Share this question
or