Udostępnij za pośrednictwem


WebView.ScriptNotify Event

Definition

Occurs when the content contained in the WebView control passes a string to the application by using JavaScript.

public:
 virtual event NotifyEventHandler ^ ScriptNotify;
// Register
event_token ScriptNotify(NotifyEventHandler const& handler) const;

// Revoke with event_token
void ScriptNotify(event_token const* cookie) const;

// Revoke with event_revoker
WebView::ScriptNotify_revoker ScriptNotify(auto_revoke_t, NotifyEventHandler const& handler) const;
public event NotifyEventHandler ScriptNotify;
function onScriptNotify(eventArgs) { /* Your code */ }
webView.addEventListener("scriptnotify", onScriptNotify);
webView.removeEventListener("scriptnotify", onScriptNotify);
- or -
webView.onscriptnotify = onScriptNotify;
Public Custom Event ScriptNotify As NotifyEventHandler 
<WebView ScriptNotify="eventhandler"/>

Event Type

Examples

The following code example demonstrates the use of the ScriptNotify event in apps compiled for Windows 8. Starting with Windows 8.1, omit the lines related to AllowedScriptNotifyUris.

public MyPage()
{
    this.InitializeComponent();
    MyWebView.ScriptNotify += MyWebView_ScriptNotify;

    // Here we have to set the AllowedScriptNotifyUri property because we are 
    // navigating to some site where we don't own the content and we want to 
    // allow window.external.notify() to pass data back to the app.
    List<Uri> allowedUris = new List<Uri>();
    allowedUris.Add(new Uri("http://www.bing.com"));
    MyWebView.AllowedScriptNotifyUris = allowedUris;
}

void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
{
    // Respond to the script notification.
}

Remarks

A hosted HTML page can fire the ScriptNotify event in your UWP app when the page calls window.external.notify and passes a string parameter.

Note

Because this event is initiated by external code, you should be careful about what you put in the event handler. To prevent malicious scripts from exploiting this event, be sure to enable it only for trusted URIs, as described below.

Windows 8.1

To enable an external web page to fire the ScriptNotify event when calling window.external.notify, you must include the page's URI in the ApplicationContentUriRules section of the app manifest. (You can do this in Visual Studio on the Content URIs tab of the Package.appxmanifest designer.) The URIs in this list must use HTTPS and may contain subdomain wildcards (for example, "https://.microsoft.com"), but they can't contain domain wildcards (for example, "https://.com" and "https://."). The manifest requirement does not apply to content that originates from the app package, uses an ms-local-stream:// URI, or is loaded using NavigateToString.

Note

If you have more then one subdomain, you must use one wildcard for each subdomain. For example, "https://*.microsoft.com" matches with "https://any.microsoft.com" but not with "https://this.any.microsoft.com."

These changes do not affect apps compiled for Windows 8, even when running on Windows 8.1.

AllowedScriptNotifyUris, AnyScriptNotifyUri, and AllowedScriptNotifyUrisProperty are not supported in apps compiled for Windows 8.1.

Windows 8

These remarks apply only to apps compiled for Windows 8, even when running on Windows 8.1.

If content is loaded into the WebView control using the Navigate method, the app must opt-in to receiving ScriptNotify events by using the AllowedScriptNotifyUris property, which has the list of URIs that can fire ScriptNotify. If the content is loaded using NavigateToString, the application will receive ScriptNotify events without the opt-in. Set the AllowedScriptNotifyUris property to the value returned by the AnyScriptNotifyUri property to indicate that any page can fire ScriptNotify events to this WebView control.

Applies to

See also