C# WebBrowser控件外部脚本不';行不通
这是存储在字符串资源中的HTML:C# WebBrowser控件外部脚本不';行不通,c#,com,webbrowser-control,C#,Com,Webbrowser Control,这是存储在字符串资源中的HTML: <!DOCTYPE html> <html> <head></head> <body> <script> (function(){ window.external.hello() })() </script> </body> </
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
(function(){
window.external.hello()
})()
</script>
</body>
</html>
这是JSCallbacks.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace JSIE
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class JSCallbacks
{
public void hello() {
MessageBox.Show("Hello, world!");
}
}
}
当我运行它时,它无法访问JavaScript
窗口.external
对象中的hello()
方法,并给我一个脚本错误消息框。我曾尝试使用this
作为对象进行脚本编写,但它也不起作用。明白了:在执行页面之前,您必须使用DocumentText属性加载HTML。对于那些不熟悉对象进行脚本编写的人来说:好吧,这很奇怪。出于某种原因,当我从
标记调用它时,hello()
函数不在“那里”,但如果我从事件属性(onclick=”“
等)调用它,它就在那里了。哦,这可能是因为在调用该代码时,DOM还没有初始化。在解析DOM的其余部分之前,您的代码正在运行。您能否在加载表单后捕获DOM的实时输出?(很抱歉,我也不是100%熟悉这个对象,也从来没有用它写过任何东西。假设加载后可以捕获live DOM元素)加载文档的方式很奇怪。在设置ObjectForScript之后,如何设置DocumentText属性?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace JSIE
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class JSCallbacks
{
public void hello() {
MessageBox.Show("Hello, world!");
}
}
}