Partager via


CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(String) Method

Definition

Adds the provided JavaScript to a list of scripts that should be run after the global object has been created, but before the HTML document has been parsed and before any other script included by the HTML document is run.

public System.Threading.Tasks.Task<string> AddScriptToExecuteOnDocumentCreatedAsync (string javaScript);
member this.AddScriptToExecuteOnDocumentCreatedAsync : string -> System.Threading.Tasks.Task<string>
Public Function AddScriptToExecuteOnDocumentCreatedAsync (javaScript As String) As Task(Of String)

Parameters

javaScript
String

The JavaScript code to be run.

Returns

A script ID that may be passed when calling RemoveScriptToExecuteOnDocumentCreated(String).

Remarks

The injected script will apply to all future top level document and child frame navigations until removed with RemoveScriptToExecuteOnDocumentCreated(String). This is applied asynchronously and you must wait for the returned IAsyncOperation to complete before you can be sure that the script is ready to execute on future navigations. If the method is run in NewWindowRequested handler, it should be called before the new window is set. For more details see NewWindow.

Note that if an HTML document has sandboxing of some kind via sandbox properties or the Content-Security-Policy HTTP header this will affect the script run here. So, for example, if the allow-modals keyword is not set then calls to the alert function will be ignored.

Applies to

See also