DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
How To Parse And Evaluate JavaScript In GWT
If you have a service that returns JavaScript (e.g. for Google Analytics), the following code should do the trick. You can call it using something like:
evalScripts(new HTML(response.getText()).getElement())
/**
* Evaluate scripts in an HTML string. Will eval both <script src=""></script>
* and <script>javascript here</scripts>.
*
* @param element a new HTML(text).getElement()
*/
public static native void evalScripts(Element element) /*-{
var scripts = element.getElementsByTagName("script");
for (i=0; i < scripts.length; i++) {
// if src, eval it, otherwise eval the body
if (scripts[i].hasAttribute("src")) {
var src = scripts[i].getAttribute("src");
var script = $doc.createElement('script');
script.setAttribute("src", src);
$doc.getElementsByTagName('body')[0].appendChild(script);
} else {
$wnd.eval(scripts[i].innerHTML);
}
}
}-*/;





