Which is the difference between $doc.getElementById(“id”) and document.getElementById(“id”) in JSNI

笑着哭i 提交于 2019-12-22 04:42:48

问题


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.



来源:https://stackoverflow.com/questions/7308911/which-is-the-difference-between-doc-getelementbyidid-and-document-getelemen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!