首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 如何让IE浏览器运行js脚本

2013-07-04 
C# 怎么让IE浏览器运行js脚本现在有IE浏览器进程,已经打开某网站,希望通过C# 让此IE窗口执行某脚本,如scr

C# 怎么让IE浏览器运行js脚本
现在有IE浏览器进程,已经打开某网站,
希望通过C# 让此IE窗口执行某脚本,如<script>alert();</script>

1,不写IE插件
2,不借助第三方浏览器,只针对IE
3,不使用Winform的WebBrowser控件.

知道的朋友给个思路


补充说明:
我使用下面这段代码试了,'aaaaaaaaa'会在页面中显示出来,但是后面那段js脚本却没有运行.很奇怪.
ShellWindows m_IEFoundBrowsers = new ShellWindowsClass();
foreach (InternetExplorer Browser in m_IEFoundBrowsers)
{
if (Browser.Document is HTMLDocumentClass)
{
HTMLDocument doc = Browser.Document as HTMLDocumentClass;
doc.body.innerHTML += "aaaaaaaaa<script type="text/javascript">alert(11);</script>";
}
} Internet?Explorer C# JavaScript
[解决办法]
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow;//拿到html对象
win.execScript("changeRegImg()", "javascript");//调用JS

用Microsoft.mshtml.dll可以再c#程序里操作IE。


[解决办法]

引用:
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow;//拿到html对象
win.execScript("changeRegImg()", "javascript");//调用JS

用Microsoft.mshtml.dll可以再c#程序里操作IE。

+1
[解决办法]
引用:


引用:
嘿嘿,我自己搞定了,不过COM这东西真烦人,接口之间没有继承关系,无法匿名转换,但是强转却也能转换,
谁能给我一下解释吗?

下面这段代码是可以成功运行的,也是完全满足要求的.
ShellWindows m_IEFoundBrowsers = new ShellWindowsClass();
foreach (InternetExplorer Browser in m_IEFoundBrowsers)
{
if (Browser.Document is HTMLDocumentClass)
{
HTMLDocumentClass doc2 = Browser.Document as HTMLDocumentClass;
HTMLScriptElement script = (HTMLScriptElement)doc2.createElement("script");
script.text = "alert('a');";
HTMLBodyClass body = doc2.body as HTMLBodyClass;
body.appendChild((IHTMLDOMNode)script);
}
}



-------------
COM是有继承的,至于匿名么貌似没有

ShellWindows m_IEFoundBrowsers = new ShellWindowsClass();
----------------
ShellWindows 继承了IShellWindows, DShellWindowsEvents_Event这两个接口.包含了公共枚举IEnumerable,和两个事件方法。主要返回回windows窗口数目


foreach (InternetExplorer Browser in m_IEFoundBrowsers)
//遍历当前IE实例
{
if (Browser.Document is HTMLDocumentClass)
{
HTMLDocumentClass这个继承了大约十二到十一个接口和一堆的方法。主要是用来增强HTMLDocument这个mS 接口。
HTMLDocumentClass doc2 = Browser.Document as HTMLDocumentClass;
HTMLScriptElement script = (HTMLScriptElement)doc2.createElement("script");
//HTMLScriptElement ,ms接口 。下面三句是添加一个Element元素到BODY里面(DOM树)
script.text = "alert('a');";
HTMLBodyClass body = doc2.body as HTMLBodyClass;
body.appendChild((IHTMLDOMNode)script);
}
}

热点排行