Javascript popups

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

window.open(url,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 = window.open(url,name,properties);

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

my_window.close();

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] = window.open(url,nombre,propiedades);
this.objetos.push(ventana);
if (!this.ventanas[ventana].opener) {
this.ventanas[ventana].opener = self;
}
}
else {
this.ventanas[ventana].location.href = url;
}
if (window.focus) {
this.ventanas[ventana].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) {
arreglo_auxiliar.push(this.objetos[auxiliar]);
}
}
this.objetos = new Array;
this.objetos = arreglo_auxiliar;
}
};

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

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

var ventanas = new objVentanaEmergente();

ventanas.abre(‘mi_ventana’,’http://google.com’,’_blank’,’height=200,width=150′);
ventanas.abre(‘otra_ventana’,’http://yahoo.com’,’_blank’,’height=200,width=150′);

document.getElementById(‘cierra’).onclick = function () {
ventanas.cierra(‘mi_ventana’);
};

document.getElementById(‘cierrat’).onclick = function () {
ventanas.cierraTodas();
};
–>
</script>

Tags: , ,

Comments are closed.