tags:

views:

57

answers:

2

Dear experts,

I have a library of JS code to load from a folder. Instead of typing the lines one by one in the tag of the HTML document, is there a way of just link one Javascript file which organizes and automatically load other javascript files.

I know the Dojotoolkit is using this technique where only one JS file is loaded onto the client's computer, and once the code has been requested in the browser, 20 other JS code each with tag are generated.

Thanks in advance.

+1  A: 

A simple way to do that:

document.write("<script type='text/javascript' src='b.js'></script>"); 
Pierre 303
Using `document.write` is generally strongly deprecated. The same effect can be achieved by adding a `<script>` tag to the document.
Pointy
Valid, but I like it's simplicity and it's low characters consumption.
Pierre 303
`document.write` isn't deprecated. It's in the HTML 5 spec for a start: http://www.w3.org/TR/html5/embedded-content-0.html#dom-document-write
Tim Down
Also, adding a `<script>` element to the document does not have the same effect: using `document.write` is synchronous (as is including a `<script>` element in HTML) while adding a `<script>` element using DOM methods is asynchronous. The synchronous approach has advantages, for example being guaranteed to have loaded before scripts that appear after it in the document.
Tim Down
+4  A: 

This is the code you need:

    // Create
    var bodyEl = document.body;
    var scriptEl = document.createElement('script');
    scriptEl.type = 'text/javascript';
    scriptEl.src = url;
    bodyEl.appendChild(scriptEl);

Put that into a function, have an array of all the javascript files, and call that function for each file.

Benefits of using the DOM is that document.write doesn't work in some funny instances. More about this here: http://stackoverflow.com/questions/367214/document-write-vs-inserting-dom-nodes-preserve-form-information

Code taken from the open source project jQuery Sparkle: http://github.com/balupton/jquery-sparkle/blob/master/scripts/resources/jquery.appendscriptstyle.js#L103

balupton
It's not necessary to set the "type" attribute.
Pointy
balupton
Most contemporary Javascript experts (Doug Crockford in particular) encourage omission of the attribute, and it's explicitly not needed in HTML5. Besides that, the HTML spec is irrelevant when you're building DOM nodes.
Pointy
will this sacrifice loading time?Is using prototype faster?
Dennis D
@Pointy: I'd love to read what Doug had to say on that topic as I also always include the `type` attribute for completeness.
Josh
@Dennis: Loading multiple JavaScript files will be slower than loading a single large file. For the fastest load time, combine all javascripts into one using something like [Minify](http://code.google.com/p/minify/)
Josh
Actually @Josh that's not necessarily true, counterintuitive as that may be. The latency of getting an HTTP connection can make it a little slower to have one big file than to have two less-big files. However one file is definitely better than 10! Oh, and you can read this from Crockford: http://javascript.crockford.com/code.html
Pointy
Hi, i just implemented this thing into my application.I am having a little bit of a problem, I cannot make the src path relative. WHen I do it with ../something.js it actually shows up as it is literally.Is there a way to fix this?Btw, I am using Ruby on Rails
Dennis D
does it have anything to do with Unobtrusive javascript?
Dennis D
@Dennis D. For the Crockford opinion:http://javascript.crockford.com/script.html"This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do."For a counter argument:http://weblogs.asp.net/bleroy/archive/2007/09/17/should-i-use-a-type-attribute-on-script-tags.aspx
balupton
@Dennis D, for the relative path... having it show up as "../" in the actual script tags URL is fine and would still work as you are intending...?
balupton
@Dennis D, for unobtrusive javascript. Yes. Unobtrusive javascript is all about having as little javascript inside our page, and having our page dependent on javascript as little as possible (if any). By having your javascript load in the rest, it can be considered unobtrusive in some circumstances (in terms of a remote script loading in more remote scripts) but say for including a few scripts in your header there is no benefit loading them in through javascript compared to html.
balupton