On Jan 27, 2008 10:02 PM, Justin Perkins <[EMAIL PROTECTED]> wrote:
> It's actually a very nice way to include external scripts, since the
> loading of a script (with src) has an effect on page load times, which
> impacts tasks that run on page load and also visible to the user by
> way of the browser's progress indicator.
>
> You can create script nodes on the fly, with normal DOM-node creating
> code and it works well in Firefox and IE6/7. I have not tested other
> browsers.
>
> This is the basic idea: http://pastie.textmate.org/144194
great example, thanks.
also, like i mentioned, scriptaculous does essentially the same thing in
scriptaculous.js, the main difference in the scriptaculous code is that it
has some runtime checks for prototype and it only tries to load
scriptaculous
modules.
require: function(libraryName) {
// inserting via DOM fails in Safari 2.0, so brute force approach
document.write('<script type="text/javascript"
src="'+libraryName+'"><\/script>');
}
/// later in the code..
$A(document.getElementsByTagName("script")).findAll( function(s) {
return (s.src && s.src.match(/scriptaculous\.js(\?.*)?$/))
}).each( function(s) {
var path = s.src.replace(/scriptaculous\.js(\?.*)?$/,'');
var includes = s.src.match(/\?.*load=([a-z,]*)/);
(includes ? includes[1] :
'builder,effects,dragdrop,controls,slider,sound').split(',').each(
function(include) { Scriptaculous.require(path+include+'.js') });
});
Note that the first thing that code does is look for a script include
> already on the page that has included the same script, and if it is
> found, then it is removed. This may be overkill, but is done to
> prevent caching problems. If the same function that includes the
> script is called multiple times, and you're expecting the script to be
> refreshed, then you may want to append some type of no-cache parameter
> (a time stamp) onto the URL, such as:
>
> // do this just before the call to setAttribute('src', src)
> src = ( src.match( /\?/ ) ? src + '&' : src + '?' ) + ( nocache ?
> 'nocache=' + new Date().getTime() + '&' : '' );
>
> This little snippet has worked out very well for me, especially when
> requesting external scripts that may take a while to load.
im certainly interested in loading scripts dynamically. it seems to me that
scripts that would be handy after a page has loaded would be best loaded in
this manor, however, scripts that need to run when the page first loads
should
be included in <script> tags by the html rendered on the server. unless im
still
missing something..
for example, im working on an application where i have a script that
produces
specialized buttons. each button has several css classes that can be
associated
with it depending upon the mouse position in relation to it (mouesover,
mouesout,
mousedown), and it also allows client code to attach a click handler to each
button.
this class is stored in its own file on the server and i load it in a
<script> tag on
pages that need it. are you saying i would be better off to load this
script dynamically?
-nathan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---