C# 如何通过webbrowser.InvokeScript执行jQuery函数?
我按照您的代码@dyatchenko更新了我的代码,但我得到了一个错误:对象引用未设置为第html=InvokeJsFunctionfoo行的对象实例; 拜托,你能帮我解决这个错误吗 更新2!!!!!C# 如何通过webbrowser.InvokeScript执行jQuery函数?,c#,jquery,winforms,webbrowser-control,C#,Jquery,Winforms,Webbrowser Control,我按照您的代码@dyatchenko更新了我的代码,但我得到了一个错误:对象引用未设置为第html=InvokeJsFunctionfoo行的对象实例; 拜托,你能帮我解决这个错误吗 更新2!!!!! 我更新了我的源html。你可以再次检查它帮助我 首先,如果您的页面已经有javascript,您必须在页面上注册它-您不需要它,如下所示: private void RegisterJsFunction(string function) { // Your WebBrowser contr
我更新了我的源html。你可以再次检查它帮助我 首先,如果您的页面已经有javascript,您必须在页面上注册它-您不需要它,如下所示:
private void RegisterJsFunction(string function)
{
// Your WebBrowser control and document
var htmlDocument = this.webBrowser1.Document;
if (htmlDocument != null)
{
HtmlElement head = htmlDocument.GetElementsByTagName("head")[0];
HtmlElement scriptEl = htmlDocument.CreateElement("script");
if (scriptEl != null)
{
IHTMLScriptElement element = (IHTMLScriptElement) scriptEl.DomElement;
element.text = function;
head.AppendChild(scriptEl);
}
}
}
private string InvokeJsFunction(string functionName)
{
string html = string.Empty;
Action action = () =>
{
// Your control and document
var htmlDocument = this.webBrowser1.Document;
if (htmlDocument != null)
{
html=htmlDocument.InvokeScript(functionName).ToString();
}
};
if (this.InvokeRequired) this.Invoke(action);
else action();
return html;
}
string html = string.Empty;
webBrowser1.DocumentCompleted += (s, exx) =>
{
switch (webBrowser1.ReadyState)
{
case WebBrowserReadyState.Complete:
{
RegisterJsFunction("function foo(){Engine.api.arrive();}");
html=InvokeJsFunction("foo"); //object reference not set to an instance of an object
break;
}
}
};
private void RegisterJsFunction(string function)
{
// Your WebBrowser control and document
var htmlDocument = this.webBrowser1.Document;
if (htmlDocument != null)
{
HtmlElement head = htmlDocument.GetElementsByTagName("head")[0];
HtmlElement scriptEl = htmlDocument.CreateElement("script");
if (scriptEl != null)
{
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = function;
head.AppendChild(scriptEl);
}
}
}
然后,您可以这样调用它:
private void RegisterJsFunction(string function)
{
// Your WebBrowser control and document
var htmlDocument = this.webBrowser1.Document;
if (htmlDocument != null)
{
HtmlElement head = htmlDocument.GetElementsByTagName("head")[0];
HtmlElement scriptEl = htmlDocument.CreateElement("script");
if (scriptEl != null)
{
IHTMLScriptElement element = (IHTMLScriptElement) scriptEl.DomElement;
element.text = function;
head.AppendChild(scriptEl);
}
}
}
private string InvokeJsFunction(string functionName)
{
string html = string.Empty;
Action action = () =>
{
// Your control and document
var htmlDocument = this.webBrowser1.Document;
if (htmlDocument != null)
{
html=htmlDocument.InvokeScript(functionName).ToString();
}
};
if (this.InvokeRequired) this.Invoke(action);
else action();
return html;
}
string html = string.Empty;
webBrowser1.DocumentCompleted += (s, exx) =>
{
switch (webBrowser1.ReadyState)
{
case WebBrowserReadyState.Complete:
{
RegisterJsFunction("function foo(){Engine.api.arrive();}");
html=InvokeJsFunction("foo"); //object reference not set to an instance of an object
break;
}
}
};
private void RegisterJsFunction(string function)
{
// Your WebBrowser control and document
var htmlDocument = this.webBrowser1.Document;
if (htmlDocument != null)
{
HtmlElement head = htmlDocument.GetElementsByTagName("head")[0];
HtmlElement scriptEl = htmlDocument.CreateElement("script");
if (scriptEl != null)
{
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = function;
head.AppendChild(scriptEl);
}
}
}
这是一个演示如何使用这两个函数的示例:
private void InvokeJsFunction(string functionName)
{
Action action = () =>
{
// Your control and document
var htmlDocument = this.webBrowser1.Document;
if (htmlDocument != null)
{
htmlDocument.InvokeScript(functionName);
}
};
if (this.InvokeRequired) this.Invoke(action);
else action();
}
这些代码剪辑来自我的项目。我也经常使用它们。
对于您的示例,您必须首先更改javascript代码:
RegisterJsFunction("function foo() { alert('hello world'); }");
InvokeJsFunction("foo");
然后您可以使用InvokeJSFunctionMyArrival;调用所需的函数;。希望这能有所帮助。如果要获取在Arrival方法的dataStracture变量中声明的表,必须使用以下代码:
<script type="text/javascript">
function MyArrive(_){
// arrive:function(_) stuff here
}
$.extend(Engine.api,{
Push:function(_){ ... },
Oarrive:function(){ ... },
arrive: function(_) { MyArrive(); },
comments:function(_){ ... },
commentbox:function(_){ ... },
comment:function(_){ ... },
del:function(_){ ... },
edit:function(_){ ... },
save:function(_){ ... },
free:function(_){ ... },
tags:function(){ ... },
files:function(_){... }
});
</script>
html变量将是理想的表,没有NullReferenceException。到达:函数不返回任何内容。您希望从这个函数中得到什么?这不是webBrowser控件的问题,而是您试图调用错误的函数。您刚刚获取了Javascript声明的一部分,这自然不起作用。您希望使用单个参数调用函数,但该函数是Engine.api的一部分。因此,你必须重新思考你正在做什么,以及你想实际完成什么。另外,它不是一个jQuery函数,也不会返回任何东西……好吧,对不起,我知道了。所以你可以通过调用script@Sami Kuhmonen帮助我执行$.extendEngine.apidyatchenko@Cornor不幸的是不是,因为我假设这也不是您想要调用的。在实际尝试调用Javascript方法之前,我建议您熟悉Javascript,并解释您实际尝试做什么。这意味着不调用函数X,但有一个带有链接/解释的页面Y有这个脚本,我想在WebBrowser控件中使用它来执行Z和U。谢谢你的帮助,但你的示例是javascript,但我的问题是jqueryso。如果我遵循您的代码,您可以帮助我介绍一个关于通过在代码上方调用脚本来执行jquery的示例。我必须将代码从Arrival复制到MyArrival,但我如何复制它?因为这个源jquery是我在运行webbrowser.navigateurl.I想要执行Arrival:函数后得到的,因为它包含我想要的内容get@Conor在本例中,您可以注册下一个函数RegisterJsFunctionfunction foo{Engine.api.arrival;};在浏览器导航之后,然后使用InvokeJsFunctionfoo调用它;我更新了我的代码,因为我按照您的代码运行,所以我得到错误对象引用未设置为对象的实例我确实遵循此代码,但对象引用仍未设置为对象的实例..我在webBrowser1.DocumentCompleted+=s,exx=>{switch webBrowser1.ReadyState]中输入此代码{case WebBrowserReadyState.Complete:{registerjsfunction foo{Engine.api.arrival;返回$'ttable.html;};html=InvokeJsFunctionfoo.ToString;//html=webBrowser1.Document.invokescriptarive:function..ToString;break;}你在这里@dyatchenko吗?我尝试了更多方法,但仍然将对象引用未设置为对象错误的实例!!!