Which is the difference between $doc.getElementById("id") and document.getElementById("id") in JSNI
I'm working in a native function inside a GWT app开发者_如何学JAVAlication 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.
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
)
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.
精彩评论