Closed zhtanjj closed 1 week ago
The content of the page may have already finished loading before the script runs. You can check if the page has already loaded before adding the listener like this
For example, the content always appears as already fully loaded with this code (on the git version of dioxus web):
use dioxus::prelude::*;
fn Home() -> Element {
const SCRIPT: &str = r#"if (document.readyState === "loading") {
// Loading hasn't finished yet
document.addEventListener("DOMContentLoaded", function() {alert("DOM loaded")});
} else {
// `DOMContentLoaded` has already fired
alert("DOM was already fully loaded");
}"#;
use_effect(|| {
document::eval(SCRIPT);
});
rsx! {
"hello"
}
}
fn main() {
launch(Home)
}
THK
Problem it can't run "window.addEventListener" in script .
Steps To Reproduce 1,Steps
2,Steps
Expected behavior in 1 step: it can run
alert('begin test window.addEventListener DOMContentLoaded');
,but can't runalert(' window.addEventListener DOMContentLoaded');
in 2 step: remove
window.addEventListener('...
, it can runalert(' window.addEventListener DOMContentLoaded');
Screenshots
Environment: