<?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>felipeNascimento.org(true); &#187; JavaScript</title>
	<atom:link href="http://felipenascimento.org/en/category/learned-stuff/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://felipenascimento.org</link>
	<description>Making the web a better place to live</description>
	<lastBuildDate>Tue, 31 Aug 2010 14:31:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bug on IE when creating elements with name (submitName)</title>
		<link>http://felipenascimento.org/en/bug-on-ie-when-creating-elements-with-name-submitname/</link>
		<comments>http://felipenascimento.org/en/bug-on-ie-when-creating-elements-with-name-submitname/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 04:40:48 +0000</pubDate>
		<dc:creator>Felipe Nascimento</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Learned Stuff / Tips]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://felipenascimento.org/?p=291</guid>
		<description><![CDATA[Hi, yesterday I had to fight against this problem, which is not very touched out there.
In the beginning, people from jQuery thought it was a jQuery bug, but searching a bit more I could find out the root of this problem.
What is it?
When running your javascript in Internet Explorer 6, or 7 or 8 in [...]]]></description>
			<content:encoded><![CDATA[<p>Hi, yesterday I had to fight against this problem, which is not very touched out there.<br />
In the beginning, people from <em>jQuery</em> thought it was a jQuery bug, but searching a bit more I could find out the root of this problem.</p>
<p><b>What is it?</b><br />
When running your javascript in Internet Explorer 6, or 7 or 8 in compatible mode, if you create dynamically an iframe, for example, and set it a <strong>name</strong> attribute, Internet Explorer will reaplace it by an <strong>submitName</strong> attribute. This attribute cannot be found with .getAttribute(&#8217;submitName&#8217;), but that is the problem, neither can be with .getAttribute(&#8217;name&#8217;)!</p>
<p><b>How to see it happening? Try this:</b></p>
<pre class="brush: jscript;">
var ifr= document.createElement('iframe');
ifr.setAttribute('name', 'iFrameOne');
document.body.appendChild(ifr);
alert(ifr.getAttribute('name'));
// you can also see it through the &quot;developer tool&quot; in the IE tools menu
</pre>
<p>The main problem is that &#8230; when you have something like a link or a form targeting this iframe, you loose it! The same happens with inputs with name, which are dynamically created.</p>
<p><b>How to fix it without ask your users to migrate to a real browser? I did this and it worked:</b></p>
<pre class="brush: jscript;">
var ifrDiv= document.createElement('div');
ifrDiv.innerHTML= &quot;&lt;iframe name='iFrameOne' &gt;&lt;/iframe&gt;&quot;;
document.body.appendChild(ifrDiv);
</pre>
<p><b>Now, why does it happen?!</b><br />
I had the chance to search for this and found in the Microsoft&#8217;s webpage something about this old, known bug in <em>Internet Explorer</em>, with names on dynamic elements. Due to &#8220;fix&#8221; this, instead of fixing, then &#8220;provided&#8221; this workarounded attribute. When you try to deal with the name attribute, it applies like an alias, redirecting it to the Microsoft&#8217;s Internet Explorer imaginary <strong>submitName</strong> attribute. But with this, you cant access a form that has a name, like this:</p>
<pre class="brush: jscript;">
document.forms['dynamicFormName'];
</pre>
<p>because the DOM hasn&#8217;t rendered that form with the name you asked for.</p>
<p>I hope it can help you, if you get stuck with this some day.</p>
]]></content:encoded>
			<wfw:commentRss>http://felipenascimento.org/en/bug-on-ie-when-creating-elements-with-name-submitname/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Three ways to reload or move the page</title>
		<link>http://felipenascimento.org/en/three-ways-to-reload-or-move-the-page/</link>
		<comments>http://felipenascimento.org/en/three-ways-to-reload-or-move-the-page/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 02:46:34 +0000</pubDate>
		<dc:creator>Felipe Nascimento</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Learned Stuff / Tips]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://felipenascimento.org/?p=286</guid>
		<description><![CDATA[Hi there.
Well, this is a question I see a lot of people doing! Then, I decided to post about it.
We have got, in Javascript, when dealing with the DOM API, the history element, with which we can move forward or backward trough the navigation historic, plus specify with the method go page or step we [...]]]></description>
			<content:encoded><![CDATA[<p>Hi there.<br />
Well, this is a question I see a lot of people doing! Then, I decided to post about it.<br />
We have got, in Javascript, when dealing with the DOM API, the history element, with which we can move forward or backward trough the navigation historic, plus specify with the method <em>go</em> page or step we want to go, by informing the < 0 value to past, or > 0 to the possible forward pages. So, we can do this, too:</p>
<pre class="brush: jscript;">

history.go(0);
</pre>
<p>This will simply reload the page. Of course you can use it with iframes, using the parent, top, self or name to work with their relation.<br />
Other alternative is using the reload method provided by the <em>location</em>, just like this:</p>
<pre class="brush: jscript;">

self.location.reload();
</pre>
<p>Now, a different alternative, and also a way to move from one page to another, is using the href property, also from location.</p>
<pre class="brush: jscript;">

self.location.href= self.location.href;
</pre>
<p>This code will reload the same page, but could be any other address to be loaded.</p>
]]></content:encoded>
			<wfw:commentRss>http://felipenascimento.org/en/three-ways-to-reload-or-move-the-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starting with Canvas in javascript</title>
		<link>http://felipenascimento.org/en/starting-with-canvas-in-javascript/</link>
		<comments>http://felipenascimento.org/en/starting-with-canvas-in-javascript/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 00:20:10 +0000</pubDate>
		<dc:creator>Felipe Nascimento</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Learned Stuff / Tips]]></category>

		<guid isPermaLink="false">http://felipenascimento.org/?p=266</guid>
		<description><![CDATA[Well, canvas is not that young feature, even though, people are still afraid of using it. Most of our browsers can understand it, and the others (I mean, the Internet Explorer, the onlly one which can&#8217;t) will &#8220;learn&#8221; it soon.
Now, I want to put here some tips about how to start working with canvas easily, [...]]]></description>
			<content:encoded><![CDATA[<p>Well, canvas is not that young feature, even though, people are still afraid of using it. Most of our browsers can understand it, and the others (I mean, the Internet Explorer, the onlly one which can&#8217;t) will &#8220;learn&#8221; it soon.<br />
Now, I want to put here some tips about how to start working with canvas easily, and also, how to import images and draw then into it.</p>
<p>First thing, you need a canvas element, and you need to treat it for <s>those</s> that which can&#8217;t render the canvas element properly.</p>
<pre class="brush: xml;">
    &lt;body&gt;
        &lt;canvas id=&quot;theCanvasElement&quot;&gt;
            whatever you write in here, will be shown ONLY
            when your canvas cannot be rendered.
            Of course, it accepts HTML tags
        &lt;/canvas&gt;
    &lt;/body&gt;
</pre>
<p>Ok, now, using event handlers we will treat it in our  javascript</p>
<pre class="brush: xml;">
    &lt;/body&gt;
    &lt;script&gt;
          // yes, addEventListener does not work on IE
          document.addEventListener('load', canvasHandler, false);
    &lt;script&gt;
&lt;/html&gt;
</pre>
<p>Good. When our page is loaded, it will call our method <em>canvasHandler</em>. Let&#8217;s see how we will open the canvas to use with javascript:</p>
<pre class="brush: jscript;">
// let's create some global variables.
// You can use it better with namespaces
var canvas= null;
var ctx= null;
canvasHandler= function(){
    // first of all, we simply get the canvas element itself
    canvas= document.getElementById('theCanvasElement');
    // now, we need to have the CONTEXT to work with
    ctx= canvas.getContext('2d');
}
</pre>
<p>Great, we have now in our variable <em>canvas</em>, the HTML canvas element itself. While the variable ctx has got its context. The context is what we will use to draw. It has the mothods and properties to allow us to interact with the canvas in 2D.<br />
No, unfortunately it does not offer any other context besides 2D.<br />
Our canvas still has no properties. It has no with and height, we can set it then.<br />
From now on, all the javascript code must be set inside the canvasHandler function&#8217;s body, under those  lines shown  before.</p>
<pre class="brush: jscript;">
    canvas.width= 480;
    canvas.height= 340;
</pre>
<p>You probably know how to load an image  from js, right?</p>
<pre class="brush: jscript;">
    var img= new Image();
    img.src= 'url.png';
</pre>
<p>Ok, you can use it to insert images inside your canvas. Just like this.</p>
<pre class="brush: jscript;">
    // instantiate an image
    var img= new Image();
    // we need to use the image when it has finished to load
    img.addEventListener('load', function(){
        // after downloading the image, we can draw it into the canvas
        ctx.drawImage(this);
        // where this= the image just downloaded
    });
    // then, we say the image's url, to it start loading
    img.src= 'url.png';
</pre>
<p>Ok, now you can see an image inside your canvas. The drawImage method supports some different structures refered by its parameters:</p>
<pre class="brush: jscript;">
    // draws the image in the position left=30, top=30
    ctx.drawImage(this, 30, 30);
    // draws the image in the 0/0 position, changing its size
    ctx.drawImage(this, 0, 0, 45, 75);
    // more complex, draws the image croping it
    ctx.drawImage(this, 0, 0, 150, 150, 0, 0, 480, 340);
</pre>
<p>When cropping, you specify the image (this), the position to start showing it (0, 0), then the size you want to crop it (150, 150). After that, you will tell the canvas the size and position the image really is(0, 0, 480, 340).</p>
<p>Soon, I&#8217;ll post about how to really draw  into your canvas through javascript, adding lines, points and texts.</p>
]]></content:encoded>
			<wfw:commentRss>http://felipenascimento.org/en/starting-with-canvas-in-javascript/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>print_r (the same as in PHP) in Javascript, the print_j</title>
		<link>http://felipenascimento.org/en/print_r-the-same-as-in-php-in-javascript-the-print_j/</link>
		<comments>http://felipenascimento.org/en/print_r-the-same-as-in-php-in-javascript-the-print_j/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 23:48:25 +0000</pubDate>
		<dc:creator>Felipe Nascimento</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[print_j]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[My Projects]]></category>

		<guid isPermaLink="false">http://felipenascimento.org/?p=223</guid>
		<description><![CDATA[Hey folks.
I prepared this small function to use, once it&#8217;s many times needed and we don&#8217;t have it natively in JavaScript. I have many times searched for it through google and couldn&#8217;t find a reall interesting lib to it.
Then, I prepared this one.
You can see the example of the output in my projects directory. And [...]]]></description>
			<content:encoded><![CDATA[<p>Hey folks.<br />
I prepared this small function to use, once it&#8217;s many times needed and we don&#8217;t have it natively in JavaScript. I have many times searched for it through google and couldn&#8217;t find a reall interesting lib to it.<br />
Then, I prepared this one.<br />
You can see the example of the output in my <a href="http://felipenascimento.org/projetos/print_j" target='_quot'>projects directory</a>. And you can download the <a href="http://felipenascimento.org/projetos/print_j/print_j.zip">print_j</a> js.</p>
]]></content:encoded>
			<wfw:commentRss>http://felipenascimento.org/en/print_r-the-same-as-in-php-in-javascript-the-print_j/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax Push / Comet with PHP</title>
		<link>http://felipenascimento.org/en/ajax-push-comet-with-php/</link>
		<comments>http://felipenascimento.org/en/ajax-push-comet-with-php/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 14:06:14 +0000</pubDate>
		<dc:creator>Felipe Nascimento</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Learned Stuff / Tips]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://felipenascimento.org/?p=72</guid>
		<description><![CDATA[Yes, it is possible.
There are many ways to implement Ajax push with PHP. In this case, we will study a way to do so, by using javascript with PHP forcing the page flush, from server side.
First of all, we must understand the ajax&#8230; Ajax asks the server for something

function forceFlush()
      [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, it is possible.<br />
There are many ways to implement Ajax push with PHP. In this case, we will study a way to do so, by using javascript with PHP forcing the page flush, from server side.<br />
First of all, we must understand the ajax&#8230; Ajax asks the server for something</p>
<pre class="brush: php;">
function forceFlush()
        {
            ob_start();
            ob_end_clean();
            flush();
            set_error_handler('_MINDNeutralizeError');
            ob_end_flush();
            restore_error_handler();
        }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://felipenascimento.org/en/ajax-push-comet-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two interesting JavaScript functions</title>
		<link>http://felipenascimento.org/en/two-interesting-javascript-functions-2/</link>
		<comments>http://felipenascimento.org/en/two-interesting-javascript-functions-2/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 05:25:23 +0000</pubDate>
		<dc:creator>Felipe Nascimento</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Learned Stuff / Tips]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://felipenascimento.org/?p=77</guid>
		<description><![CDATA[Two interesting functions in JavaScript I&#8217;ve created due to &#8220;fix&#8221; a few problems I&#8217;ve fought against.
Both functins exist in PHP, and I needed to use&#8217;em in my client side.
The first one, is the strrev function. Javascript has in the Array prototype, the function reverse, but not in strings. Then, here goes the &#8220;one line&#8221; function [...]]]></description>
			<content:encoded><![CDATA[<p>Two interesting functions in JavaScript I&#8217;ve created due to &#8220;fix&#8221; a few problems I&#8217;ve fought against.<br />
Both functins exist in PHP, and I needed to use&#8217;em in my client side.<br />
The first one, is the strrev function. Javascript has in the Array prototype, the function reverse, but not in strings. Then, here goes the &#8220;one line&#8221; function to revert strings in javascript.<br />
<span id="more-77"></span><br />
function strrev(str)<br />
{<br />
	return str.split(&#8221;).reverse().join(&#8221;);<br />
}</p>
<p>We must use it like this:<br />
var rev= strrev(&#8221;Felipe Nascimento&#8221;);</p>
<p>we also could have put it into the prototype of our strings:</p>
<p>String.prototype.invert= function(){<br />
	return this.split(&#8221;).reverse().join(&#8221;);<br />
}</p>
<p>In this case, the example would be:<br />
&#8220;Felipe Nascimento&#8221;.invert();</p>
<p>Everything I did here, was just transform the string into an Array, revert it, and then transform it into a String again.</p>
<p>The second is a more complex function, but a quite useful one. It&#8217;s the number_format function, also inherited from PHP.<br />
It&#8217;s signature is: numberFormat(num, dec, decPoint, thousandSep)<br />
It receives a number or string, and returns it transformed by those patterns sent to it.<br />
We can use it to change the decimal places(values after , or .), as it depends on the country parttener, and other detalis.<br />
It&#8217;s usage is just like this:</p>
<p>alert(numberFormat(&#8217;1050.1&#8242;, 2, &#8216;.&#8217;, &#8216; &#8216;));<br />
alert(numberFormat(&#8217;10.50,1&#8242;, 2, &#8216;,&#8217;, &#8216;.&#8217;));<br />
alert(numberFormat(&#8217;10050.123456&#8242;, 5, &#8216;,&#8217;, &#8216; &#8216;));<br />
alert(numberFormat(&#8217;10.50,1234&#8242;, 3, &#8216;~&#8217;, &#8216;.&#8217;));</p>
<p>The first example is the more used, in which we are forcing the value to have only 2 decimal places.<br />
We can force it to have as many as we want, which will fill the value with zeros.<br />
We can also choose the decimal point, and also the thousand separator.<br />
Here, follows the code:</p>
<p>function numberFormat(num, dec, decPoint, thousandSep)<br />
{<br />
	if(typeof num == &#8217;string&#8217;)<br />
	{<br />
		if(num.indexOf(&#8217;,')>-1 &#038;&#038; num.indexOf(&#8217;.')>-1)<br />
		{ // fixing strings<br />
			if(num.indexOf(&#8217;,') > num.indexOf(&#8217;.'))<br />
			{ // eg.: 10.000,000 => 10000.000<br />
				num= num.replace(&#8217;.', &#8221;).replace(&#8217;,', &#8216;.&#8217;);<br />
			}else{ // eg.: 10,000.000 => 10000.000<br />
					num= num.replace(&#8217;,', &#8221;);<br />
				 }<br />
		}else{<br />
				if(num.indexOf(&#8217;,')>-1)<br />
				{ // eg.: 10000,000 => 10000.000<br />
					num= num.replace(&#8217;,', &#8216;.&#8217;);<br />
				}<br />
			 }<br />
	}<br />
	dec= dec|| 2; // 2 decimal places, as default<br />
	num= Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);<br />
	num= (&#8221;+num).replace(&#8217;.', (decPoint||&#8217;.'));<br />
	while(num.split(decPoint)[1].length < dec)<br />
		num+= &#8216;0&#8242;; // fixing decimal places, after the decimal point<br />
	// apply thousand separatores<br />
	num= num.split(decPoint);<br />
	num[0]= strrev(strrev(num[0]).replace(/([0-9]{3})/g, &#8216;$1&#8242;+(thousandSep||&#8217; &#8216;)));<br />
	return num.join(decPoint);<br />
}</p>
<p>I hope it becomes useful to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://felipenascimento.org/en/two-interesting-javascript-functions-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Absurd error using Internet Explorer</title>
		<link>http://felipenascimento.org/en/absurd-error-using-internet-explorer/</link>
		<comments>http://felipenascimento.org/en/absurd-error-using-internet-explorer/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 04:00:12 +0000</pubDate>
		<dc:creator>Felipe Nascimento</dc:creator>
				<category><![CDATA[Generic]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Learned Stuff / Tips]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://felipenascimento.org/?p=89</guid>
		<description><![CDATA[En:
Today, I fought with the following error in Internet Explorer: System Erro: -1072896658
I got curious, once it used to work prefectly in all the browsers, Internet epxlorer was blowing the error to me(ok, it is not a surprise at all)!
After a lot of tests and have searched all the web, I found out that our [...]]]></description>
			<content:encoded><![CDATA[<p>En:<br />
Today, I fought with the following error in Internet Explorer: System Erro: -1072896658<br />
I got curious, once it used to work prefectly in all the browsers, Internet epxlorer was blowing the error to me(ok, it is not a surprise at all)!<br />
After a lot of tests and have searched all the web, I found out that our friend IE culdn&#8217;t build the Ajax object when it had come back if the charset of the current page was UTF-8. The solution? The absurdest one. I just had to change its charset to utf-8&#8230; yes, in lower-case.<br />
Just the case changed the page itself.<br />
I hope this post will help someone.</p>
<hr/>
Pt:<br />
Hoje me deparei com o seguinte erro no Internet Explorer: Erro de sistema: -1072896658 Fiquei curioso, uma vez que funcionava perfeitamente para todos os navegadores, exceto IE(o que não é nenhuma surpresa)! Apos muitos testes, e boas pesquisas na web, descobri que o IE nao conseguia construir o objeto ajax no seu retorno, caso o charset da página estivesse setado para UTF-8. A solução? Mais absurda que o problema, bastou setar o charset para utf-8. Sim, minúsculo, apenas. Espero que acabe ajudando alguem com isto, ja que me deu uma certa dor de cabeça. </p>
]]></content:encoded>
			<wfw:commentRss>http://felipenascimento.org/en/absurd-error-using-internet-explorer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery at TargetTrust</title>
		<link>http://felipenascimento.org/en/jquery-at-targettrust/</link>
		<comments>http://felipenascimento.org/en/jquery-at-targettrust/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 03:51:38 +0000</pubDate>
		<dc:creator>Felipe Nascimento</dc:creator>
				<category><![CDATA[Generic]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://felipenascimento.org/?p=87</guid>
		<description><![CDATA[Jaydson e eu apresentamos uma palestra na TargetTrust nesa noite.
A palestra foi muito legal, e conseguimos prender bem a atenção dos quase 20 interessados.
Abaixo, encontramos os slides da palestra.
Vamos ver se não transformamos isso em um novo curso, focado em jQuery.
Como Fazer (+) Em AplicaçõEs Ria Escrevendo ( )
View more presentations from Felipe Nascimento.

]]></description>
			<content:encoded><![CDATA[<p><a href="http://Jaydson.org">Jaydson</a> e eu apresentamos uma palestra na <a href="http://targettrust.com.br">TargetTrust</a> nesa noite.<br />
A palestra foi muito legal, e conseguimos prender bem a atenção dos quase 20 interessados.<br />
Abaixo, encontramos os slides da palestra.<br />
Vamos ver se não transformamos isso em um novo curso, focado em jQuery.</p>
<div style="width:425px;text-align:left" id="__ss_2525170"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/felipenmoura/como-fazer-em-aplicaes-ria-escrevendo" title="Como Fazer (+) Em AplicaçõEs Ria Escrevendo ( )">Como Fazer (+) Em AplicaçõEs Ria Escrevendo ( )</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=comofazeremaplicaesriaescrevendo-091117214940-phpapp02&#038;rel=0&#038;stripped_title=como-fazer-em-aplicaes-ria-escrevendo" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=comofazeremaplicaesriaescrevendo-091117214940-phpapp02&#038;rel=0&#038;stripped_title=como-fazer-em-aplicaes-ria-escrevendo" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/felipenmoura">Felipe Nascimento</a>.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://felipenascimento.org/en/jquery-at-targettrust/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Two interesting JavaScript functions</title>
		<link>http://felipenascimento.org/en/two-interesting-javascript-functions/</link>
		<comments>http://felipenascimento.org/en/two-interesting-javascript-functions/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 12:04:18 +0000</pubDate>
		<dc:creator>Felipe Nascimento</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Learned Stuff / Tips]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://felipenascimento.org/?p=78</guid>
		<description><![CDATA[Two interesting functions in JavaScript I&#8217;ve created due to &#8220;fix&#8221; a few problems I&#8217;ve fought against.
Both functins exist in PHP, and I needed to use&#8217;em in my client side.
The first one, is the strrev function. Javascript has in the Array prototype, the function reverse, but not in strings. Then, here goes the &#8220;one line&#8221; function [...]]]></description>
			<content:encoded><![CDATA[<p>Two interesting functions in JavaScript I&#8217;ve created due to &#8220;fix&#8221; a few problems I&#8217;ve fought against.<br />
Both functins exist in PHP, and I needed to use&#8217;em in my client side.<br />
The first one, is the strrev function. Javascript has in the Array prototype, the function reverse, but not in strings. Then, here goes the &#8220;one line&#8221; function to revert strings in javascript.</p>
<pre class="brush: jscript;">
function strrev(str)
{
	return str.split('').reverse().join('');
}
</pre>
<p>We must use it like this:<br />
var rev= strrev(&#8221;Felipe Nascimento&#8221;);</p>
<p>we also could have put it into the prototype of our strings:</p>
<pre class="brush: jscript;">
String.prototype.invert= function(){
	return this.split('').reverse().join('');
}
</pre>
<p>In this case, the example would be:<br />
&#8220;Felipe Nascimento&#8221;.invert();</p>
<p>Everything I did here, was just transform the string into an Array, revert it, and then transform it into a String again.</p>
<p>The second is a more complex function, but a quite useful one. It&#8217;s the number_format function, also inherited from PHP.<br />
It&#8217;s signature is: numberFormat(num, dec, decPoint, thousandSep)<br />
It receives a number or string, and returns it transformed by those patterns sent to it.<br />
We can use it to change the decimal places(values after , or .), as it depends on the country parttener, and other detalis.<br />
It&#8217;s usage is just like this:</p>
<pre class="brush: jscript;">
alert(numberFormat('1050.1', 2, '.', ' '));
alert(numberFormat('10.50,1', 2, ',', '.'));
alert(numberFormat('10050.123456', 5, ',', ' '));
alert(numberFormat('10.50,1234', 3, '~', '.'));
</pre>
<p>The first example is the more used, in which we are forcing the value to have only 2 decimal places.<br />
We can force it to have as many as we want, which will fill the value with zeros.<br />
We can also choose the decimal point, and also the thousand separator.<br />
Here, follows the code:</p>
<pre class="brush: jscript;">
function numberFormat(num, dec, decPoint, thousandSep)
{
	if(typeof num == 'string')
	{
		if(num.indexOf(',')&amp;amp;gt;-1 &amp;amp;amp;&amp;amp;amp; num.indexOf('.')&amp;amp;gt;-1)
		{ // fixing strings
			if(num.indexOf(',') &amp;amp;gt; num.indexOf('.'))
			{ // eg.: 10.000,000 =&amp;amp;gt; 10000.000
				num= num.replace('.', '').replace(',', '.');
			}else{ // eg.: 10,000.000 =&amp;amp;gt; 10000.000
					num= num.replace(',', '');
				 }
		}else{
				if(num.indexOf(',')&amp;amp;gt;-1)
				{ // eg.: 10000,000 =&amp;amp;gt; 10000.000
					num= num.replace(',', '.');
				}
			 }
	}
	dec= dec|| 2; // 2 decimal places, as default
	num= Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	num= (''+num).replace('.', (decPoint||'.'));
	while(num.split(decPoint)[1].length &amp;amp;lt; dec)
		num+= '0'; // fixing decimal places, after the decimal point
	// apply thousand separatores
	num= num.split(decPoint);
	num[0]= strrev(strrev(num[0]).replace(/([0-9]{3})/g, '$1'+(thousandSep||' ')));
	return num.join(decPoint);
}
</pre>
<p>I hope it becomes useful to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://felipenascimento.org/en/two-interesting-javascript-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
