Skip to main content

call_js_function

Function call_js_function 

Source
pub fn call_js_function<S, I>(name: S, args: I) -> Result<JsValue, Error>
where S: AsRef<str>, I: IntoIterator, I::Item: Into<JsValue>,
Expand description

Calls a global JavaScript function by name, defaulting the this context to null.

This is a convenience wrapper around call_js_function_with_context. It allows callers to simply provide the function name and an iterable of arguments without worrying about the this binding. Passing null as this means that in non‑strict mode, JavaScript will default to using the global window object.

§Parameters

  • name - The name of the JavaScript function (a property on the global window object).
  • args - An iterable list of arguments to pass to the function call.

§Type Parameters

  • S: AsRef<str> – Type for representing the name of the function.
  • I: IntoIterator – An iterable collection of function arguments.
  • I::Item: Into<JsValue> – Each argument can be converted into a JsValue.

§Errors

See call_js_function_with_context for error details.

§Examples

Calling a global function like alert:

// Calls alert("Hello World!") on the global window.
call_js_function("alert", ["Hello World!"])?;