c#调用js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制
using mshtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
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;
namespace Web_wpf
{
////c#调用web里的js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
initWb();
}
public void initWb()
{
wb1.Navigate("http://www.wjf.com/test.html");//= new Uri(file.FullName);
// IHTMLDocument2 doc = wb1.Document;
#region MyRegion
// //System.IO.FileInfo file = new System.IO.FileInfo("index.htm");
//// WebBrowser控件显示的网页路径
//wb1.Navigate("http://www.wjf.com/test.html");//= new Uri(file.FullName);
//// 将当前类设置为可由脚本访问
//info info = new info();
//info.mw = this;
//wb1.ObjectForScripting = info;
#endregion
}
public void kuayu()
{
//IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb1.Document;
//FramesCollection frames = doc.parentWindow.frames;
//String testValue = doc.GetElementById("search").GetAttribute("value");
////IHTMLDocument3 doc = ;
//IHTMLDocument3 baiduDoc = CorssDomainHelper.GetDocumentFromWindow(frames[0].DomWindow as IHTMLWindow2);
//baiduDoc.getElementById("kw").setAttribute("value", testValue);
//baiduDoc.getElementById("su").click();
}
private void CallIHTMLwindow2()
{
string scriptline01 = @"function ShowPageInfo() {";
string scriptline02 = @" var numLinks = document.links.length; ";
string scriptline03 = @" var numForms = document.forms.length; ";
string scriptline04 = @" var numImages = document.images.length; ";
string scriptline05 = @" var numScripts = document.scripts.length; ";
string scriptline06 = @" alert('网页的统计结果:\r\n链接数:' + numLinks + ";
string scriptline07 = @" '\r\n表单数:' + numForms + ";
string scriptline08 = @" '\r\n图像数:' + numImages + ";
string scriptline09 = @" '\r\n脚本数:' + numScripts);}";
string scriptline10 = @"ShowPageInfo();";
string strScript = scriptline01 + scriptline02 + scriptline03 + scriptline04 + scriptline05 +
scriptline06 + scriptline07 + scriptline08 + scriptline09 + scriptline10;
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb1.Document;
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc.parentWindow;
win.execScript(strScript, "Javascript");
}
//C# call js
private void Button_Click(object sender, RoutedEventArgs e)
{
wb1.InvokeScript("messageBox", new string[] { "123", "456" });
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
CallIHTMLwindow2();
}
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]// 注意: 类定义前需要加上下面两行,否则调用失败!
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class info
{
public MainWindow mw;
public void ShowMessage(string message)
{
MessageBox.Show(message);
// mw.Close();
}
}
}
c#调用js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制的更多相关文章
- [转]html页面调用js文件里的函数报错onclick is not defined处理方法
原文地址:http://blog.csdn.net/ywl570717586/article/details/53130863 今天处理html标签里的onclick功能的时候总是报错:Uncaugh ...
- jsp页面:js方法里嵌套java代码(是操作数据库的),如果这个js 方法没被调用,当jsp页面被解析的时候,不管这个js方法有没有被调用这段java代码都会被执行?
jsp页面:js方法里嵌套java代码(是操作数据库的),如果这个js 方法没被调用,当jsp页面被解析的时候,不管这个js方法有没有被调用这段java代码都会被执行? 因为在解析时最新解析的就是JA ...
- 在js文件里调用另一个js文件里的函数
这个是我今天解决的一个小问题,我在创建界面的时候,根据不同的界面需求对应创建了不同的js文件来搭建界面,搭建完毕之后再将各个生成页面的函数汇总到主界面上,通过visibility属性切换显示,这时候出 ...
- js调用php和php调用js的方法举例
js调用php和php调用js的方法举例1 JS方式调用PHP文件并取得php中的值 举一个简单的例子来说明: 如在页面a.html中用下面这句调用: <script type="te ...
- JS调用PHP 和 PHP调用JS的方法举例
http://my.oschina.net/jiangchike/blog/220988 1.JS方式调用PHP文件并取得PHP中的值举一个简单的例子来说明:如在页面test_json1中用下面这句调 ...
- Js文件中调用其它Js函数的方法
在项目开发过程中,也许你会遇这样的情况.在某一Js文件中需要完成某一功能,但这一功能的大部分代码在另外一个Js文件中已经完成了,自己只需要调用这个方法再加上几句代码就可以实现所需的功能.我们知道,在h ...
- 【Angular JS】正确调用JQuery与Angular JS脚本 - 修复Warning: Tired to load angular more than once
自己正在做一个小网站,使用Angular JS + Express JS + Mongo DB,在开发过程中,遇到一些问题,所以整理出来.希望对大家都有帮助. 这是今天解决的一个问题,Angular ...
- js调用后台,后台调用前台等方法总结
1. javaScript函数中执行C#代码中的函数:方法一:1.首先建立一个按钮,在后台将调用或处理的内容写入button_click中; 2.在前台写一个js函数,内容为docume ...
- Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结
Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结 1.1. 边缘检测的基本方法Canny最常用了1 1.2. 编写matlab边缘检测代码, ...
随机推荐
- Using Run-Time Dynamic Linking(使用运行时动态链接库)
// A simple program that uses LoadLibrary and // GetProcAddress to access myPuts from Myputs.dll. #i ...
- WEB开发时Browser控件得到C:\fakepath\ 的解决方式
IE9中JS获得文件上传控件的路径不对,为:C:\fakepath\ 原来要修改: 工具 -> Internet选项 -> 安全 -> 自定义级别 -> 将本地文件上载至服务 ...
- /Users/XX/Library/Developer/Xcode/DerivedData/XX.app/xxsdk.bundle Directory not empty
今天在升级xcode后真机调试偶然发现这个问题,查了一些资料发现还是不能完全解决 解决方法:参考的(http://blog.csdn.net/alincexiaohao/article/details ...
- ORACLE 关连更新 update select
总结: 关键的地方是where 语句的加入. 在11G中, 如果不加11G , 或造成除匹配的行数更新为相应的值之后, 其余的会变成负数. 所以, 测试的办法就是: 先查看需要更新的数量即连接的数 ...
- c语言中结构体的定义、初始化及内存分配
#include <stdio.h> struct person { char *name; int age; }; int main() { //结构体可以定义在函数内,也可以定义到函数 ...
- jQuery弹性滑块导航
曾起何时在某网站上看到一弹性滑块导航的效果,瞬间被些效果吸引,开始以为是用FLASH实现的,但查源代码发现用的是JQuery缓动效果. 今天心血来潮想拿这个效果练练手.也看看这段时间学习JS及jque ...
- Mysql笔记【4】-查询操作
1.查询所有列数据 select * from 表名 一般情况下,除非使用表中所有字段,最好不要使用通配符 "*",如果不知道所需要的列名,可以使用*查询获取 2.带in关键字的查 ...
- leetcode342——Power of Four(C++)
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- rapidxml对unicode的支持
为了提高duilib创建布局控件的效率,在LuaDui项目中使用rapidxml解析器替换了duilib库自带的xml解析器. duilib使用unicode编译,所以rapidxml需要解析unic ...
- NODE JS拼命吹,我就不喜欢. 别问为什么,直觉.
NODE JS拼命吹,我就不喜欢. 别问为什么,直觉. 来看看node js 在paypal的捣鼓文章吧.https://www.paypal-engineering.com/2013/11/22/n ...