wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

set_onload isnt working as expected, am I missing anything?

Open haiderlikesrust opened this issue 3 years ago • 1 comments

Summary

I'm using set_onload on the document object and for some reason set_onclick works but set_onload doesn't this is not a web api issue but can a be a bug, I have tried different browsers but got the same behavior.

Additional Details

pub fn start(self) {
        let _window = window().unwrap();
        web_sys::console::log_1(&"Hello World 1".into());

        let c: Closure<dyn FnMut(Event)> = Closure::new(move |e: Event| {
            let components = self.components.clone();
            let route = _window.location().href().unwrap();
            for component in components {
                if route == component.route() {
                    let doc = _window
                        .document()
                        .unwrap()
                        .query_selector("#app")
                        .unwrap()
                        .unwrap()
                        .set_inner_html(&component.app(self.clone().state).html_str);
                }
            }
        });

        window()
            .unwrap()
            .document()
            .unwrap()
            .set_onload(Some(c.as_ref().unchecked_ref()));
        c.forget();
    }

Let me know if I'm missing something but I don't think so I'm

haiderlikesrust avatar Jul 26 '22 19:07 haiderlikesrust

Probably, the onload event already fired when you call set_onload. That's because the wasm_bindgen JS code loads the WASM module asynchronously. You can write your own JS code to fetch the WASM module synchronously and then use initSync instead of init, but why? Just execute the code inside the closure directly instead of attaching it to onload.

lukaslihotzki avatar Aug 04 '22 00:08 lukaslihotzki