问题
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