DioxusLabs / dioxus

Fullstack app framework for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
21.66k stars 834 forks source link

have a bug in script {}? #3238

Closed zhtanjj closed 1 week ago

zhtanjj commented 1 week ago

Problem it can't run "window.addEventListener" in script .

Steps To Reproduce 1,Steps

       script{"
        alert('begin test window.addEventListener DOMContentLoaded');
        window.addEventListener('DOMContentLoaded', function () {{
            alert(' window.addEventListener DOMContentLoaded');
        }});
       "}

2,Steps

       script{"
         alert('begin test window.addEventListener DOMContentLoaded');        
         alert(' window.addEventListener DOMContentLoaded');        
       "}

Expected behavior in 1 step: it can run alert('begin test window.addEventListener DOMContentLoaded'); ,but can't run alert(' window.addEventListener DOMContentLoaded');

in 2 step: remove window.addEventListener('..., it can run alert(' window.addEventListener DOMContentLoaded');

Screenshots

Environment:

ealmloff commented 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)
}
zhtanjj commented 1 week ago

THK