If possible try loading large scripts asynchronously
Enable GZIP compression on all your static content, e.g. CSS and JavaScript files
Set far future expire headers on your static content
Microsoft Ajax Minifier
I have searched for a tool that could help me implement many of the optimization guidelines and found a few worth taking a closer look at, one of these is the Google Closure Compiler. It has loads of features including file combining, minification, “compiling” your JavaScript into better JavaScript, as well as an API for loading your scripts asynchronously.
However, being a pragmatic .NET developer, I chose the most easily accessible tool available for me, which is Microsoft’s Ajax Minifier. It’s a free tool available on Codeplex and documentation is provided on the www.asp.net site.
Ajax Minifier comes as a command-line application and after you have installed it you can use it through it’s own command prompt.
Minifying JavaScript
Minifying a single JavaScript file is pretty straight forward. Simply type in the command prompt:
ajaxmin inputfile.js –out outputfile.js
then add –clobber if you wish to override the outputfile.js if it exists, as in:
ajaxmin inputfile.js –out outputfile.js –clobber
Minifying CSS
Minifying a single CSS file is equally simplistic as follows:
As before, add –clobber to override any existing output file.
Combining files
Unfortunately there are no documentation on the asp.net site about how to combine multiple files into one, however, the ajaxmin command itself provides a hint given the /? parameter:
As an alternative to the input file parameter you can use the –xml parameter and supplying it with the path to an xml file of the following format:
xmlversion="1.0"encoding="utf-8"?>
<root>
<outputpath="outputfile.js">
<inputpath="inputfile1.js"/>
<inputpath="inputfile2.js"/>
-… ->
output>
root>
The Ajax Minifier will combine all of your input files into one, minify it, and produce the result in the output file. Using this approach you can also control in what order the files are loaded into the page and sort out any dependency issues, just as you would, had you listed includes in the regular html file (or .master or whatever).
The Ajax Minifier does not seem to have the smarts to deduce that this is JavaScript files so you need to supply it with a –js parameter like so:
ajaxmin –js –xml jsxmlfile.xml –clobber
I haven’t found a way to combine JavaScript and CSS in the same xml file so you need to make a separate file for you CSS files with the same format as above and instead of the –js parameter, use the –css dito:
A colleague of mine asked me how to apply a stylesheet to a web page dynamically using jQuery and I had never done such a thing but my first thought was that is must be pretty simple. I've spent a lot of time thinking of so many things other than web et. al. so it was nice to delve into some of that stuff again. Check out the live demo
As we know stylesheets are defined in the head section of an html file like this
Now, say that we want to apply another stylesheet dynamically after the fact, so to speak, triggered by some event. This could be a button click or some other arbitrary event that is triggered. So, what we want to do is simply insert a new element into the head section of the page DOM. This can be done in a couple of lines of jQuery:
$(document).ready(function () {
$("a").click(function () {
$('head').append('');
});
});
The key is at what time we add the link to the style sheet. The first line in the code above repeated here asserts that the DOM is ready for manipulation.
$(document).ready(function () {
//...
});
The second part repeated here adds a click event to all hyperlinks in the page.
$("a").click(function() { //...
And the very task is performed by the last piece of code where the head element is appended with a new link element.