c# webBrowser控件与js的交互

时间:2023-03-09 14:36:32
c# webBrowser控件与js的交互

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

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

[System.Runtime.InteropServices.ComVisibleAttribute(true)]
这是为了将该类设置为com可访问

Url属性:WebBrowser控件显示的网页路径

ObjectForScripting属性:该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
JavaScript通过window.external调用C#公开的方法。即由ObjectForScripting属性设置的类的实例中所包含的公共方法。

// WebBrowser控件显示的网页路径
webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/XXX.html", UriKind.RelativeOrAbsolute));
// 将当前类设置为可由脚本访问
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. {
  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. privatevoid Button_Click(object sender, RoutedEventArgs e)
  31. {
  32. textBox1.Text = DoSomething.name;
  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. }
  49. }
  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. [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问
  36. public class Basic
  37. {
  38. public static string name;
  39. public string Name
  40. {
  41. get { return name; }
  42. set { name = value; }
  43. }
  44. public void ClickEvent(string str)
  45. {
  46. this.Name = str;
  47. }
  48. }
  49. }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Web;
using System.Security.Permissions;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Basic ds = new Basic ();
webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/aaa.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件
webBrowser1.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
}
private void Button_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = DoSomething.name;
}

}
[System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问
public class Basic
{
public static string name;
public string Name
{
get { return name; }
set { name = value; }
}
public void ClickEvent(string str)
{
this.Name = str;
}
}
}

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

<HTML>
<head>
<mce:script language="JavaScript" type="text/javascript"><!--
function Selec()
{
var divV=document.getElementById('div2').innerText;
window.external.ClickEvent(divV);
}
// --></mce:script>
</head>
<Body>
<Form>
<div id="div1" onClick="Selec();">000000000000</div>
<div id="div2">111111</div>
</Form>
</Body>
</HTML>

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

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