Initialise External Javascript in Page Fragments

In the last couple of hours I was contemplating around maintainable, performant and non-obstrusive Javascript on a project I am working on.

Following situation:
- Much Inline Javascript
- Many JS and CSS files referenced
- No Frontend Deployment/Build Process in place
- Some Javascript values are set at page generation time by Server Side code
- Webserver is not configured yet correctly for setting expires & compression headers

Problems with this:
- Maintainability and separation of concerns is an issue with lots of inline JS
- Long Pageload because of many HTTP request
- Webserver takes big hit for all the static content loading
- Network is being stressed as well
- Javascript Logic is unobfuscated/not minimised and visible to the world

Tasks:

  • Externalize JS
  • Modularisation of JS Dependency Loading vs. all in one
  • Configuration of HTTP headers in Webserver
  • Identify Scripts / static content for CDN
  • Seamless Deployment/Build process from comfortable JS dev to the production code

The “challenge” was to externalise the inline Javascript.

For modularisation there are several tools to handle the dependency combination of Javascript files. Depending on the amount of Javascript and the usage metrics you can as well just pack all in one. Of course there are many other methods for combination and modularisation and you can as well just run your own method. So this is bascially just a matter of choosing the right tool / method and not that difficult. For compressing / minimisation the YUI compressor seems like state of the art.

Configuration of the correct headers is also just a – well – configuration task of setting meanigful caching control and expires headers and deflate compression for Apache2, so that the documents get compressed over the network. As for deploment and build process sprockets, ant or others are a valiable solution.

What I would like to share here is the solution for externalize Javascript that was initialised by server side code.

The basic idea builds upon this post by dustin diaz. So I am not gonna repeat his content instead if you are not familiar with private, public and privileged Objects in Javascript you might read his post first. As a starting point when combining multiple inline Javascript files we have to take care about namespaces.

//just namespacing
 
function registerNS(ns)
{
 var nsParts = ns.split(".");
 var root = window;
 
 for(var i=0; i

Ok so far so good, now we need to have a way initialise those external Javascript files with dynamic server-side variables:

//initialise a namespaced object
 
var MY = function() {
	var private_var;
	function private_method() {
		// do stuff here
	}
	return {
                init : function(var1) {
                    private_var=var1;
                },
		method_1 : function() {
			alert("method 1");
		},
		method_2 : function() {
			alert(private_var);
		}
	};
}();
 
MY.init("hey you!");
MY.method_1();
MY.method_2();

That worked out as well. But there’s a but. Our javascript will be referenced externally and along with the YUI Performance rules the script include tags will be at the bottom of the page. The initialisation code on the other hand is at the top of every page fragment/widget and might be executed at rendering time. To prevent timing problems the initialisation code therfore needs to configure an object that is already live:

//global js config obj
var runcfg = new Array();
 
//within html
var a = runcfg["details"] = new Array();
a["url"]="http://uebersoftware.com";
 
//external js
var MY = function() {
	var private_var;
        var myconfig = runcfg["details"];
	function private_method() {
		// do stuff here
	}
	return {
                init : function(var1) {
                    private_var=var1;
                },
		method_1 : function() {
			alert(myconfig ["url"] );
		}
	};
}();
 
// usage
MY.method_1();

This method allows us easy externalized the Javascript without much refactoring. The script code is namespaced and is still dynamically configurable.

This entry was posted in javascript, Uncategorized and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.

One Trackback

  1. [...] Opinions and thoughts on Software and Technology. Subscribe S.Mail Skip to content About « Initialise External Javascript in Page Fragments [...]