question

ansalc-1556 avatar image
0 Votes"
ansalc-1556 asked ·

WebView.InvokeScriptAsync code examples

Can someone provide some code examples for using WebView.InvokeScriptAsync

I am successfully using simple code like

             Dim scriptString As String = "document.getElementsByName('submit')[0].click()"
             Await WebView1.InvokeScriptAsync("eval", {scriptString})

However, I would like to be able to write more sofisticated code like the one below that I used in my old VB projects using Internet Explorer referencing the Microsoft HTML Object Library:

Public WithEvents Wbe As New SHDocVw.InternetExplorer

HTMLDoc = Wbe.Document

     iHTMLCol = HTMLDoc.getElementsByTagName("input")

     For Each iHTMLEle In iHTMLCol
         If Not iHTMLEle.getAttribute("name") Is Nothing Then
             If iHTMLEle.getAttribute("name").ToString = "sort" Then
                 iHTMLEle.setAttribute("value", "1234")
                 Exit For
             End If
         End If
     Next

I guess it is just a matter of the writing what I call above the scriptString. But some actual code examples will be helpful

uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered ·

Hello,

Welcome to Microsoft Q&A!

There are two main uses about webview.InvokeScriptAsync(), the first is to call a function that already exists in the HTML, and could pass the parameters or with the JavaScript eval function to inject content into the web page. If you want to achieve the function about the sofisticated code like the one above that you used in your old VB projects, you need to convert the code snippet from VB to the JavaScript and then put the JavaScript statements into webview.InvokeScriptAsync() method.

About how to interact with the content of the WebView by using the InvokeScriptAsync method, you can refer to this document which contains some code samples.

· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello FayWang,

Thank you for your input. I will go from there.

0 Votes 0 · ·

OK, if you have any questions about InvokeScriptAsync, please feel free to ask.

0 Votes 0 · ·