updated no_modules example to show web-sys usage

This commit is contained in:
ibaryshnikov
2018-11-27 14:44:57 +01:00
parent 091eaa66f3
commit 873898e6c0
3 changed files with 34 additions and 19 deletions

View File

@ -1,13 +1,22 @@
extern crate wasm_bindgen;
extern crate web_sys;
use wasm_bindgen::prelude::*;
// Called by our JS entry point to run the example
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
pub fn run() -> Result<(), JsValue> {
// Use `web_sys`'s global `window` function to get a handle on the global
// window object.
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
let body = document.body().expect("document should have a body");
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
// Manufacture the element we're gonna append
let val = document.create_element("p")?;
val.set_inner_html("Hello from Rust!");
body.append_child(&val)?;
Ok(())
}