Which is the difference between $doc.getElementById("id") and document.getElementById("id") in JSNI
Asked Answered
S

2

7

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.

Selenodont answered 5/9, 2011 at 13:38 Comment(0)
P
7

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)

Privative answered 5/9, 2011 at 14:31 Comment(0)
R
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.

Remount answered 5/9, 2011 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.