Thanks for your quick and complete answer! I've followed your hints
and I think that I've reached an elegant (but maybe not performant)
solution.

> String#evalScripts uses String#extractScripts[4], which uses this
> regex repeatedly to build an array of the contents of script tags:
> '<script[^>]*>([\\S\\s]*?)<\/script>' ...

Right! I adapted Prototype's regex to leave script tags that reference
an external source file, simply by redefining the Prototype's
ScriptFragment property (argh, it took me about 1 hour to find that
crazy regex):
Prototype.ScriptFragment = '<script(?!.*src)[^>]*>([\\S\\s]*?)<\/
script>';

Nevertheless, it seems that Firefox does not load external script
files unless they are inserted in the HTML head element (can someone
confirm that?). That means that even if the script element was
inserted into the DOM after update, script contents were never
interpreted.

> The second approach, which may be simpler, only works if you're in
> control of what comes back (e.g., you're not calling some third-party
> code you don't have control over).  If you are in control of it, you
> could change it to use inline script to load external scripts, rather
> than script tags.

Yes, I could control this as output mostly comes from source files
that have been automatically generated (MDA approach) but I think this
solution would break browser file cache. I couldn't check Proto-
Scripty wiki (seems to be down...) to learn about dynamic script
loading but this put me in the way.

In fact, I choose to use Ajax.Responders to parse response text from
output and dynamically load any external source files by appending a
script element in the HTML head (inspired by [1], feel free to reveal
problems or to suggest optimizations):

Ajax.Responders.register({
  onComplete: function(responder) {
    var reponseText = responder.transport.responseText;
        reponseText.scan(/<script[^>]*src="(.*)"[^>]*>/, function(match){
                ActionUtils.loadScript(match[1]);
        });
  }
});

ActionUtils = {

        ...

        Resources : [],

        loadScript : function(resource, onload) {
                if(!this.Resources.include(resource)){
                        var script = new Element('script', {
                                'type' : 'text/javascript',
                                'src' : resource
                        });

                        script.onload = onload | this.emptyFunction;
                        this.getHtmlHead().insert(script);
                        this.Resources.push(resource);
                }
        }
}

(Prototype's ScriptFragment property has not been changed in this
scenario!)

In this way, developpers don't bother how to include external JS
files, the asynchronous circuit does all the job for them.
Nevertheless, I think that inline scripts may be executed before
external resource has been completely interpreted, which may lead to
invalid references.

This has only been tested on Firefox, so let me know if it works with
other main browsers.

[1] http://www.phpied.com/javascript-include-ready-onload/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to