Hi,

> 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.

It wouldn't, but you couldn't have known that as you couldn't see the
wiki article. :-)

> I couldn't check Proto-
> Scripty wiki (seems to be down...) to learn about dynamic script
> loading but this put me in the way.

Wow, I've never known wikidot (where the wiki is hosted) to be down.
I suppose it must happen. :-)  It's working right now as I write this
(I followed the link in my earlier note to make sure it wasn't a
typo).

> Nevertheless, it seems that Firefox does not load external script
> files unless they are inserted in the HTML head element (can someone
> confirm that?).

It doesn't process them if you assign them as strings via innerHTML,
which is what your modified update would do (Element#update uses
innerHTML behind the scenes).  It does process them correctly if you
insert them using DOM methods as your code does, even if you don't put
them in the head.  But putting them in the head is probably the most
appropriate thing to do anyway, and indeed is exactly what the
unofficial wiki article does. :-)

> In fact, I choose to use Ajax.Responders to parse response text from
> output and dynamically load any external source files...

Ah, interesting approach.  I think I'd prefer to leave Prototype
unmodified and build my own updater-like-thing via Ajax.Request, but
obviously it's up to you.

> Nevertheless, I think that inline scripts may be executed before
> external resource has been completely interpreted, which may lead to
> invalid references.

Very likely.  The inline scripts will get evaluated very, very soon
after the content is updated (Element#update does it via Function#defer
[1]), whereas the external scripts may still be downloading at that
point.  A really robust solution would probably walk through the
script tags (inline and external) in the order in which they appear
and load/execute them sequentially.  I think if you were going to do
that, you'd have to retrieve the script files directly (via XHR calls)
rather than inserting script tags, because otherwise I'm not sure how
you'd be able to know that the file had finished loading.  XHR stuff
*should* use the browser cache, and I understand it mostly does
although there are some revalidation issues reported in the XHR
Wikipedia article (mostly indicating that stale content may get
reused, as opposed to getting re-retrieved too often).[2]

[1] http://prototypejs.org/api/function/defer
[2] http://en.wikipedia.org/wiki/XMLHttpRequest

Have fun,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On May 6, 2:45 pm, almeidap <almei...@gmail.com> wrote:
> 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 prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to