<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>yesJames.com &#187; JavaScript</title>
	<atom:link href="http://www.yesjames.com/index.php/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yesjames.com</link>
	<description>ahuh... sure... what ever you say...</description>
	<lastBuildDate>Fri, 21 Oct 2011 05:16:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Firefox compatible xPath functions in JavaScript</title>
		<link>http://www.yesjames.com/index.php/2008/11/firefox-compatible-xpath-functions-in-javascript/</link>
		<comments>http://www.yesjames.com/index.php/2008/11/firefox-compatible-xpath-functions-in-javascript/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 01:41:27 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XPath]]></category>

		<guid isPermaLink="false">http://www.yesjames.com/?p=74</guid>
		<description><![CDATA[There are a lot of quirks between different browsers, in particular though are the differences in the way each browser handles the DOM in JavaScript. Internet Explorer, for the most part, implements MSXml 4.0 or higher. However that&#8217;s (obviously) a Microsoft technology, and the standard implementation doesn&#8217;t support XPath in the DOM. In particular the [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of quirks between different browsers, in particular though are the differences in the way each browser handles the DOM in JavaScript.</p>
<p>Internet Explorer, for the most part, implements MSXml 4.0 or higher. However that&#8217;s (obviously) a Microsoft technology, and the standard implementation doesn&#8217;t support XPath in the DOM.</p>
<p>In particular the <strong>selectNodes()</strong> and <strong>selectSingleNode()</strong> functions are of particular use when manipulating XML or the DOM. Using these functions you can parse an XPath expression and get a nodelist of elements in your DOM that match the expression.</p>
<p><span id="more-74"></span></p>
<p>The code below adds basic XPath parsing functionality to the DOM in Mozilla. (Thanks to km0ti0n)</p>
<pre name="code" class="brush: javascript">
// mozXPath - http://km0.la/js/mozXPath/
// km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License
// http://creativecommons.org/licenses/by-sa/2.5/
if (document.implementation.hasFeature("XPath", "3.0")) {
	if ( typeof XMLDocument == "undefined" ) {
		XMLDocument = Document;
	}
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode) {
		if ( !xNode ) { xNode = this; }
		var oNSResolver = this.createNSResolver(this.documentElement);
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		var aResult = [];
		for ( var i = 0; i < aItems.snapshotLength; i++) {
			aResult[i] =  aItems.snapshotItem(i);
		}
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) {
		if( !xNode ) { xNode = this; }
		var xItems = this.selectNodes(cXPathString, xNode);
		if ( xItems.length > 0 ) { return xItems[0]; }
		else { return null; }
	}

	Element.prototype.selectNodes = function(cXPathString) {
		if (this.ownerDocument.selectNodes) {
			return this.ownerDocument.selectNodes(cXPathString, this);
		} else { throw "For XML Elements Only"; }
	}

	Element.prototype.selectSingleNode = function(cXPathString) {
		if (this.ownerDocument.selectSingleNode) {
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		} else { throw "For XML Elements Only"; }
	}
}
</pre>
<p>It appears that Opera doesn&#8217;t have the XMLDocument object type, so, when using the XmlHttpRequest object the <em>responseXML</em> is in fact a <em>Document</em> object.</p>
<p>To get around this fact, we can use:</p>
<pre name="code" class="brush: javascript">
if ( typeof XMLDocument == "undefined" ) {
    XMLDocument = Document;
}
</pre>
<p>To use these new features, in either IE or Firefox, you can now simple call the functions as a method of the elements themselves&#8230; e.g.:</p>
<p>On the following XML Document:</p>
<pre name="code" class="brush: xml">
<MainNode>
    <SubNode id="1" enabled="true">
      <ContentNode>Some Content</ContentNode>
    </SubNode>
    <SubNode id="2" enabled="true">
      <ContentNode>Some Content</ContentNode>
    </SubNode>
    <SubNode id="3" enabled="false">
      <ContentNode>Some Content</ContentNode>
    </SubNode>
</MainNode>
</pre>
<p>For the XML sample above, you can use the following XPath to select all <em>ContentNode</em>&#8216;s with their <em>enabled</em> attribute set to <em>&#8220;true&#8221;</em>:<br />
<code>//subnode[@enabled='true']/contentnode</code></p>
<p>So you would retrieve your XML Node List by executing the following JavaScript function:</p>
<pre name="code" class="brush: javascript">
//assume the variable xDOM is an XmlDocument object
//...that has been pre-loaded with the example XML
var nodeList = xDOM.selectNodes('//subnode[@enabled='true']/contentnode');
for each (var xNode in nodeList) {
    //do some operation...
}
</pre>
<p>This would perform some operation on each <em>contentnode</em> which is inside a <em>subnode</em> with it&#8217;s <em>enabled</em> property set to <em>true</em>.</p>
<p><em><strong>NOTE:</strong> Be sure to double check your syntax, spelling and remember XML/XPath is CaseSensitive.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yesjames.com/index.php/2008/11/firefox-compatible-xpath-functions-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>howto: Format numbers in JavaScript</title>
		<link>http://www.yesjames.com/index.php/2008/11/howto-format-numbers-in-javascript/</link>
		<comments>http://www.yesjames.com/index.php/2008/11/howto-format-numbers-in-javascript/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 23:17:32 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[web programming]]></category>

		<guid isPermaLink="false">http://www.yesjames.com/?p=41</guid>
		<description><![CDATA[Formatting Numbers Numeric formatting in JavaScript can be very tedious. From handling non-numeric characters, to multiple-currency symbols, to alternate negative display methods. The functions below should be valuable in assisting to format numbers and currencies of all types. To do complete formatting of a number, the following function formatNumber(), takes several parameters specifying the properties [...]]]></description>
			<content:encoded><![CDATA[<h3>Formatting Numbers</h3>
<p>Numeric formatting in JavaScript can be very tedious. From handling non-numeric characters, to multiple-currency symbols, to alternate negative display methods. The functions below should be valuable in assisting to format numbers and currencies of all types.</p>
<p><span id="more-41"></span></p>
<p>To do complete formatting of a number, the following function <b>formatNumber()</b>, takes several parameters specifying the properties of the output.</p>
<pre name="code" class="brush: js">
/* formatNumber: PARAMETERS...
num: the decimal number to convert (must be numeric)
dec: number of resulting decimal places to keep
thou: the character to use for thousands separator
pnt: the character to use for decimal point
curr1: Preceding Currency Symbol
curr2: Trailing Currency symbol
n1: Preceding Negative character
n2: Trailing Negative character
*/
function formatNumber(num, dec, thou, pnt, curr1, curr2, n1, n2) {
	var x = Math.round(num * Math.pow(10,dec));
	if (x >= 0) n1 = n2 = '';
	var y = (''+ Math.abs(x)).split('');
	var z = y.length - dec;
	if (z<0) z--;
	for (var i = z; i < 0; i++) y.unshift('0');
	if (z<0) z = 1;
	y.splice(z, 0, pnt);
	if (y[0] == pnt) y.unshift('0');
	while (z > 3) { z-=3; y.splice(z,0,thou); }
	var r = curr1 + n1 + y.join('') + n2 + curr2;
	return r;
}
</pre>
<p>To use this function, simply call the function with the parameters you require.<br />
e.g. To display a currency (western such as USD or AUD):</p>
<pre name="code" class="brush: js">
var cur = formatNumber(1257.45, 2, ',', '.', '$', '', '-', '');
alert(cur);
</pre>
<p>will output: <b>$1,257.45</b></p>
<p>Alternatively, you may decide to use a helper function that defaults most of these values for you. For example:</p>
<pre name="code" class="brush: js">
function formatCurrency(num) {
  return formatNumber(num, 2, ',', '.', '$', '', '-', '');
}
</pre>
<p>The thousandsFormat() function takes any decimal number, negative or positive, and formats it by adding commas every three digits. This makes the number more readable for your site visitors:</p>
<pre name="code" class="brush: js">
// This function formats numbers by adding commas
function thousandsFormat(nStr) {
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length &gt; 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1))
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  return x1 + x2;
}
</pre>
<h2>Strip Non-Numeric Characters From a String</h2>
<p>The stripNonNumeric() function strips any non-numeric characters from a string leaving you with a valid decimal number. This function considers the minus sign (hyphen) and the period to be numeric and will not strip them unless the minus sign is not at the beginning of the number or there is more than one period:</p>
<pre name="code" class="brush: js">
// This function removes non-numeric characters
function stripNonNumeric(str) {
  str += '';
  var rgx = /^\d|\.|-$/;
  var out = '';
  for (var i = 0; i < str.length; i++) {
    if (rgx.test(str.charAt(i))) {
      if (!((str.charAt(i)=='.' &#038;&#038; out.indexOf('.')!=-1)
        || (str.charAt(i)=='-' &#038;&#038;  out.length!=0))) {
        out += str.charAt(i);
      }
    }
  }
  return out;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.yesjames.com/index.php/2008/11/howto-format-numbers-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Trim() function</title>
		<link>http://www.yesjames.com/index.php/2008/03/javascript-trim-function/</link>
		<comments>http://www.yesjames.com/index.php/2008/03/javascript-trim-function/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 05:25:14 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[trim]]></category>
		<category><![CDATA[web design]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.yesjames.com/index.php/2008/03/20/javascript-trim-function/</guid>
		<description><![CDATA[Trim(), is one function that we all use almost every day in most languages, whether it&#8217;s a core function of the language you&#8217;re using or a member (or prototype in JavaScript) function of the String object itself (as in C#), it&#8217;s invaluable for developers. Unfortunately JavaScript doesn&#8217;t have this sort of thing built in, so [...]]]></description>
			<content:encoded><![CDATA[<p>Trim(), is one function that we all use almost every day in most languages, whether it&#8217;s a core function of the language you&#8217;re using or a member (or prototype in JavaScript) function of the String object itself (as in C#), it&#8217;s invaluable for developers.</p>
<p>Unfortunately JavaScript doesn&#8217;t have this sort of thing built in, so here&#8217;s how to implement your own <em>trim()</em> functions (as well as ltrim() and rtrim()) in various ways depending on the level of backward compatibility you require.</p>
<p><span id="more-42"></span></p>
<h3>trim() member functions</h3>
<p>Use the code below to make trim a method of all Strings. Ordinarily you would place this code into a global JavaScript file that would be included in all your pages.</p>
<pre name="code" class="brush: javascript">
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}
</pre>
<h4>Examples using trim(), ltrim(), and rtrim() prototyped methods</h4>
<pre name="code" class="javascript">
var testString = " hello world ";
alert("#"+ testString.trim() +"#");
alert("#"+ testString.ltrim() +"#");
alert("#"+ testString.rtrim() +"#");
</pre>
<h3>standalone JavaScript functions</h3>
<p>If you&#8217;d prefer to use regular functions to return trimmed strings rather than modifying the String object using prototyping, then try the following code:</p>
<pre name="code" class="brush: javascript">
function trim(value) {
    return value.replace(/^\s+|\s+$/g,"");
}
function ltrim(value) {
    return value.replace(/^\s+/,"");
}
function rtrim(value) {
    return value.replace(/\s+$/,"");
}
</pre>
<h4>Examples using trim(), ltrim(), and rtrim() functions</h4>
<pre name="code" class="brush: javascript">
var testString = " hello world ";
alert("#"+ trim(testString) +"#");
alert("#"+ ltrim(testString) +"#");
alert("#"+ rtrim(testString) +"#");
</pre>
<h3>Compatibility</h3>
<p>The above functions require JavaScript 1.2+ and JScript 3.0+ as they are using Regular Expressions to work their magic. This pretty much covers all modern browsers (v4 and above of IE and opera, and all versions of Firefox).<br />
If you require functions that will be compatible with very old browsers only supporting JavaScript 1.0 or there about, try using the following set of functions. These functions strip the following standard whitespace characters: space, tab, line feed, carriage return, and form feed. The IsWhitespace function checks if a character is whitespace.</p>
<pre name="code" class="brush: javascript">
function ltrim(str) {
    for(var k = 0;
        k &lt; str.length &amp;&amp; isWhitespace(str.charAt(k));
        k++);
    return str.substring(k, str.length);
}
function rtrim(str) {
    for(var j=str.length-1;
        j&gt;=0 &amp;&amp; isWhitespace(str.charAt(j));
        j--);
    return str.substring(0,j+1);
}
function trim(str) {
    return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
    var whitespaceChars = " \t\n\r\f";
    return (whitespaceChars.indexOf(charToCheck) != -1);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.yesjames.com/index.php/2008/03/javascript-trim-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>howto: GET a URL parameter in JavaScript</title>
		<link>http://www.yesjames.com/index.php/2008/02/howto-get-a-url-parameter-in-javascript/</link>
		<comments>http://www.yesjames.com/index.php/2008/02/howto-get-a-url-parameter-in-javascript/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 08:38:01 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[get parameter]]></category>
		<category><![CDATA[parameters]]></category>
		<category><![CDATA[url parameter]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.yesjames.com/index.php/2008/02/11/howto-get-a-url-parameter-in-javascript/</guid>
		<description><![CDATA[A useful little piece of JavaScript for retrieving a URL parameter (Query String) from your page. If a requested parameter doesn&#8217;t exist it will return an empty string rather than a NULL object. It will also handle the inclusion of anchor tags in the URL and exclude them from possible results. e.g. http://www.yesjames.com/test.php?myparam=justatest calling &#8220;gerURLParam(&#8216;myparam&#8217;) [...]]]></description>
			<content:encoded><![CDATA[<p>A useful little piece of JavaScript for retrieving a URL parameter (Query String) from your page. If a requested parameter doesn&#8217;t exist it will return an empty string rather than a NULL object. It will also handle the inclusion of anchor tags in the URL and exclude them from possible results.</p>
<blockquote><p>e.g. http://www.yesjames.com/test.php?myparam=justatest<br />
calling &#8220;gerURLParam(&#8216;myparam&#8217;) will return &#8220;justatest&#8221;</p></blockquote>
<pre name="code" class="brush: javascript">
function getURLParam(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var pattern = "[\\?&amp;]"+name+"=([^&amp;#]*)";
  var regex = new RegExp(pattern);
  var aResult = regex.exec(window.location.href);
  if ( aResult == null ) {
    return "";
  } else {
    return aResult[1];
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.yesjames.com/index.php/2008/02/howto-get-a-url-parameter-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

