Dynamically inserting css stylesheets into the DOM

If you need to insert stylesheets client side with javascript, see the snippet below. Uses jquery to insert the css link tag, but that could be changed fairly easily.

 var cssUrls = ["jquery-ui.css","override.css"] ;
                for (var i in cssUrls){
                   var url = "http://www.domain.com/css/" + cssUrls[i];
                if (document.createStyleSheet){
                        document.createStyleSheet(url);
                    }else{
                        $('<link rel="stylesheet" type="text/css" href="' + url + '" />').appendTo('head'); 
                    }
                }

Firebug lite easily in IE7

Firebug is a great way to debug during web page development. Although it is not available natively in IE it can be easily added by pasting in the code below in the address bar of the page you want to use it.

javascript:var firebug=document.createElement('script');firebug.setAttribute('src','http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js');document.body.appendChild(firebug);(function(){if(window.firebug.version){firebug.init();}else{setTimeout(arguments.callee);}})();void(firebug);

LINQPad – great tool for testing LINQ

If you’re dealing with LINQ in C# .Net there is a great tool called Linqpad that allows LINQ queries to be run from a GUI against your SQL DB.  The tool is free, but requires a license for intellisense support.

http://www.linqpad.net/

Add leading zeros to a number in Javascript

Needed a quick way to add leading zeros to numbers for a js countdown script. Not hard to do, now I can look at this post for the code when I need it again.

function addLeadingZeros(n, totalDigits)
	{
	    if(!totalDigits)totalDigits = 2;
	    n = String(n);
	    while (n.length < totalDigits)
		n = "0"+n;
	    return n;
	}
 
var num = 5;
 
num = addLeadingZeros(num, 3);

Remove Crap from WordPress dashboard

Wordpress Admin Screenshot

WordPress is great software. I love the UI. I don’t like the extra clutter in the dashboard and neither do some of my clients. Getting rid of it (or adding to it) is fairly easy. See http://codex.wordpress.org/Dashboard_Widgets_API for all that you can do.

If you just need to quickly trim down the dashboard use the code below in your themes functions.php file.

/************************
 * Control over widgets in admin dashboard
 ************************/
add_action('wp_dashboard_setup', '_remove_dashboard_widgets' );
 
function _remove_dashboard_widgets() {
    global $wp_meta_boxes;
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
}

Time Sheet, a nice Windows app for time tracking

Time Sheet Screenshot

Found a great time tracking application for windows : Time Sheet .

You can train it to associate an application/document title with a project and track the time entered.

From the website:
"TimeSheet is a powerful automated time & task recording tool for professionals.
It will improve your accountability by increasing the billable hours: let the tool register what you are doing and for how long."

Trimming whitespace from a field in MYSQL

Sometimes whitespace in user entered data can be annoying. Even more annoying if it is imported data, or better yet your fault for not trimming the input. Anyway, its a simple matter to trim a field in MYSQL – see the query below , preserved here for posterity.

UPDATE tablename
SET fieldName = TRIM(fieldName)

Vimeo Embeds on the iPhone

Vimeo does not (at the time of this writing) have an iPhone/Pad version of their embed code. After seeing this solution , I had a need to do something similar but with all Vimeo embeds sitewide.

jQuery to the rescue and must be included for this to work – best used in a dom ready event.

    var agent=navigator.userAgent.toLowerCase();
    var is_iphone = (agent.indexOf('iphone')!='-1');
    var is_ipad = (agent.indexOf('ipad')!='-1');
    if(is_ipad || is_iphone){
	$('embed').each(function(i){
	    var el = $(this);
	    if(el.attr('src').indexOf('clip_id') != -1){
		var reg =  new RegExp(/clip_id=\d*/gi);
		var clipid = el.attr('src').match(reg);
		embed = "<video src='http://www.vimeo.com/play_redirect?"+ clipid + "&quality=mobile'  controls='controls' width='"+el.attr('width')+"' height='"+el.attr('height')+"'></video>";
		el.parent().after(embed);
		el.parent().remove();
	    }
	})
    }

* Note: this solution requires  a Vimeo plus account and you  need to enable “Mobile Embedding” found in | Settings > Embed  – for each embed.  Also, vimeo could change their architecture and break this system.

Tinting/Colorizing a MovieClip in AS3

This task is one I seem to be doing repeatedly, so here is a snippet for all the world to enjoy.

import flash.geom.ColorTransform;
 
private function tint(c:Number, obj:*) {
			var myColor:ColorTransform  =  obj.transform.colorTransform;
				myColor.color = c;
				obj.transform.colorTransform = myColor;
		}
 
 
tint(0xffcc00, myMovieClip);

Ignoring xml tag case in AS3 (E4X)

Sometimes in flash you need to get an XMLList from tags while ignoring tag case.

<xml>
<tagName>contents</tagName>
<TAGNAME>contents</TAGNAME>
</xml>

I have a class that extracts XHTML from the page where a swf is embeded. Works great except different browsers return different case for tags. Below is the snippet I used to get a node list no matter the tag case. The example is getting all “span” tags.

var nodeList:XMLList = data..*.(new RegExp('span',"i")["test"](name()));