### Method

# WebKit WebView call_async_javascript_function

since: 2.40

#### Declaration [src]

    void
    webkit_web_view_call_async_javascript_function (
      WebKitWebView* web_view,
      const char* body,
      gssize length,
      GVariant* arguments,
      const char* world_name,
      const char* source_uri,
      GCancellable* cancellable,
      GAsyncReadyCallback callback,
      gpointer user_data
    )

#### Description [src]

Asynchronously call `body` with `arguments` in the script world with name `world_name` of the main frame current context in `web_view`. The `arguments` values must be one of the following types, or contain only the following GVariant types: number, string and dictionary. The result of the operation can be a Promise that will be properly passed to the callback. If `world_name` is `NULL`, the default world is used. Any value that is not `NULL` is a distin ct world. The `source_uri` will be shown in exceptions and doesn’t affect the behavior of the script. When not provided, the document URL is used.

Note that if `WebKitSettings:enable-javascript` is `FALSE`, this method will do nothing. If you want to use this method but still prevent web content from executing its own JavaScript, then use `WebKitSettings:enable-javascript-markup`.

When the operation is finished, `callback` will be called. You can then call `webkit_web_view_call_async_javascript_function_finish()` to get the result of the operation.

This is an example that shows how to pass arguments to a JS function that returns a Promise that resolves with the passed argument:

    static void
    web_view_javascript_finished (GObject      *object,
                                  GAsyncResult *result,
                                  gpointer      user_data)
    {
        JSCValue               *value;
        GError                 *error = NULL;

        value = webkit_web_view_call_async_javascript_function_finish (WEBKIT_WEB_VIEW (object), result, &error);
        if (!value) {
            g_warning ("Error running javascript: %s", error->message);
            g_error_free (error);
            return;
        }

        if (jsc_value_is_number (value)) {
            gint32        int_value = jsc_value_to_string (value);
            JSCException *exception = jsc_context_get_exception (jsc_value_get_context (value));
            if (exception)
                g_warning ("Error running javascript: %s", jsc_exception_get_message (exception));
            else
                g_print ("Script result: %d\n", int_value);
            g_free (str_value);
        } else {
            g_warning ("Error running javascript: unexpected return value");
        }
        g_object_unref (value);
    }

    static void
    web_view_evaluate_promise (WebKitWebView *web_view)
    {
        GVariantDict dict;
        g_variant_dict_init (&dict, NULL);
        g_variant_dict_insert (&dict, "count", "u", 42);
        GVariant *args = g_variant_dict_end (&dict);
        const gchar *body = "return new Promise((resolve) => { resolve(count); });";
        webkit_web_view_call_async_javascript_function (web_view, body, -1, arguments, NULL, NULL, NULL, web_view_javascript_finished, NULL);
    }

Available since: 2.40

This method completes asynchronously. Use `webkit_web_view_call_async_javascript_function_finish()` inside the `GAsyncReadyCallback` to obtain the result of the operation.

#### Parameters

`body`

  Type: `const char*`

  The function body.

  The data is owned by the caller of the method.
  The value is a NUL terminated UTF-8 string.

`length`

  Type: `gssize`

  Length of `body`, or -1 if `body` is a nul-terminated string.

`arguments`

  Type: `GVariant`

  A `GVariant` with format `a{sv}` storing the function arguments, or `NULL`.

  The argument can be `NULL`.
  The data is owned by the caller of the method.

`world_name`

  Type: `const char*`

  The name of a `WebKitScriptWorld` or `NULL` to use the default.

  The argument can be `NULL`.
  The data is owned by the caller of the method.
  The value is a NUL terminated UTF-8 string.

`source_uri`

  Type: `const char*`

  The source URI.

  The argument can be `NULL`.
  The data is owned by the caller of the method.
  The value is a NUL terminated UTF-8 string.

`cancellable`

  Type: `GCancellable`

  A `GCancellable` or `NULL` to ignore.

  The argument can be `NULL`.
  The data is owned by the caller of the method.

`callback`

  Type: `GAsyncReadyCallback`

  A `GAsyncReadyCallback` to call when the script finished.

  The argument can be `NULL`.

`user_data`

  Type: `gpointer`

  The data to pass to callback function.

  The argument can be `NULL`.
  The data is owned by the caller of the method.