Giter Club home page Giter Club logo

Comments (8)

timfayz avatar timfayz commented on July 30, 2024

I've found it using js.Global.Get("document").Get("readyState"). But it is always has loading state( Could give an example how can I run eg println("ready!") after DOM is loaded?

from go-js-dom.

dmitshur avatar dmitshur commented on July 30, 2024

What does it print if you do this?

var document = dom.GetWindow().Document().(dom.HTMLDocument)

func main() {
    document.AddEventListener("DOMContentLoaded", false, func(_ dom.Event) {
        println(js.Global.Get("document").Get("readyState"))
    })
}

from go-js-dom.

timfayz avatar timfayz commented on July 30, 2024

The output is interactive.

from go-js-dom.

dmitshur avatar dmitshur commented on July 30, 2024

The first thing what I did is include compiled main.js file into <head> tag. However whatever selector I use (dom.GetWindow().Document().querySelector("...")) - it returned nil.

That's because the body does not yet exist when the script runs in <head>. However, if you delay the execution of your code by calling it from the DOMContentLoaded event listener, then body and its elements should be ready by then, so QuerySelector should work.

from go-js-dom.

timfayz avatar timfayz commented on July 30, 2024

Of course, I know it. But I thought (I don't know why) that the gopherjs compile into the code that start only when dom is ready)) Thank you for explanation and very good example! If readyState property defined in DOM specification and it is implemented in all (almost) browsers - why not to include it to the package itself?)

from go-js-dom.

dmitshur avatar dmitshur commented on July 30, 2024

If readyState property defined in DOM specification and it is implemented in all (almost) browsers - why not to include it to the package itself?)

I don't think there's a reason not to, it's just not done yet (there are a few not yet implemented features, e.g., see dom.go#L950).

I believe that contributions are welcome (@dominikh, correct if wrong), so feel free to make a PR that adds it if you can/want (since there's no CONTRIBUTING.md file that has special contribution guidelines in this repo, making PRs is the default approach). Thanks!

from go-js-dom.

dominikh avatar dominikh commented on July 30, 2024

It's already implemented, as HTMLDocument.ReadyState. HTMLDocument is an interface, ReadyState in turn a method.

An example of accessing it:

dom.GetWindow().Document().(dom.HTMLDocument).ReadyState()

Window.Document returns the Document interface, because not all uses of DOM have to be in the context of HTML (SVG would be another context, for example). If you know that you're working with HTML, you can type assert to the HTMLDocument interface.

For documentation on all the types and methods, see http://godoc.org/honnef.co/go/js/dom.

from go-js-dom.

timfayz avatar timfayz commented on July 30, 2024

Good!) Thanks you)

from go-js-dom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.