Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4065060
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:56:07+00:00 2026-05-20T15:56:07+00:00

Is it possible to call javascript from c# something like this? ScriptRuntime py =

  • 0

Is it possible to call javascript from c# something like this?

ScriptRuntime py = Python.CreateRuntime();
dynamic random = py.UseFile("cal.js");
var result =random.Add(1,2);

cal.js


 function Add(a, b) {

        return (a + b);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:56:08+00:00Added an answer on May 20, 2026 at 3:56 pm

    Yes, as long as you host the js in a webrowser control. You can use the ObjectForScripting and document properties for this along with a com visible object. Indeed the opposite is true as well, that is you can call c# methods from JavaScript. The type dynamic allow you to pass, and work with, complex objects without having to use reflection Invoke/GetProperty, etc.

    Here is a really simple example.

    [ComVisibleAttribute(true)]
    public class Ofs
    {
       public void MethodA(string msg)
       {
          MessageBox.Show(msg); // hello from js!
       }
    }
    
    public class Form1
    {
    
      void Form1()
      {
        WebBrowser b = new WebBrowser();
        b.DocumentText = "<script>"
          + "var a = [];"
          + "a.foo = 'bar!'";
          + "var jsFunc=function(y){alert(y);}"
          + "var jsFunc2=function(){return a;}"
          + "window.external.MethodA('hello from js!');</script>";
    
        b.ObjectForScripting = new Ofs(); // we can call any public method in Ofs from js now...
        b.Document.InvokeScript("jsFunc", new string[] { "hello!" }); // we can call the js function
        dynamic a = (dynamic)b.Document.InvokeScript("jsFunc2"); // c#'a' is js'a'
        string x = a.foo;
        MessageBox.Show(x); // bar!
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to call javascript function from c# Is there a way to
Is it possible to call a javascript function from a controller in rails?
Is it possible to call the base method from a prototype method in JavaScript
How can I call a ViewResult controller action/method from jquery/javascript using something similar to
Is it possible to call a javascript function from the URL? I am basically
UPDATE: An alternative title for this could be: How do I call javascript from
Is it possible to call a method attributed with [WebMethod] from javascript on a
Possible Duplicate: javascript object, access variable property name? I'm trying to call a method
Is it possible to call a .ctp file from a layout which is also
Is it possible to call an exe file and get result from it? (basic)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.