forked from rustwasm/wasm-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rustwasm#1044 from ibaryshnikov/dom-example-withou…
…t-bundlers Added web-sys dom example withot npm and webpack
- Loading branch information
Showing
3 changed files
with
34 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
} |