日期:2014-05-17  浏览次数:21333 次

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);
}
}