Archive for the ‘Javascript’ Category

When a server asks for help!

Saturday, September 26th, 2009

So it’s been a couple of days since the last post but I’ve been occupied with Nsteins WCM, customer support and server maintenance.

This post has to do with server maintenance and the proper diagnosis of problems.

About a month ago one of our most important customers called arguing that their web page was “down”. I’m no longer working full time for that customer but because I’m basically the server admin the ball was thrown to me.

Not all the site was down, it was a subdomain wich was aquired to a third party because they did such a great job with a project. So the company I work for was given the task to improve and mantain the site. Since the guy that created the site was proficient with Javascript and not that good with PHP he decided to code the whole site in Javascript and deal with the database through simple PHP scripts (right! AJAX).

He even coded a javascript obfuscator! He’s a man in his old 40’s so I was really impressed.

Anyway there’s this tiny minimum html code and then a HUGE obfuscated Javascript code.

Since the former programmer wasn’t really a programmer he coded the stuff to work forgeting about efficiency, security and errors management. Being the lack of errors management the issue that made me go bananas for like a week.

The site was displaying just HTML code, anything related to databases was blank. No error messages, not a clue of what was happening. So my coleagues asked me to take a look at the server and so I did.

First thing I checked were the files then the permissions. After that I went to php.ini and enabled error messages. Then I got this error saying that the script weren’t able to connect to the database, cool! this looked like an easy task!

Typed top, view the server status and realized MySQL was down but trying to start. Why wasn’t it starting?

I manually tried to start MySQL and got an error that after a couple of minutes I realized means the server HD is full. It was a 70 Gb HD so what was growing so fast?

Went to mysql data folder and realized a table was using 17Gb, ok still I’m missing lots of Gb so i started to look for very big files playing with find:

find / -type f -size +1000000k -exec ls -lh {} \;

I got a huge list and saw a couple of access log files related to apache. All of them adding a total of approx 12Gb. So I went and deleted them and left one so Apache could use it.

Everything went fine for a couple of weeks and then it happened again and I did the same thing I did before: delete the files and restart MySQL.

A week ago happened the same and also yesterday so I thought the database was growing really fast and indeed it was taking all the disk’s free space so I went ando told that to my boss and made a petition to get another disk up ASAP.

Just today the server went down again! I deleted the files and freed 2 Gb, a couple of hours later it went down again! WTF! something weird is happening! the database couldn’t grow so fast so I took a dive in the server searching again for long files and ther it was! a HUGE log file (25Gb) called mysqld.log…..

So stupid me read the last 50 lines and became aware that a table was damaged and needed to be repaired… the whole 25 Gb were the same message!!!! To make it better the damaged table size was 17Gb… 😮

repair table [table];

After a couple of hours I got a message indicating the table was repaired, reduced the log file size to cero and gave me a hug, everything was ok. Later that day just checked the logs again and surprise! they were empty. The thing is that the damaged table didn’t stored any onfo for about a month…. Next time the first thing I’ll check will be logs.

If a server asks for help this is how it’ll talk to me. Rookie no more hahahaha

Javascript events & parameters

Monday, August 24th, 2009

Having learned the right way to register events to an object with javascript lead me to another couple of issues.
First I created a wrapper for the event handler so I might be able to manage both IE and FF, I based the wrapper on this.
The wrapper call ended like this:


The problem here was, what if the function parameter needs parameters??? I thought it was a big deal but after making a couple of Google searches I found that I could wrapp this function.

Let me put an example:

<script language=”javascript” type=”text/javascript”>
//an example function that doesn’t need parameters
function popAlert() {
alert(‘yadda yadda’);
addEventListener(obj,’click’,popAlert); //whenever obj gets clicked function popAlert will be executed



This was no problem at all works perfect but there are some cases where the function called needs parameters. how could I do that?! I tried the following:


WRONG! didn’t work so I was like about to give up when I found I could do something like this:


This actually works so now I may pass parameters to any function I register to an event!

In the case I needed to pass an actual event the function would end up looking like this:


Note: This also applies for IE attachEvent.

Give it a try!

Javascript popups

Monday, August 24th, 2009

Ok, I know the thing is not new, everybody knows what the following line does:,name,properties);

