user857521
user857521

Reputation:

WebBrowser InvokeScript

I have a Webrowser with some settings that are changed using javascript. I'm trying to use the example here but can't get the correct syntax

the script looks like this

        <div class="DisplayInput"><input type="radio" name="displaytype" 
value="decimal" onclick="setdisplayType('decimal');" checked="checked"><a 
    href="javaScript:setdisplayType('decimal');" 
    onclick="s_objectID=&quot;javascript:setdisplayType('decimal');_1&quot;;return this.s_oc?     this.s_oc(e):true">Decimal</a></div>

So far I've tried these with no success

this.webBrowser1.InvokeScript("setdisplayType");
this.webBrowser1.InvokeScript("setdisplayType('decimal')");
this.webBrowser1.InvokeScript("setdisplayType","decimal");

Upvotes: 3

Views: 9836

Answers (3)

BlackStar Vvek
BlackStar Vvek

Reputation: 1

Try this:

private async void web_LoadCompleted(object sender, NavigationEventArgs e)
{
  Object[] parameters = new Object[1]; 
  await yourBrowser.Document.InvokeScript("scriptName", parameters);
}

Upvotes: 0

Rimcanfly
Rimcanfly

Reputation: 53

You have to pass an array of Object as second parameter. You can do it this way :

Object[] parameters = new Object[1]; //assuming your JS function has one parameter
parameters[0] = (Object)"yourStringParameter"; //for example the parameter is a string
yourBrowser.Document.InvokeScript("scriptName", parameters);

Upvotes: 1

David Hoerster
David Hoerster

Reputation: 28701

Without knowing what's happening with your application's error and also not knowing what setdisplayType looks like, I'm guessing that maybe you're trying to invoke the function setdisplayType before it's been loaded. Per the MSDN documentation...

InvokeScript(String, Object()) should not be called before the document that implements it has finished loading. You can detect when a document has finished loading by handling the LoadCompleted event.

Maybe you can implement the LoadCompleted event handler and then invoke your script.

Hope this helps!

Upvotes: 7

Related Questions