Loading JS into the footer


#1

I use a cheeky tag for combining scripts into the footer if anyone is interested in commenting.
Drop this into the footer where you want the link to the JS:

<tags:inPlaceJS id="footerJS" lFiles="/js/jquery.qtip.js/js/jquery-textfill.js,/js/easing.min.js,/js/boostrap.min.js,/wbRevolutionarySlider/js/jquery.themepunch.revolution.min.js,/wbRevolutionarySlider/js/jquery.themepunch.tools.min.js">

The Tag (inPlaceJS.cfm):

<cfsetting enablecfoutputonly="true" />
<cfimport taglib="/farcry/core/tags/webskin" prefix="skin" />
<cfparam name="attributes.id" default=""><!--- The id of the library that has been registered with the application --->
<cfparam name="attributes.lFiles" type="string" default="" /> <!---the List of Files--->
<cfif NOT structKeyExists(application.fc.stJSLibraries,attributes.id)> 
<skin:registerJS id="footerJS" 
                        baseHREF=""
                            lFiles="#attributes.lFiles#" />
</cfif>

<cfif structKeyExists(application.fc.stJSLibraries,attributes.id)>
        <cfif NOT structKeyExists(application.fc.stJSLibraries[attributes.id],"sCacheFileName") OR application.fc.stJSLibraries[attributes.id].sCacheFileName EQ "" OR NOT application.fc.lib.cdn.ioFileExists(location="cache",file=application.fc.stJSLibraries[attributes.id].sCacheFileName)>
            <cflock name="#attributes.id#" timeout="10">
                <cfset title = attributes.id & "-" & hash("#application.fc.stJSLibraries[attributes.id].LFULLFILEBASEHREFS#") />
              
                <cfset theJS = "">
                <cfloop index="i" list="#application.fc.stJSLibraries[attributes.id].LFULLFILEBASEHREFS#">
                <cffile action="read" file="#expandPath(i)#" variable="tmpJS" >
                <cfset theJS = theJS & '#Chr(10)# #tmpJS#;'>
                </cfloop>
                <cfset application.fc.stJSLibraries[attributes.id].sCacheFileName = application.fc.lib.cdn.ioWriteFile(location="cache",file="#title#.js",data=compressJsWithJSMin(theJS)) />
            </cflock>
        </cfif>
</cfif>

<cfoutput>
<script type="application/javascript" src="/cache/#application.fc.stJSLibraries[attributes.id].sCacheFileName#"></script>
</cfoutput>

<cffunction name="compressJsWithJSMin" access="public" returnType="string" output="no" hint="takes a javascript string and returns a compressed version, using JSMin">
        <cfargument name="sInput" type="string" required="true" />
        <cfscript>
        var javaLoader= createObject("component", "farcry.core.packages.farcry.javaloader.JavaLoader");
        var jJSMin = "";
        var jarPath= expandPath('/farcry/core/packages/farcry/combine/lib');
        var sOut = arguments.sInput;
        var jarFileArray = arrayNew(1); 
        var jOutputStream = createObject("java","java.io.ByteArrayOutputStream");
        var jStringReader = createObject("java","java.io.StringReader");
        var joInput = jStringReader.init(sOut);
        var joOutput = jOutputStream.init();
        arrayAppend(jarFileArray, jarPath & "/combine.jar");
        arrayAppend(jarFileArray, jarPath & "/yuicompressor-2.4.6.jar");
        javaLoader.init(jarFileArray);
        jJSMin = javaLoader.create("com.magnoliabox.jsmin.JSMin");             
        joJSMin = jJSMin.init(joInput, joOutput);
        joJSMin.jsmin();
        joInput.close();
        sOut = joOutput.toString();
        joOutput.close();
        
        return sOut;
</cfscript>
</cffunction>

<cfsetting enablecfoutputonly="false" />

#2

I’m impressed you untangled the current process so well. You could also use
<core:inHead …> to do custom handling of libraries.

Blair