On 23/10/06, Michael Geary <[EMAIL PROTECTED]> wrote:
> > I would like to be able to load my jquery plugins dynamically
> > as this would reduce the number of scripts in my document head.
>
> You can easily load any .js file dynamically, for example with code like
> this:
>
>    function addScript( url ) {
>       var script = document.createElement( 'script' );
>       script.type = 'text/javascript';
>       script.charset = 'utf-8';
>       script.src = url;
>       document.getElementsByTagName('head')[0].appendChild( script );
>    };
>
> That's just basic JavaScript code that doesn't require jQuery or anything.
>
> Or, using jQuery and my DOM plugin, you could do this:
>
>    function addScript( url ) {
>       $('head',document).append(
>          $.SCRIPT({ type:'text/javascript', charset:'utf-8', src:url })
>       );
>    };
>
> There are other similar ways of doing this as well.

What about $.getScript("myplugin.js", callback)? With all the code you
want to run in the callback function.

> However, it may or may not be a good idea. Loading your plugin scripts
> dynamically will reduce the number of files loaded in HEAD, but the overall
> load time won't be any better. Loading a number of different scripts will
> take time however you do it.
>
> In many cases, a better approach is to simply concatenate your scripts into
> a single file. You don't increase the overall download size at all this way,
> but you can greatly reduce the *number* of scripts that are loaded.
>
> The Dojo Toolkit folks have a lot of experience with this. Dojo will load
> any number of scripts dynamically, but to gain performance, you create a
> "build" which is simply a single .js file containing all of the Dojo scripts
> you are using. It makes a big improvement in page load time.
>
> -Mike
>
>
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
>

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to