• c# webBrowser控件与js的交互


     转自:http://blog.csdn.net/sd2131512/article/details/7467564

    1. [System.Runtime.InteropServices.ComVisibleAttribute(true)] 
    2. 这是为了将该类设置为com可访问 
    3.  
    4. Url属性:WebBrowser控件显示的网页路径  
    5.  
    6. ObjectForScripting属性:该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问 
    7. JavaScript通过window.external调用C#公开的方法。即由ObjectForScripting属性设置的类的实例中所包含的公共方法。 
    8.  
    9.   
    10. // WebBrowser控件显示的网页路径 
    11. webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/XXX.html", UriKind.RelativeOrAbsolute)); 
    12. // 将当前类设置为可由脚本访问 
    13. webBrowser1.ObjectForScripting = this
    1. [System.Runtime.InteropServices.ComVisibleAttribute(true)]  
    2. 这是为了将该类设置为com可访问  
    3.   
    4. Url属性:WebBrowser控件显示的网页路径   
    5.   
    6. ObjectForScripting属性:该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问  
    7. JavaScript通过window.external调用C#公开的方法。即由ObjectForScripting属性设置的类的实例中所包含的公共方法。  
    8.   
    9.    
    10. // WebBrowser控件显示的网页路径  
    11. webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/XXX.html", UriKind.RelativeOrAbsolute));  
    12. // 将当前类设置为可由脚本访问  
    13. webBrowser1.ObjectForScripting = this;  

    .CS

    1. using System; 
    2. using System.Collections.Generic; 
    3. using System.Linq; 
    4. using System.Text; 
    5. using System.Windows; 
    6. using System.Windows.Controls; 
    7. using System.Windows.Data; 
    8. using System.Windows.Documents; 
    9. using System.Windows.Input; 
    10. using System.Windows.Media; 
    11. using System.Windows.Media.Imaging; 
    12. using System.Windows.Navigation; 
    13. using System.Windows.Shapes; 
    14. using System.Web; 
    15. using System.Security.Permissions; 
    16. namespace WpfApplication1 
    17.     /// <summary> 
    18.     /// Interaction logic for Window1.xaml 
    19.     /// </summary> 
    20.     public partial class Window1 : Window 
    21.     { 
    22.         public Window1() 
    23.         { 
    24.             InitializeComponent(); 
    25.             Basic ds = new Basic (); 
    26.             webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/aaa.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件 
    27.             webBrowser1.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问 
    28.         } 
    29.         privatevoid Button_Click(object sender, RoutedEventArgs e) 
    30.         { 
    31.             textBox1.Text = DoSomething.name; 
    32.         } 
    33.  
    34.     } 
    35.     [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问 
    36.     publicclass Basic 
    37.     { 
    38.         publicstaticstring name; 
    39.         publicstring Name 
    40.         { 
    41.             get { return name; } 
    42.             set { name = value; } 
    43.         } 
    44.         publicvoid ClickEvent(string str) 
    45.         { 
    46.             this.Name = str; 
    47.         } 
    48.     } 
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Windows;  
    6. using System.Windows.Controls;  
    7. using System.Windows.Data;  
    8. using System.Windows.Documents;  
    9. using System.Windows.Input;  
    10. using System.Windows.Media;  
    11. using System.Windows.Media.Imaging;  
    12. using System.Windows.Navigation;  
    13. using System.Windows.Shapes;  
    14. using System.Web;  
    15. using System.Security.Permissions;  
    16. namespace WpfApplication1  
    17. {  
    18.     /// <summary>  
    19.     /// Interaction logic for Window1.xaml  
    20.     /// </summary>  
    21.     public partial class Window1 : Window  
    22.     {  
    23.         public Window1()  
    24.         {  
    25.             InitializeComponent();  
    26.             Basic ds = new Basic ();  
    27.             webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/aaa.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件  
    28.             webBrowser1.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问  
    29.         }  
    30.         private void Button_Click(object sender, RoutedEventArgs e)  
    31.         {  
    32.             textBox1.Text = DoSomething.name;  
    33.         }  
    34.   
    35.     }  
    36.     [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问  
    37.     public class Basic  
    38.     {  
    39.         public static string name;  
    40.         public string Name  
    41.         {  
    42.             get { return name; }  
    43.             set { name = value; }  
    44.         }  
    45.         public void ClickEvent(string str)  
    46.         {  
    47.             this.Name = str;  
    48.         }  
    49.     }  
    50. }  

    HTML

    1. <HTML> 
    2. <head> 
    3. <mce:script language="JavaScript" type="text/javascript"><!-- 
    4. function Selec() 
    5. var divV=document.getElementById('div2').innerText; 
    6. window.external.ClickEvent(divV); 
    7. // --></mce:script> 
    8. </head> 
    9. <Body> 
    10. <Form> 
    11. <div id="div1" onClick="Selec();">000000000000</div> 
    12. <div id="div2">111111</div> 
    13. </Form> 
    14. </Body> 
    15. </HTML> 
    1. <HTML>  
    2. <head>  
    3. <mce:script language="JavaScript" type="text/javascript"><!--  
    4. function Selec()  
    5. {  
    6. var divV=document.getElementById('div2').innerText;  
    7. window.external.ClickEvent(divV);  
    8. }  
    9. // --></mce:script>  
    10. </head>  
    11. <Body>  
    12. <Form>  
    13. <div id="div1" onClick="Selec();">000000000000</div>  
    14. <div id="div2">111111</div>  
    15. </Form>  
    16. </Body>  
    17. </HTML>  

    如果需要在运行时点击按钮后再将值传入页面显示,则用下列方法传值

    this.webBrowser1.InvokeScript("js中的函数",“要传的值”);

  • 相关阅读:
    三.Python数据类型详述
    二.Python基础语法和数据类型
    一.Python特点
    Hive之explode和lateral view
    javascript之函数作用域
    javascript之函数使用
    javascript之函数定义
    javascript之变量
    Html之元素
    Html之页面
  • 原文地址:https://www.cnblogs.com/nanfei/p/3487229.html
Copyright © 2020-2023  润新知