felipeNascimento.org(true);

Making the web a better place to live

Browsing Posts tagged js

Olá pessoal.
Preparei esta pequena função para usar, uma vez que muitas vezes precisamos disso e JavaScript não conta com esta função nativamente.
Tenho muitas vezes pesquisado google afora e não pude encontrar uma biblioteca realmente interessante para isto
Então, resolvi preparar uma eu mesmo.
Você pode ver um exemplo de seu output em meu diretório de projetos. E também pode baixar o print_j.

Ajax Push / Comet with PHP

No comments

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… Ajax asks the server for something

function forceFlush()
{
ob_start();
ob_end_clean();
flush();
set_error_handler(’_MINDNeutralizeError’);
ob_end_flush();
restore_error_handler();
}

Two interesting JavaScript functions

No comments

Two interesting functions in JavaScript I’ve created due to “fix” a few problems I’ve fought against.
Both functins exist in PHP, and I needed to use’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 “one line” function to revert strings in javascript.
function strrev(str)
{
return str.split(”).reverse().join(”);
}

We must use it like this:
var rev= strrev(”Felipe Nascimento”);

we also could have put it into the prototype of our strings:

String.prototype.invert= function(){
return this.split(”).reverse().join(”);
}

In this case, the example would be:
“Felipe Nascimento”.invert();

Everything I did here, was just transform the string into an Array, revert it, and then transform it into a String again.

The second is a more complex function, but a quite useful one. It’s the number_format function, also inherited from PHP.
It’s signature is: numberFormat(num, dec, decPoint, thousandSep)
It receives a number or string, and returns it transformed by those patterns sent to it.
We can use it to change the decimal places(values after , or .), as it depends on the country parttener, and other detalis.
It’s usage is just like this:

alert(numberFormat(’1050.1′, 2, ‘.’, ‘ ‘));
alert(numberFormat(’10.50,1′, 2, ‘,’, ‘.’));
alert(numberFormat(’10050.123456′, 5, ‘,’, ‘ ‘));
alert(numberFormat(’10.50,1234′, 3, ‘~’, ‘.’));

The first example is the more used, in which we are forcing the value to have only 2 decimal places.
We can force it to have as many as we want, which will fill the value with zeros.
We can also choose the decimal point, and also the thousand separator.
Here, follows the code:

function numberFormat(num, dec, decPoint, thousandSep)
{
if(typeof num == ’string’)
{
if(num.indexOf(’,')>-1 && num.indexOf(’.')>-1)
{ // fixing strings
if(num.indexOf(’,') > num.indexOf(’.'))
{ // eg.: 10.000,000 => 10000.000
num= num.replace(’.', ”).replace(’,', ‘.’);
}else{ // eg.: 10,000.000 => 10000.000
num= num.replace(’,', ”);
}
}else{
if(num.indexOf(’,')>-1)
{ // eg.: 10000,000 => 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 < 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);
}

I hope it becomes useful to you.

Absurd error using Internet Explorer

No comments

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 friend IE culdn’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… yes, in lower-case.
Just the case changed the page itself.
I hope this post will help someone.


Pt:
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.

Two interesting JavaScript functions

No comments

Two interesting functions in JavaScript I’ve created due to “fix” a few problems I’ve fought against.
Both functins exist in PHP, and I needed to use’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 “one line” function to revert strings in javascript.

function strrev(str)
{
	return str.split('').reverse().join('');
}

We must use it like this:
var rev= strrev(”Felipe Nascimento”);

we also could have put it into the prototype of our strings:

String.prototype.invert= function(){
	return this.split('').reverse().join('');
}

In this case, the example would be:
“Felipe Nascimento”.invert();

Everything I did here, was just transform the string into an Array, revert it, and then transform it into a String again.

The second is a more complex function, but a quite useful one. It’s the number_format function, also inherited from PHP.
It’s signature is: numberFormat(num, dec, decPoint, thousandSep)
It receives a number or string, and returns it transformed by those patterns sent to it.
We can use it to change the decimal places(values after , or .), as it depends on the country parttener, and other detalis.
It’s usage is just like this:

alert(numberFormat('1050.1', 2, '.', ' '));
alert(numberFormat('10.50,1', 2, ',', '.'));
alert(numberFormat('10050.123456', 5, ',', ' '));
alert(numberFormat('10.50,1234', 3, '~', '.'));

The first example is the more used, in which we are forcing the value to have only 2 decimal places.
We can force it to have as many as we want, which will fill the value with zeros.
We can also choose the decimal point, and also the thousand separator.
Here, follows the code:

function numberFormat(num, dec, decPoint, thousandSep)
{
	if(typeof num == 'string')
	{
		if(num.indexOf(',')&amp;gt;-1 &amp;amp;&amp;amp; num.indexOf('.')&amp;gt;-1)
		{ // fixing strings
			if(num.indexOf(',') &amp;gt; num.indexOf('.'))
			{ // eg.: 10.000,000 =&amp;gt; 10000.000
				num= num.replace('.', '').replace(',', '.');
			}else{ // eg.: 10,000.000 =&amp;gt; 10000.000
					num= num.replace(',', '');
				 }
		}else{
				if(num.indexOf(',')&amp;gt;-1)
				{ // eg.: 10000,000 =&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;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);
}

I hope it becomes useful to you.