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);
            }
        }
    }-*/;

On Feb 12, 11:05 pm, Matt Raible <mrai...@gmail.com> wrote:
> 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
>
> On Feb 12, 3:44 pm, Jason Essington <jason.essing...@gmail.com> wrote:
>
> > Why would you need prototype? just use RequestBuilder to fetch the  
> > text, and pass the result into your JSNI eval() ...
>
> > -jason
>
> > On Feb 12, 2009, at 4:17 PM, jdwyah wrote:
>
> > > 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
>
> > > On Feb 12, 3:56 pm, Matt Raible <mrai...@gmail.com> wrote:
> > >> How do I go about fetching the script and then eval'ing it?
>
> > >> Thanks,
>
> > >> Matt
>
> > >> On Feb 12, 11:45 am, jdwyah <jdw...@gmail.com> wrote:
>
> > >>> 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
>
> > >>> On Feb 12, 2:23 pm, Matt Raible <mrai...@gmail.com> wrote:
>
> > >>>> 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
> > >>>>             }
> > >>>>         }
> > >>>>     }-*/;
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to