I'm working in a native function inside a GWT application and I've tried this two methods: document.getElementById("id") returns null but $doc.getElementById() returns a valid element. Which is the difference (conceptually) between this methods? Thanks in advance.
问题:
回答1:
The code of your GWT app runs in a (hidden) iframe, so document
references that iframe's document (and window
the iframe's browsing context). GWT thus initializes the variables $doc
and $wnd
to let you easily reference the document and browsing context (window) of the "host page" that loads the GWT app.
Note that linkers decide how the compiled code is loaded, the default one (std
) and the newer xsiframe
use iframes, whereas the deprecated xs
loads your code in the same browsing context (so $doc == document
and $wnd == window
)
回答2:
From the GWT JSNI page:
Note that the code did not reference the JavaScript window object directly inside the method. When accessing the browser's window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. Your compiled script runs in a nested frame, and $wnd and $doc are automatically initialized to correctly refer to the host page's window and document.