最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to call JavaScript inside a WebBrowser control? - Stack Overflow

programmeradmin1浏览0评论

I want to call the JavaScript function "Goto" like this:

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:

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

Do you have any idea?

I want to call the JavaScript function "Goto" like this:

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:

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

Do you have any idea?

Share Improve this question edited Mar 2, 2022 at 9:33 Uwe Keim 40.7k61 gold badges187 silver badges302 bronze badges asked Dec 2, 2014 at 15:14 mosflexmosflex 1711 gold badge2 silver badges15 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 13

Since you are using a WebBrowser object, I will assume that this is actually a Windows forms question and not an asp.net question.

You should look at the InvokeScript function of the web browser.

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:

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

Or this:

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

Check out this doc...

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

Maybe ... put the javascript:Goto into the

<body onload="">

... inside the quotes.

发布评论

评论列表(0)

  1. 暂无评论