简体   繁体   English

如何在winforms中的WebBrowser控件中调用javascript?

[英]How to call javascript inside a WebBrowser control in winforms?

I want to call the javascript function 'Goto' like this : 我想像这样调用javascript函数'Goto':

javascript:Goto('DM_NEW_OBJECT.ASPX?DM_CAT_ID=2063&DM_PARENT_ID=2217&INPUTSELECTION=&DM_OBJECT_ID=0&PACK_ID=0&CASE_ID=0&mode=0&SITE=Default');

the function is located in the DefaultGeneral.aspx page, and i need to call it from within a WebBrowser control 该函数位于DefaultGeneral.aspx页面中,我需要从WebBrowser控件中调用它

webBrowser1.Navigate("http://mySite/DefaultGeneral.aspx");

Do you have any idea ? 你有什么主意吗 ? Thank you. 谢谢。

Since you are using a WebBrowser object, I will assume that this is actually a Windows forms question and not an asp.net question. 由于您使用的是WebBrowser对象,我将假设这实际上是一个Windows窗体问题而不是asp.net问题。

You should look at the InvokeScript function of the web browser. 您应该查看Web浏览器的InvokeScript功能。

Let's say your webpage has the following function: 假设您的网页具有以下功能:

WITHOUT PARAMETERS: 没有参数:

<script type="text/javascript">
    // Function Without Parameters
    function JavaScriptFunctionWithoutParameters() {
        outputID.innerHTML = "JavaScript function called!";
    }
</script>

You would want to call it the following way: 您可能希望通过以下方式调用它:

this.webBrowser.InvokeScript("JavaScriptFunctionWithoutParameters");

WITH PARAMETERS: 带参数:

<script type="text/javascript">
    // Function With Parameters
    function Goto(someParameter) {
        outputID.innerHTML = someParameter;
    }
</script>

You would call it like this: 你会这样称呼它:

object[] param = new object[1];
param [0] = "DM_NEW_OBJECT.ASPX?DM_CAT_ID=2063&amp;DM_PARENT_ID=2217&amp;INPUTSELECTION=&amp;DM_OBJECT_ID=0&amp;PACK_ID=0&amp;CASE_ID=0&amp;mode=0&amp;SITE=Default";
this.webBrowser1.Document.InvokeScript("Goto", param );

In C# you have to do something like this: 在C#中你必须做这样的事情:

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","MyFunction()",true);

Or this: 或这个:

ClientScript.RegisterStartupScript(GetType(),"hwa","alert('Hello World');",true);

Check out this doc... 看看这个doc ...

http://msdn.microsoft.com/en-us/library/system.web.ui.page.clientscript(v=vs.110).aspx

Maybe ... put the javascript:Goto into the 也许......把javascript:转到

<body onload="">

... inside the quotes. ......在报价内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在WinForms WebBrowser控件中禁用JavaScript? - Disable javascript in WinForms WebBrowser control? 如何通过WebBrowser控件为WinForms处理Javascript事件 - How to handle Javascript events via WebBrowser control for WinForms 使用Java的C#Winforms Webbrowser控件 - C# winforms webbrowser control with Javascript 将javascript变量放入webbrowser控件winforms - get javascript variable into webbrowser control winforms Winforms Webbrowser控件中的JavaScript访问问题 - JavaScript access issue in winforms webbrowser control 在托管WebBrowser控件的应用程序中,如何在由WebBrowser控件查看的页面中调用JavaScript函数? - How might I call a JavaScript Function in a Page Viewed by a WebBrowser Control from the Application Hosting the WebBrowser Control? 在WinForms / WPF WebBrowser控件中使用外部JavaScript文件 - Using external JavaScript files in a WinForms/WPF WebBrowser control 在WebBrowser控件(Winforms,C#)中访问Javascript资源 - Accessing Javascript resources in a WebBrowser control (Winforms, C#) 在Winforms Webbrowser控件中调用href单击事件JavaScript - Invoking an href click event JavaScript in winforms webbrowser control 如何在C#WinForms中将Javascript函数调用到Web内容(可能是WebBrowser)上 - How to call Javascript function onto web content (WebBrowser maybe) in C# WinForms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM