Evaling scripts in a JSNI method

60 weergaven
Naar het eerste ongelezen bericht

Matt Raible

ongelezen,
12 feb 2009, 14:23:0012-02-2009
aan Google Web Toolkit
I'm trying to integrate analytics into my GWT application. To do this,
I'm calling a service that returns a String of HTML that needs to be
parsed and eval'ed. The following seems to work to eval() the contents
of a <script> tag, but it fails (silently) if I try to parse a .js
file (referenced in a "src" attribute). Any ideas why?

Thanks,

Matt


try {
evalJS(new HTML(response).getElement());
} catch (JavaScriptException jse) {
GWT.log("Failed to parse analytics scripts.", jse);
GWT.log("Analytics script contents: " + response,
null);
}


public static native String evalJS(Element e) /*-{
var scripts = e.getElementsByTagName("script");

for (i=0; i < scripts.length; i++) {
// if src, eval it, otherwise eval the body
if (scripts[i].hasAttribute("src")) {
eval(scripts[i].getAttribute("src")); // silently
fails here
} else {
eval(scripts[i].innerHTML); // this works
}
}
}-*/;

jdwyah

ongelezen,
12 feb 2009, 14:45:4812-02-2009
aan Google Web Toolkit
scripts[i].getAttribute("src") is going to be something like
'myscript.js'

you can't just eval that string. Are you trying to fetch the js and
eval? I'd imagine you need to do that explicitly.

-Jeff

Matt Raible

ongelezen,
12 feb 2009, 15:56:5912-02-2009
aan Google Web Toolkit
How do I go about fetching the script and then eval'ing it?

Thanks,

Matt

jdwyah

ongelezen,
12 feb 2009, 18:17:3212-02-2009
aan Google Web Toolkit
js only I guess you could do an Ajax.Request, but in JSNI you don't
have Prototype..

I dunno, I feel like you're descending down the rabbit hole. I wonder
if there's another approach.

-jdwyah

Jason Essington

ongelezen,
12 feb 2009, 18:44:3912-02-2009
aan Google-We...@googlegroups.com
Why would you need prototype? just use RequestBuilder to fetch the
text, and pass the result into your JSNI eval() ...

-jason

Matt Raible

ongelezen,
13 feb 2009, 01:05:4813-02-2009
aan Google Web Toolkit
If I'm in JSNI, I don't have access to the RequestBuilder, do I? If
so, do
you have some example code?

Some options I can think of:

1) Parse the String and look for <script src=""> before calling eval
(). If
"src" is found, grab the script's location and load it using
RequestBuilder.
2) Add a JS Library like jQuery or Prototype and use their XHR API to
load
the script in my JSNI method.

Matt

Matt Raible

ongelezen,
13 feb 2009, 02:11:3413-02-2009
aan Google Web Toolkit
Figured it out:

/**
* Evaluate scripts in an HTML document. 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 {
eval(scripts[i].innerHTML);
}
}
}-*/;
Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten