<?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>Jtanium's Notebook &#187; Javascript</title>
	<atom:link href="http://www.jtanium.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jtanium.com</link>
	<description>I jot things down, in hopes of finding them later...</description>
	<lastBuildDate>Wed, 21 Dec 2011 23:21:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Firefox Gotcha: innerHTML Strips td Tags</title>
		<link>http://www.jtanium.com/2009/10/28/firefox-gotcha-innerhtml-strips-td-tags/</link>
		<comments>http://www.jtanium.com/2009/10/28/firefox-gotcha-innerhtml-strips-td-tags/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 22:16:38 +0000</pubDate>
		<dc:creator>jtanium</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[appendchild]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[innerhtml]]></category>

		<guid isPermaLink="false">http://www.jtanium.com/?p=166</guid>
		<description><![CDATA[Try and run the following in Firefox, and it&#8217;s gonna tell you &#8220;you&#8217;re screwed&#8221;: var newTable = document.createElement('table'); var rowContent = '&#60;td&#62;some stuff&#60;/td&#62;&#60;td&#62;&#60;em&#62;something else&#60;/em&#62;&#60;/td&#62;'; var newTr = document.createElement('tr'); newTr.innerHTML = rowContent; newTable.appendChild(newTr); if ('&#60;td&#62;some stuff&#60;/td&#62;&#60;td&#62;&#60;em&#62;something else&#60;/em&#62;&#60;/td&#62;' == newTr.innerHTML) { alert("Everything's hunkydory"); } else { alert("You're screwed"); } Why? My guess is Firefox doesn&#8217;t like it [...]]]></description>
			<content:encoded><![CDATA[<p>Try and run the following in Firefox, and it&#8217;s gonna tell you &#8220;you&#8217;re screwed&#8221;:</p>
<pre>
var newTable = document.createElement('table');
var rowContent = '&lt;td&gt;some stuff&lt;/td&gt;&lt;td&gt;&lt;em&gt;something else&lt;/em&gt;&lt;/td&gt;';
var newTr = document.createElement('tr');
newTr.innerHTML = rowContent;
newTable.appendChild(newTr);

if ('&lt;td&gt;some stuff&lt;/td&gt;&lt;td&gt;&lt;em&gt;something else&lt;/em&gt;&lt;/td&gt;' == newTr.innerHTML) {
    alert("Everything's hunkydory");
}
else {
    alert("You're screwed");
}
</pre>
<p>Why? My guess is Firefox doesn&#8217;t like it when you add td tags to elements that aren&#8217;t in a table, so it strips them. After doing the innerHTML assignment</p>
<pre>
&lt;td&gt;some stuff&lt;/td&gt;&lt;td&gt;&lt;em&gt;something else&lt;/em&gt;&lt;/td&gt;
</pre>
<p> becomes</p>
<pre>some stuff&lt;em&gt;something else&lt;/em&gt;</pre>
<p>Fortunately, the solution is easy, just append the tr before the innerHTML assignment:</p>
<pre>
newTable.appendChild(newTr);
newTr.innerHTML = rowContent;
</pre>
<p>Then you&#8217;ll see &#8220;Everything&#8217;s hunkydory&#8221;. <img src='https://www.jtanium.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtanium.com/2009/10/28/firefox-gotcha-innerhtml-strips-td-tags/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Format Currency in JavaScript</title>
		<link>http://www.jtanium.com/2009/10/14/format-currency-in-javascript/</link>
		<comments>http://www.jtanium.com/2009/10/14/format-currency-in-javascript/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 20:33:34 +0000</pubDate>
		<dc:creator>jtanium</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.jtanium.com/2009/10/14/format-currency-in-javascript/</guid>
		<description><![CDATA[I went cruising around the web to find a good, easy way, to format a number into currency in JavaScript. No luck. I took ideas from various places and this is what I came up with: function numberToCurrency(number) { var currencySymbol = '$'; var thousandsSeparator = ','; number = stripDollarSign(number); number = isNaN(number) &#124;&#124; number [...]]]></description>
			<content:encoded><![CDATA[<p>I went cruising around the web to find a good, easy way, to format a number into currency in JavaScript.  No luck.</p>
<p>I took ideas from various places and this is what I came up with:</p>
<pre>

function numberToCurrency(number) {

    var currencySymbol     = '$';
    var thousandsSeparator = ',';

    number = stripDollarSign(number);
    number = isNaN(number) || number == '' || number == null ? 0.00 : number;
    var numberStr = parseFloat(number).toFixed(2).toString();
    var numberFormatted = new Array(numberStr.slice(-3));   // this returns the decimal and cents
    numberStr = numberStr.substring(0, numberStr.length-3); // this removes the decimal and cents
    /*
     * Why is there an `unshift()` function, but no `shift()`?
     * Also, a `pop()` function would be handy here.
     */
    while (numberStr.length > 3) {
        numberFormatted.unshift(numberStr.slice(-3)); // this prepends the last three digits to `numberFormatted`
        numberFormatted.unshift(thousandsSeparator); // this prepends the thousandsSeparator to `numberFormatted`
        numberStr = numberStr.substring(0, numberStr.length-3);  // this removes the last three digits
    }
    numberFormatted.unshift(numberStr); // there are less than three digits in numberStr, so prepend them
    numberFormatted.unshift(currencySymbol); // prepend the currencySymbol

    return numberFormatted.join(''); // put it all together
}

function stripDollarSign(s) {
    if (typeof s == 'string') { s = s.replace(/\$/g, ''); }
    return s;
}</pre>
<p>I even created a unit test to make sure it functions as expected:</p>
<pre>
    testNumberToCurrency: function() { with(this) {
            assertEqual('$1.20', numberToCurrency(1.2));
            assertEqual('$1.20', numberToCurrency('1.2'));
            assertEqual('$1.20', numberToCurrency('1.202222'));
            assertEqual('$1.21', numberToCurrency('1.205555'));
            assertEqual('$1.20', numberToCurrency('$1.20'));
            assertEqual('$1.20', numberToCurrency('$1.2'));
            assertEqual('$1,000.20', numberToCurrency('$1000.2'));
            assertEqual('$10,000.20', numberToCurrency('$10000.2'));
            assertEqual('$100,000.20', numberToCurrency('$100000.2'));
            assertEqual('$1,000,000.20', numberToCurrency('$1000000.2'));
            assertEqual('$10,000,000.20', numberToCurrency('$10000000.2'));
    }}, </pre>
<p>Naturally this is a Scriptaculous Test.Unit.Runner test.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtanium.com/2009/10/14/format-currency-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

