Skip to content

Instantly share code, notes, and snippets.

@yoshuawuyts
Created October 4, 2018 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshuawuyts/0e1e6e230b3f2c0d69ade5b6a5c71d0d to your computer and use it in GitHub Desktop.
Save yoshuawuyts/0e1e6e230b3f2c0d69ade5b6a5c71d0d to your computer and use it in GitHub Desktop.
use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast;
use web_sys::{Document, EventTarget};
let document = Document::new().map_err(|_| ErrorKind::Document)?;
let target: EventTarget = document.into();
let cb = Closure::wrap(Box::new(move || {
println!("hello world from WASM");
}) as Box<Fn()>);
let event_name = "DOMContentLoaded";
target
.add_event_listener_with_callback(event_name, cb.as_ref().unchecked_ref())
.map_err(|_| ErrorKind::Document)?;
use dom::{self, Document, Events};
let document = Document::new()?;
let event = Events::DomContentLoaded;
document.add_event_listener(event, |_| {
println!("DOM Loaded!");
})?;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment