• Home
  • 简单生活
  • 一般技术
  • .NET
  • XML-RPC
  • FreeBSD
  • Linux
  • Python
  • Database
  • 书籍阅读
  • 电视电影
  • 网站相关
  • 乱七八糟
  • 娱记
  • 常用软件
  • 标签云

  • C# WinForm程序如何与WebBrowser中js交互

    2009年01月21日 WebBrowser WinForm

     程序调用JS脚本如下:
    JS脚本代码:
     <script language="javascript">
    function  WrongMsg(msg)
    {
    alert(msg);
    return;
    }
    </script>

    C#代码调用如下:
     using System.Security.Permissions;

        注意: 类定义前需要加上下面两行,否则调用失败!
     [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]

        调用的函数:
     webBrowser1.Document.InvokeScript("WrongMsg", new string[] {'ssss'});

    JS中调用C#函数示例:
    C#函数如下:
     public void ShowMessage(string msg)
    {
    MessageBox.Show(msg);
    }

    JS里的调用方法:
     <script language="javascript">
    function  InvokeFunc()
    {
    window.external.ShowMessage(msg);
    }
    </script>

    评论(1 条评论)

    1. 感谢楼主, 有用

    发表评论

    *必填

    *必填 (不会被公开)

    *
    To prove you're a person (not a spam script), type the security word shown in the picture.
    Anti-Spam Image