Optimize your mojoPortal Skin Scripts

Almost every single skin we develop uses a couple JavaScript scripts, but even the addition of one script can slow the speed of a website. We use a fairly simple process to combine as many of our scripts into one file and then using a neat mojoPortal control, we add that script the combined site script. The combined site script contains most of the scripts mojoPortal uses for things like site cookies, ASP.NET CSS friendly adapters, etc… This process will result in fewer files being delivered to the browser, thus improving performance (somewhat).

Create the Skin Script and call it from the layout.master

  1. Create a new js file in your skin directory, I usually name it skinscript.js.
  2. Add your javascript to the new file. Don't use <script> tags, you don't need them in javascript files.
  3. Add the following to your layout.master. I usually add this right after the <asp:ScriptManager control.
    <portal:SkinFolderScript ID="sfs1" runat="server" ScriptFileName="skinscript.js" AddToCombinedScript="true" />

Copy all the scripts from your layout.master to the Skin Script

This is the part that can be a little bit tricky because not all scripts are going to work properly when combined with other scripts. If you copy the scripts to your new file one at a time and test the result, you'll find those pesky ones that will not cooperate. Just leave them in the layout.master, or if you're feeling froggy, rewrite them so they can be combined.

A note about SuperFish

Most of our skins use a SuperFish menu but the scripts for the SuperFishmenu come in two parts. One is the static script that is in the ClientScript directory which contains all of the SuperFish magic. The other is an initialization script that is loaded in the layout.master of the skin. Typically, these are loaded by adding the layout.master a <portal:SiteScript control to load the main SuperFish script and then a <script> element with a bit of jQuery to initialize the SuperFish menu. We decided to use the jQuery .getScript() method to load the main script from our Skin Script. Using this method allows us to initialize the SuperFish menu only if the main script is loaded properly.

A Sample Skin Script

The script below is what we use for starting our skin scripts. It includes the JavaScript for the Administration Toolbar Menu, SuperFish Menu and a neat bit of jQuery that will give the "Add File" button in the Shared Files module a hover state. The script is written out long-form so you can easily see what it does. I suggest that you minify the script before using it in production.

//  ========== 
//  = This file stores all of the scripts that would normally be placed inside the layout.master
//  = Use the following syntax for referencing this script from the layout.master.
//  = 
//  ==========

/*Standard JavaScript */

function HideMenuToolbar() {
	$("#toolbar").fadeOut();
	$("#toolbarbut").fadeIn("slow");
}

function ShowMenuToolbar() {
	$("#toolbar").fadeIn();
	$("#toolbarbut").fadeOut("slow");
}


/* jQuery Scripts */
$(document).ready(function() {

	/* Admin Toolbar */
	$("span.downarr a").click(function() {
		HideMenuToolbar();
		Set_Cookie('openstate', 'closed')
	});
	$("span.showbar a").click(function() {
		ShowMenuToolbar();
		Set_Cookie('openstate', 'open')
	});
	$("span.downarr a, span.showbar a").click(function() {
		return false;
	});
	var openState = Get_Cookie('openstate');
	if (openState != null) {
		if (openState == 'closed') {
			HideMenuToolbar();
		}
		if (openState == 'open') {
			ShowMenuToolbar();
		}
	}

	/* Superfish Menu */
	/* Get Superfish mojoPortal Script */
	$.getScript("/ClientScript/jqmojo/mojosuperfish.js", function() {
		$("ul.sf-menu").supersubs({
			minWidth: 1,
			maxWidth: 27,
			extraWidth: .2
		}).superfish({
			pathClass: 'current',
			pathLevels: 0,
			delay: 500,
			animation: {
				opacity: 'show',
				height: 'show'
			},
			speed: 200,
			dropShadows: false,
			autoArrows: true
		});
	});
	/* This will make the Shared Files "Add File" button have a hover state */
	$("div.uploadpanel > div > input + div + div").hover(
		function() {
			$("div.uploadpanel div .jqbutton").addClass("ui-state-hover")
		},
		function() {
			$("div.uploadpanel div .jqbutton").removeClass("ui-state-hover")
		});
});

Further Reading

If you're serious about optimizing the speed of your mojoPortal site, I highly suggest you read the "Improving Your YSlow or Page Speed Score" article in the mojoPortal Documentation.

Happy mojo-ing!

View User Profile for Joe Davis Joe is the Founder and Managing Director of i7MEDIA. His passion is finding creative solutions to complex problems. He is married to Devyn and has three kids; Elijah, Ruth and Hannah. He is a Christian and life-long Boy Scout. When he is not at work, he's working his small homestead farm, or volunteering with the Boy Scouts or his church.

Comments