3

I have a library of JS code to load from a folder. Instead of typing the <script src='...'></script> 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 <script> tag are generated.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
Dennis D
  • 1,293
  • 4
  • 17
  • 24

3 Answers3

5

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

Community
  • 1
  • 1
balupton
  • 47,113
  • 32
  • 131
  • 182
  • It's not necessary to set the "type" attribute. – Pointy Aug 28 '10 at 12:47
  • 1
    @Pointy while it will work without it, it is still a required attribute: http://www.w3schools.com/TAGS/tag_script.asp and http://www.google.com.au/search?hl=en&q=%22required+attribute+type+not+specified%22&aq=f&aqi=&aql=&oq=&gs_rfai= – balupton Aug 28 '10 at 12:51
  • 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 Aug 28 '10 at 12:58
  • will this sacrifice loading time? Is using prototype faster? – Dennis D Aug 28 '10 at 13:24
  • @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 Aug 28 '10 at 13:25
  • @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 Aug 28 '10 at 13:27
  • 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 Aug 28 '10 at 14:07
  • 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 Aug 28 '10 at 14:42
  • does it have anything to do with Unobtrusive javascript? – Dennis D Aug 28 '10 at 14:58
  • @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 Aug 28 '10 at 16:46
  • @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 Aug 28 '10 at 16:47
  • @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 Aug 28 '10 at 16:50
1

A simple way to do that:

document.write("<script type='text/javascript' src='b.js'></script>"); 
  • 4
    Using `document.write` is generally strongly deprecated. The same effect can be achieved by adding a ` – Pointy Aug 28 '10 at 12:47
  • Valid, but I like it's simplicity and it's low characters consumption. –  Aug 28 '10 at 12:53
  • 1
    `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 Aug 28 '10 at 13:25
  • 1
    Also, adding a ` – Tim Down Aug 28 '10 at 14:00
0

Try use requireJs, he have very userful functions

Official website

Zerstoren
  • 126
  • 6