chapter31

development in a land far far away…

at the moment

History is not what happened. History is what was written down.

I’m working on an application which stores a lot of data in the application scope. Part of the data stored is a config CFC that has a method which loads ‘external assets’ (javascript and css files etc) into the <head> of the html document via <cfhtmlhead>. I love that tag :)

My part in this application development is limited to a particular section only, but there was already a lot of external assets present in the header and things were getting very messy. I was looking for a way to simulate <cfinclude> but from within a .js file so I could clean up my output a bit, fortunately there is a way!

I now have a master js file to add to the <head> section of my document:

From within this master.js file I can ‘include’ any js files I want.

master.js

The above code includes myFile1.js and myFile2.js, both of which are located in the relative ‘js/’ directory.

Not only does this clean up my output, but as my config file was stored in application scope every time I wanted to add/remove an asset from my application I need to refresh the scope…annoying. But now I am happy :)


Related Pages

20 Responses to “Including js files from within js files”

  1. hola there! .-)
    Looks interesting. Did you check crossbrowser compatibility?
    Cheers, Kai

    Kai

  2. Hi Kai,

    I’ve tested with success on:

    WinXP
    - Firefox 2.0
    - IE 6
    - IE 7
    - Netscape 8.1.2
    - Opera 9.0
    - Seamonkey 1.0.5

    Mac Tiger 10.4.6
    - Firefox 2.0
    - Safari 2.0.3

    Michael Sharman

  3. That is a pretty nifty technique! I hear you on the clean output. Do you have any idea if this affects the ability for the browser to cache the included Javascript files? I assume it doesn’t as they are still just script tags, but just thought I would throw that out there.

    Excellent tip though!

    Ben Nadel

  4. I use a similar technique and one problem i had -
    the function finishes before the javascript is actually loaded into the browser. so before calling any function in the included js file check if is loaded

    Cheers kris

    Kris John

  5. Hi Kris,

    Interestingly I just ran across this issue when loading the scriptaculous framework. scriptaculous.js by default loads all the effects libraries, but it was throwing a ‘not defined’ error because those other libraries weren’t loaded yet!

    Oh well, it’s good to know the limitations of this technique so you know when you can and can’t use it.

    Thanks again

    Michael Sharman

  6. I use a similar solution, without the defer attribute, as i want the javascript to be parsed before the page ends. Have you noticed any benefit to defering?

    Michael, kris: I too have the problem where file B requires a function in file A, but file B is parsed before file A is completely loaded in memory. In my case obect in file B directly inherit from objects in file A, so i need a work around for this. Do either of you have a good work around for this?

    Also, you can always try the document.write() if the dom way fails. This will support a couple more of the older browsers.

    matt snider

  7. Hello! I have some scripts files ” *.js “, but When I drag them to firefox, thei dont run, thei are listed!!!

    How can I install this files that I have on disk?

    Ricardo Afonso

  8. What if I had a javascript file that was imbedded into deeper folders then the includes I wanted to access? So what I’m saying is there a way to include a .js file that is x folders up in the folder hierarchy?

    Your example looks like its relative as in in the samve folder as the .js itself

    Steve

  9. Hi Steve,

    Yes you can either use a Web Mapping (virtual directory) or use the ../ style notation.

    eg if my directory path was the following:

    C:\mps\dev\js\includes\js\master.js

    I could use this to reference a file in C:\mps\dev:

    include(’../../../myJavascript.js’);

    or I could use a web mapping (myWebMapping points to C:\mps\dev):

    include(’/myWebMapping/myJavascript.js’);

    Michael

    Michael Sharman

  10. not work on ie 6 ????

    wizzzy

  11. Does not work if your page is a frameset ( doing lots of fancy redirection calculations ) and has no head element!

    Garry Lindsay

  12. @Garry - Hmm that would be quite a rare scenario I would imagine.

    Still, good to know. Thanks for the comment.

    Michael Sharman

  13. very nice tip ! was just looking for a solution for this problem .

    Tom

  14. Great!, it helped me out… i was looking for something like this… Thanks.. keep it up

    Alyas

  15. Nice tip, very interesting…

    Happy

  16. Good solution, although, out of interest, what would happen if you included two js files that themselves required the same (third) common js file included?

    As far as I can see, you would have to name the ‘include’ function differently in each js file, and you would end up with the script included twice.

    James wiseman

  17. Hi there i found your article looking for exactly this, your script is really nice but i had a problem when i tried to use it; the question is: when are you loading the functions include(’whatever.js’)

    If i enter only the script as you show it, didn’t happens anything, i tried to put them into another function and load it in the onload event of the body and didn’t work again

    how do you load them??
    Cheers.

    Juwe

  18. ejem, hehe sorry man, my problem wasn’t the loading of the functions, it was the relative location of the js files ;)

    i liked your code is a great and simple solution, as we said here in MX: está de guevos :D

    Juwe

  19. Hi Juwe,

    Glad it worked out for you! Hello from sunny Australia :)

    Michael Sharman

  20. yeah thanks, i added your blog to my feed reader. Greetings from Mexico City. :P

    Cheers.

    Juwe

Leave a Reply