Webru
Frequently used javascript functions for Rust
and WebAssembly
webru
is a binding for web-sys
This crate assumes that you will only use this crate inside browser. Not any other javascript runtime such as Node.js
Usage
Using some global objects and functions
use ;
use ;
// get the body
let body: HtmlElement = body;
// show an alert
alert;
// Get the name from the user using "prompt" method
let name: = prompt;
// creating a <h1> tag using javascript `document.createElement()` method
// This function will return the newly created element/tag
let h1: Element = create_element;
// adding some attributes to <h1> tag
h1.set_id; // html `id` property
h1.set_class_name; // html `class` property`
match name
// pushing the <h1> tag into the DOM
body.append_child.unwrap;
Using setTimeout
and setInterval
use Cell;
use console_log;
// Alternatively, you can use `Timeout` and `Interval` structs instead of `set_timeout` and `set_interval`
Using selectors
use ;
use ;
let class_name = "active-element";
// creating some elements with the class name "active-element" adding it to the DOM
// selecting all elements with the class name "active-element"
// selecting the element with the id "heading-3"
Using javascript callback
use ;
use Cell;
use JsCast;
use HtmlElement;
use console_log;
let count = new;
// "onclick" event handler for the <button> tag
let onclick = callback;
// get the body object
let body: HtmlElement = body;
// createing a <button> tag
let button: HtmlElement = create_element
.
.unwrap
.clone;
// adding some attributes to <button> tag
button.set_inner_html; // innerHTML property
button.set_onclick; // onclick event
onclick.forget;
// pushing the <button> tag into the DOM
body.append_child.unwrap;
Using media query
use media_query;
use console_log;
media_query;
Warning
This project is not well tested. So make sure to test every functions from this crate you used in your project before going to production.
Contributions
Your valuable PRs and Issues are welcome. Note that all the contribution submitted by you, shall be licensed as MIT or APACHE 2.0 at your choice, without any additional terms or conditions.