But there are some cases when you really need more from these simple line. We know it opens a new window but there are a few things we must ask ourselves 2 see if we should just leave it like that.
A couple of weeks ago I was very happy coding an application. Some screens required to open a popup, this was very simple. Every now and then I would simply add the previous line of code and it was all done. Hell broke when the customer told me: “U know what? I need that if I close the main window then all child windows should close.”
Damn! I never thought the customer would want 2 do something like this! anyway there were lots of lines of code that needed to b modified.
My first approach was really simple. Change the above line of code for something like this:

var my_window =,name,properties);

So now everytime I closed the main window I would call this function:


Cool! everything is alright now…. wrong!!!! some screens had like 10 popups so coding this was boring and was taking time. So I came up with a great solution based on a cool, simple script found here.
The thing is that I created a class that would wrapp the window open and close methods. With this class I wouldn’t have to write 1 window.close for each window since one of the method closes every popup created with it.
Here it is, happy coding!!!!

<script language=”javascript” type=”text/javascript”>
function objVentanaEmergente () {
this.ventanas = new Object;
this.objetos = new Array;

this.abre = function (ventana,url,nombre,propiedades) {
if (typeof(this.ventanas) == ‘object’) {
if (typeof(this.ventanas[ventana]) == ‘undefined’) {
this.ventanas[ventana] =,nombre,propiedades);
if (!this.ventanas[ventana].opener) {
this.ventanas[ventana].opener = self;
else {
this.ventanas[ventana].location.href = url;
if (window.focus) {
return false;

this.quitaDeArreglo = function (ventana) {

var auxiliar = 0;
var cuantos = this.objetos.length;
var arreglo_auxiliar = new Array;
if ( cuantos > 0 ) {
for ( auxiliar = 0; auxiliar <>
if (this.objetos[auxiliar] != ventana) {
this.objetos = new Array;
this.objetos = arreglo_auxiliar;

this.cierra = function (ventana) {
if (typeof(this.ventanas) == ‘object’) {
if (typeof(this.ventanas[ventana]) != ‘undefined’) {
return false;

this.cierraTodas = function () {
var auxiliar = 0;
while (this.objetos.length > 0) {
this.objetos = new Array;
this.ventanas = new Object;;

var ventanas = new objVentanaEmergente();


document.getElementById(‘cierra’).onclick = function () {

document.getElementById(‘cierrat’).onclick = function () {

Extendiendo las propiedades de los objetos en Javascript

Monday, August 24th, 2009

Como muchos sabemos Javascript (que no es lo mismo que Java) es un lenguaje que está muy presente por la aparición de Ajax(Asynchronous Javascript & XML).
Para los proramadores web es un lenguaje muy sencillo. Realmente la complejidad aparece en los exploradores pues cada uno implementa Javascript de una forma diferente. Aquí es donde me ha dado serios problemas.
Mi idea es poder extender las funcionalidades de ciertos objetos al agregar métodos y propiedades. Navegando por internet encuentro que es posible cualquiera de las siguientes maneras.
Para una propiedad o método:
Object.prototype.propiedad1 = 10;
Object.propiedad1 = 10;
Object[“propiedad1″] = 10;
Sucede que pruebo esto en FF y funciona increiblemente bien. La gran desilusión viene cuando se intenta implementar en IE.
Sucede que en FF todos los elementos son heredados de la clase Object y al agregar un método/propiedad a esa clase pues los elementos la heredan automáticamente. Pero esto no ocurre en IE, esto aún cuando al debuguear se encuentra que, por decir un ejemplo, el elemento input type=”text” hereda de object.
¿Cómo ver esto?

<script language=”javascript” type=”text/javascript”>
alert(typeof(document.getElementById(‘[id del objeto]’));

En ambos casos nos dice ‘object’.
¿Cómo implementar entonces métodos/propiedades a todos los objetos HTML en IE?
En cuanto encuentre la maldita forma publico mi respuesta.
Ahora bien agradecería si alguien tiene algo que comentar.