Peter Michaux wrote:
> FOn 12/13/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >
> > Peter,
> >
> > I tried your solution ... and it doesn't work ...
>
> This example works for me in Firefox 1.5, Firefox 2, Safari 2, Opera 9 and IE6
>
> <http://peter.michaux.ca/temp/loader/page.html>
>
> It looks like there is a new loader script in the works that uses the
> same DOM decent idea

getElementsByTagName only fails in the above scenario if a second load
script is used with a different path to the first one (hence you need
an updated path) *and* it is in the head element.  Below is a load
script that works fine as long as the second and subsequent loads are
done from the body.  It can easily be extended to include more
features, I just wanted to show you can avoid a recursive node walk
(which must grate on anyone looking for a clean design).

For convenience, it's probably best to put all the load scripts in the
body if this method is used.

If you use DOM methods to add the scripts, there is no issue at all
with either multiple calls to getElementsByTagName or just using the
(live) collection returned by the first call.  I understand
document.write() is used because old versions of Safari don't properly
add scripts via DOM - I'm not sure that's an issue with scriptaculous
since large slabs of it don't work in Safari 1.x anyway.

It seems Firefox doesn't update the DOM for elements written to the
head using document.write() until after the head is closed, but it
seems to update the body as it goes - more testing is required, I
focused on Firefox 2.0 and checked in IE 6.

var loadJS = (function()
{
  var gotScripts, path, scriptList;
  var lastScript = function(){
    return gotScripts[gotScripts.length-1];
  }

  return {
    load : function(obj)
    {
      scriptList = obj || [];
      gotScripts = document.getElementsByTagName('script');
      path = lastScript().src.replace(/[^\/]+\.js.*$/,'');

      for (var i=0, len=scriptList.length; i<len; i++){
        document.write('<script type="text/javascript" src="'
          + path + '/' + scriptList[i]
          + '"><\/script>');
      }
    }
  }
})();


Load scripts consist of:

loadJS.load( [
    'foo.js',
    'bar.js'
  ]);


HTML looks like:

 <head>
  <title>Project</title>
  <script type="text/javascript">
   document.write('<script type="text/javascript"
src="x.js"><\/script>');
  </script>
  <script type="text/javascript" src="scripts/load.js"></script>
  <script type="text/javascript" src="scripts1/load1.js"></script>
 </head>
 <body>
  <script type="text/javascript" src="scripts2/load2.js"></script>
  <script type="text/javascript" src="scripts3/load3.js"></script>
  ...
 </body>


-- 
Fred


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Ruby on Rails: Spinoffs" 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/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to