Javascript in HTMLPanel

狂风中的少年 提交于 2019-12-10 16:36:50

问题


I want to include Javascript code in a HTMLPanel element, but it is not working. Could you please help me? Thanks in advance.

Scripts/pro.js

alert('hello');

WITH HTMLPANEL DOES NOT WORK (no alert is displayed)

MyPage.java (which is EntryPoint for MyPage.html)

String preHtml="<script type=\"text/javascript\" src=\"Scripts/pro.js\"></script>";
HTMLPanel prePanel=new HTMLPanel(preHtml);
RootPanel.get("scriptContainer").add(prePanel);

MyPage.html

<td align="left" valign="top" id="scriptContainer"></td>

WITHOUT HTMLPANEL DOES WORK (alert is displayed)

MyPage.html

<td align="left" valign="top"><script type="text/javascript" src="Scripts/pro.js"></script></td>

回答1:


I think it should be other way around. The HTMLPanel quoting the javadocs

A panel that contains HTML, and which can attach child widgets to identified elements within that HTML.

should wrap your container not your script. The below example works unless you absolutely have a reason to use HTMLPanel to wrap a script.

HTMLPanel html = new HTMLPanel("<table><tr><td id='scriptContainer'></td></tr></table>");
RootPanel.get().add(html);  
Element script = DOM.createElement("script");
DOM.setElementAttribute(script,"language","JavaScript");
DOM.setElementAttribute(script,"src","Scripts/pro.js");
DOM.appendChild(DOM.getElementById("scriptContainer"),script);


来源:https://stackoverflow.com/questions/7057312/javascript-in-htmlpanel

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