locked
Calling Javascript using C# WinForm

    Question

  •  

    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

    Wednesday, November 14, 2007 12:48 PM

Answers

  • 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.

    Wednesday, November 14, 2007 5:38 PM
  • 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

     

     

    Thursday, November 15, 2007 9:48 AM

All replies

  • 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.

    Wednesday, November 14, 2007 5:38 PM
  •  

    hi..

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

     

    reg

    Inba

    Thursday, November 15, 2007 9:11 AM
  • 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

     

     

    Thursday, November 15, 2007 9:48 AM
  • 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, October 27, 2009 1:23 PM
  • 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.
    Tuesday, November 24, 2009 1:31 PM
  • I have a Javascript file named vbulletin_md5.js

     

    var hexcase=0;
    var b64pad="";
    var chrsz=8;

    function hex_md5(A){return binl2hex(core_md5(str2binl(A),A.length*chrsz))}
    function b64_md5(A){return binl2b64(core_md5(str2binl(A),A.length*chrsz))}
    ...

    many other functions here
    ...

    function md5hash(B,A,E,C)
    {
        if(      navigator.userAgent.indexOf("Mozilla/")==0
            &&   parseInt(navigator.appVersion)>=4          )
        {    var D=hex_md5(str_to_ent(trim(B.value)));
            A.value=D;
            if(E)
            {    D=hex_md5(trim(B.value));
                E.value=D}
                if(!C)
                {   
                    B.value=""                       
                }
        }
            return true
    };

    I want to reuse the function md5hash without recoding it and relate function in C# (not ASP)

    How to do?

     

    Monday, July 26, 2010 5:43 AM
  • Saturday, August 7, 2010 12:50 AM
  • here is a link about the webrowser-document-invokescript method that can use to issue the js

    http://www.codeproject.com/tips/60924/Using-WebBrowser-Document-InvokeScript-to-mess-aro.aspx

    Saturday, January 8, 2011 8:49 AM