yewstack/yew · warning

global document not set

Error message

global document not set

What it means

The tutorial constructs a Document via Document::new(), which maps to the JS Document constructor - browsers reject constructing documents this way (illegal constructor), and the binding is unavailable in non-window environments, so the expect fires. Despite the message, you almost never want a new document: you want the page's existing global document.

Source

Thrown at website/versioned_docs/version-0.21/concepts/basic-web-technologies/web-sys.mdx:183

```rust ,no_run
use wasm_bindgen::{prelude::Closure, JsCast};
use web_sys::{console, Document, HtmlElement, MouseEvent};

let mousemove = Closure::<dyn Fn(MouseEvent)>::wrap(Box::new(|e| {
    let rect = e
        .target()
        .expect("mouse event doesn't have a target")
        .dyn_into::<HtmlElement>()
        .expect("event target should be of type HtmlElement")
        .get_bounding_client_rect();
    let x = (e.client_x() as f64) - rect.left();
    let y = (e.client_y() as f64) - rect.top();
    console::log_1(&format!("Left? : {} ; Top? : {}", x, y).into());
}));

Document::new()
    .expect("global document not set")
    .get_element_by_id("mousemoveme")
    .expect("element with id `mousemoveme` not present")
    .unchecked_into::<HtmlElement>()
    .set_onmousemove(mousemove.as_ref().dyn_ref());

// we now need to save the `mousemove` Closure so that when
// this event fires the closure is still in memory.
```

This version is much more verbose, but you will probably notice part of that is because of failure
types reminding us that some of these function calls have invariants that must be held, or otherwise will
cause a panic in Rust. Another part of the verbosity is the calls to `JsCast` to cast into
different types so that you can call its specific methods.

### Yew example

In Yew you will mostly be creating [`Callback`](concepts/function-components/callbacks.mdx)s to use in the
[`html!`](concepts/html/introduction.mdx) macro so the example is going to use this approach instead of completely copying

View on GitHub (pinned to 0e4a05472f)

Solutions

  1. Use the existing document: web_sys::window().unwrap().document().unwrap() or gloo::utils::document()
  2. If a detached document is genuinely required, use document.implementation().create_html_document("")
  3. Gate DOM access with yew::is_browser() in code shared across targets
  4. Update copied snippets - this is a known weak spot of the old docs example

Example fix

// before
Document::new()
    .expect("global document not set")
    .get_element_by_id("mousemoveme")

// after
gloo::utils::document()
    .get_element_by_id("mousemoveme")
Defensive patterns

Strategy: validation

Validate before calling

// Check the environment before touching DOM bindings in shared code:
if let Some(win) = web_sys::window() {
    let doc = win.document().expect("window without a document");
    // proceed with doc
}

Prevention

When it happens

Trigger: Running the doc snippet verbatim in a browser (new Document() throws immediately); calling DOM bindings from a worker or other environment where no global document exists.

Common situations: Copy-pasting the tutorial into a real app; code shared between browser and worker/server contexts without environment checks.

Related errors


AI-assisted analysis of yewstack/yew@0e4a05472f (2026-08-22). Data as JSON: /api/errors/8bd6b5384b5544a7. Report an issue: GitHub.