Mathmagician Wiki
Advertisement

This page contains useful JavaScript functions that I may want to reuse. Note: These aren't fully functional scripts by themselves, they're just tools used to build other things!

getComputedStyleCBJC[]

Usage examples:
1 getComputedStyleCBJC(document.getElementById('WikiaRail'))
2 getComputedStyleCBJC(document.getElementsByClassName('module'))
3 getComputedStyleCBJC($('.module'))
4 getComputedStyleCBJC('.module')

/**
 * Gets the computed style (CSSStyleDeclaration) of a single HTMLElement.
 * Cross-browser and jQuery support. By - User:Mathmagician
 * @param selection Possible values: an HTMLElement, an array of
 * HTMLElements, a jQuery object, a string containing a selector to use to
 * create a jQuery object, or any function that returns one of the above
 * @returns CSSStyleDeclaration the set of computed CSS styles for the given element
 */
function getComputedStyleCBJC (selection) {
	var elem;
	if ("function" == typeof selection) {
		selection = selection();
	}
	if (selection instanceof HTMLElement) {
		elem = selection;
	} else if (selection.hasOwnProperty(0) && selection[0] instanceof HTMLElement) {
		elem = selection[0];
	} else if (!("string" == typeof selection && (elem = $(selection).get(0)) instanceof HTMLElement)) {
		return 'Invalid selection passed to getComputedStyle';
	}
	if (window.getComputedStyle) { // chrome, firefox
		return window.getComputedStyle(elem);
	} else if ('undefined' != typeof elem.currentStyle) { // internet explorer 8-
		return elem.currentStyle;
	} else if ('undefined' != typeof document.defaultView
			&& 'function' == typeof document.defaultView.getComputedStyle) { // oldschool fallback
		return document.defaultView.getComputedStyle(elem, null);
	} else {
		return 'Your browser doesn\'t appear to be supported.';
	}
}
Advertisement