从C#调用javascripts [英] Calling javascripts from C#

查看:55
本文介绍了从C#调用javascripts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


< font face ="Verdana,Geneva,Arial,Sans-serif"color ="#0000ff"size = 2>我不确定,我是否将此帖子发布到正确的组。请帮忙,因为这很紧急。


我正在加载我的C#代码中的页面网页(jsp)。我能够获取加载页面的文档完成事件。然后我使用intenet explorer类来获取当前加载页面的文档。我也可以访问网页上的各种控件。当我尝试访问网页中的js函数时出现问题。然后我们得到一个"无效的强制转换异常"的例外。我已经导入了所有需要的参考dll,比如shDocvw和mshtml。


调试时我明白,在获取文档本身时,文档的某些属性(如框架,父窗口和脚本)会出现此异常。


public SHDocVw。 InternetExplorer ieWebBrowser;


mshtml。 IHTMLDocument2 doc = ieWebBrowser。将记录为 mshtml。 IHTMLDocument2 ;


mshtml。 IHTMLWindow2 windo w = doc.Script as mshtml。 IHTMLWindow2 ;


window.execScript(" MyJavaScript(43535)" " javascript" );


我如何克服这个?

解决方案

错误是bcoz您正在尝试投射doc .Script到mshtml.IHTMLWindow2

试试这个..........

在表单中添加一个Web浏览器控件...

webBrowser1。 Document.InvokeScript(" MyJavaScript ",new object [] {" 4​​3535 " });结果

Hi all,

         I am not sure, whether I am posting this to the right group. Please help as this is urgent.

         I am loading a page webpage(jsp) from my C# code. I am able to get the document complete event of the page loaded. Then I am using the intenet explorer class to get the document of the currently loaded page. I am also able to access the various controls on the webpage. The problem arises when I am trying to access js functions in the webpage. Then we are getting an exception of "Invalid Cast Exception".I have imported all the reference dlls needed,like shDocvw and mshtml.

While debugging I understand that while getting the document itself this exception is present for some of the properties of the document like frames, parent window and scripts.

The following is the sample code snippet:

public SHDocVw.InternetExplorer ieWebBrowser;

mshtml.IHTMLDocument2 doc = ieWebBrowser.Document as mshtml.IHTMLDocument2;

mshtml.IHTMLWindow2 window = doc.Script as mshtml.IHTMLWindow2;

window.execScript("MyJavaScript(43535)", "javascript");

How can I overcome this?

解决方案

The error is bcoz you are trying to cast doc.Script to mshtml.IHTMLWindow2

Try this one..........

Add a Web Browser control to the form...

webBrowser1.Document.InvokeScript("MyJavaScript", new object[] { "43535" });


这篇关于从C#调用javascripts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