Ask a questionAsk a question
 

AnswerCalling Javascript using C# WinForm

  • Wednesday, November 14, 2007 12:48 PMInbaraj Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Hi.. alll

     

       Is it possible to call a Javascript code from C# winform... If yes.. plz help me with sample codeing....

     

    with reg

    Inbaraj.d

Answers

  • Wednesday, November 14, 2007 5:38 PMDerek Smyth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi,

     

    Do you mean Javascript on a web page? Displayed in the Web Browser control?

     

    If you do then the WebBrowsers Document property has an InvokeScript method.

     

    this.WebBrowser.Document.InvokeScript("foo", new string[] { "hello world" });

     

    I have no idea how else you could do it.

  • Thursday, November 15, 2007 9:48 AMYan-Fei Wei Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Inbaraj,

     You can also use the "Microsoft.mshtml library" to do this.

    Assume you have following JavaScript code.

     

    Code Block
      <script   language="javascript">  
      var   a   =   'hello';  
      function   test(msg)  
      {  
          alert(msg);  
          alert(a);  
      }  
      </script>

     

     

    Then you can call the test function like this:

    Code Block

    mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)axwebBrowser1.Document;

    mshtml.IHTMLWindow2 parentWin = doc.parentWindow;

    parentWin.execScript("test(99)", "javascript");

     

    you can get detail information and sample code at:

    http://www.codeproject.com/jscript/htmlgetshooked.asp

     

     

All Replies

  • Wednesday, November 14, 2007 5:38 PMDerek Smyth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi,

     

    Do you mean Javascript on a web page? Displayed in the Web Browser control?

     

    If you do then the WebBrowsers Document property has an InvokeScript method.

     

    this.WebBrowser.Document.InvokeScript("foo", new string[] { "hello world" });

     

    I have no idea how else you could do it.

  • Thursday, November 15, 2007 9:11 AMInbaraj Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    hi..

      I want to call the javascript in C# windows form is it possible...

     

    reg

    Inba

  • Thursday, November 15, 2007 9:48 AMYan-Fei Wei Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Inbaraj,

     You can also use the "Microsoft.mshtml library" to do this.

    Assume you have following JavaScript code.

     

    Code Block
      <script   language="javascript">  
      var   a   =   'hello';  
      function   test(msg)  
      {  
          alert(msg);  
          alert(a);  
      }  
      </script>

     

     

    Then you can call the test function like this:

    Code Block

    mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)axwebBrowser1.Document;

    mshtml.IHTMLWindow2 parentWin = doc.parentWindow;

    parentWin.execScript("test(99)", "javascript");

     

    you can get detail information and sample code at:

    http://www.codeproject.com/jscript/htmlgetshooked.asp

     

     

  • Tuesday, October 27, 2009 1:23 PMmiridfd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi there,

    I am facing the exact scene - I need to run some js through a winform app.
    If you could provide some code, because i found the link above not addressing the issue.

    Thanks in advance
  • Tuesday, November 24, 2009 1:31 PMKumaresh Babu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Jscript cannot be called from the windows forms. Why do want to do so? Is there any postbacks in windows forms?, then y do you need clien side code? Jscrip not necessarily required in windows forms.