Archive for September, 2009

Javascript XML extensions

While several features of Microsoft’s DOMDocument object are not supported by standards, it’s hard to deny that many of them are quite useful, and probably should be standardized.

When writing AJAX web apps, you need easy access to XML nodes. And more than anything, you need it to be cross-browser capable. To that end, I often include the following script in my AJAX apps.

/**
 * XML functions
 */
if (window.ActiveXObject) {
	/* Functions for IE */
	domLoadXml=function(xml){
		var doc=new ActiveXObject('MSXML2.DOMDocument');
		doc.async=false;
		doc.loadXML(xml);
		return doc;
	}
}else{
	/* Functions for non-IE browsers */
	domLoadXml=function(xml){
		var parser=new DOMParser();
		return parser.parseFromString(xml,'text/xml');
	}
	//fill in the missing convenience methods for Firefox
	Element.prototype.selectSingleNode=function(xpath){
		var xpe=new XPathEvaluator();
		var n=xpe.evaluate(xpath,this,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
		if(n!=null) return n.singleNodeValue;
		else return null;
	}
	Element.prototype.selectNodes=function(xpath){
		var xpe=new XPathEvaluator();
		var xpr=xpe.evaluate(xpath,this,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);
		var nodes=new Array();
		if(xpr!=null){
			var el=xpr.iterateNext();
			while(el){
				nodes.push(el);
				el=xpr.iterateNext();
			}
		}
		return nodes;
	}
	if(typeof HTMLElement.prototype.__defineGetter__ != 'undefined'){
		Element.prototype.__defineGetter__('xml', function() {return XMLSerializer().serializeToString(this);} );
		Element.prototype.__defineGetter__('text', function() {return this.textContent;} );
		Element.prototype.__defineSetter__('text', function(s) {this.textContent=s;} );
		//Element.prototype.__defineSetter__('xml', function(s) {} );
	}
}

Tags: ,

Javascript String functions

For any serious enterprise web apps, there are a handful of default Javascript functions I like to include in order to make my life easier in the long run.

Today, I present some handy extensions to the String object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
String.prototype.trim = function () {
	return this.replace(/(^\s+)|(\s+$)/, '');
}
String.prototype.addTrailingSlash = function () {
	return ('/' != this.charAt(this.length - 1)) ? this.toString() + '/' : this.toString();
}
String.prototype.left = function (n) {
	if (n <= 0) return "";
	else if (n > String(this).length) return this;
	else return String(this).substring(0,n);
}
String.prototype.right = function (n){
    if (n <= 0) return "";
    else if (n > String(this).length) return str;
    else {
       var iLen = String(this).length;
       return String(this).substring(iLen, iLen - n);
    }
}

The .trim() method is generally quite useful everywhere where text input is accepted.

The .addTrailingSlash() method is a nice way to help guarantee that you get valid paths and URLs when variables are involved.

The other two methods — .left() and .right() — are merely a conveniece at times, and I actually don’t find myself using them much as I prefer proper use of the .substring() method.

Tags:

WordPress rocks

I am writing this from my iPhone. This proves that wordpress rocks and that the iPhone is awesome. :)

Tags: ,