1

I'd like to run a js function that is not written into loaded html page, for example we have a simple html page with button, and I'd like to disable the button using an external js script:

    <!DOCTYPE html>
    <html>
    <body>

    <button id="my_button" type="button" onclick="">Click Me!</button>

    </body>
    </html>

and I have my script form a file:

 function disableFun() {
 document.getElementById("my_button").disabled = true;
 }

as is mentioned in official doc there is a API method InvokeScriptAsync(string scriptName, string[] arguments) but is mentioned that we must already have the script into html loaded page.

Any advices are apreciated!

P.S. in android we do it very simple webView.loadUrl('my script code')

1 Answer 1

2

You can use InvokeScriptAsync with the JavaScript eval function to inject content into the web page.

see Interacting with WebView content section of this document.

So to disable the button, you can use the following codes in your cs file:

String scriptStr = @"document.getElementById('your_buttonId').disabled=true";
await myWebView.InvokeScriptAsync("eval", new String[] {scriptStr});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.