Passing a variable from C# to Javascript

2 posts, 0 answers
  1. Peter
    Peter avatar
    1 posts
    Member since:
    Feb 2013

    Posted 24 Feb 2013 Link to this post

    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.
  2. Boyan Boev
    Admin
    Boyan Boev avatar
    1072 posts

    Posted 26 Feb 2013 Link to this post

    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
Back to Top