Foros del Web » Programando para Internet » Javascript »

Problema con el JS y el DOCTYPE

Estas en el tema de Problema con el JS y el DOCTYPE en el foro de Javascript en Foros del Web. Tengo este JS, lo curioso es que cuando quito el Doctype ya funciona el Script Código HTML: <SCRIPT TYPE= "text/javascript" > if(!window.Halloween) Halloween=new Object(); Halloween.ghostImages ...
  #1 (permalink)  
Antiguo 21/08/2011, 12:30
Avatar de djboro88  
Fecha de Ingreso: junio-2009
Ubicación: Valencia
Mensajes: 180
Antigüedad: 14 años, 10 meses
Puntos: 2
Pregunta Problema con el JS y el DOCTYPE

Tengo este JS, lo curioso es que cuando quito el Doctype ya funciona el Script

Código HTML:
 <SCRIPT TYPE="text/javascript">

if(!window.Halloween) Halloween=new Object();
Halloween.ghostImages = new Array( 
	"<img src='boo.gif'>",
	"<img src='boo2.gif'>",
	"<img src='boo.gif'>",
	"<img src='boo2.gif'>"
);
var ns4 = document.layers;
var ie4 = document.all;
Halloween.makeLayer = function(id)
{
	var el = 	document.getElementById	? document.getElementById(id) :
			document.all 		? document.all[id] :
							  document.layers[id];
	if(ns4) el.style=el;
	el.sP=function(x,y){this.style.left = x;this.style.top=y;};
	el.show=function(){ this.style.visibility = "visible"; } 
	el.hide=function(){ this.style.visibility = "hidden"; } 
	if(ns4 || window.opera) 
		el.sO = function(pc){return 0;};
	else if(ie4)
		el.sO = function(pc)
		{
			if(this.style.filter=="")
				this.style.filter="alpha(opacity=100);";
			this.filters.alpha.opacity=pc;
		}
	else
		el.sO = function(pc){this.style.MozOpacity=pc/100;}

	return el;
}

if(window.innerWidth)
{
	gX=function(){return innerWidth;};
	gY=function(){return innerHeight;};
}
else
{
	gX=function(){return document.body.clientWidth-30 + "px";};
	gY=function(){return document.body.clientHeight-30 + "px";};
}
Halloween.ghostOutput=function()
{
	for(var i=0 ; i<Halloween.ghostImages.length ; i++)
		document.write(ns4 ? "<LAYER  NAME='gh"+i+"'>"+Halloween.ghostImages[i]+"</LAYER>" : 
					   "<DIV id='gh"+i+"' style='position:absolute'>"+Halloween.ghostImages[i]+"</DIV>" );
	
}
Halloween.ghostSprites = new Array();
Halloween.ghostStartAni = function()
{
	for(var i=0 ;i<Halloween.ghostImages.length;i++)
	{
		var el=Halloween.makeLayer("gh"+i);
		el.x=Math.random()*gX();
		el.y=Math.random()*gY();
		el.tx=Math.random()*gX();
		el.ty=Math.random()*gY();
		el.dx=-5+Math.random()*10;
		el.dy=-5+Math.random()*10;
		el.state="off";
		el.op=0;
		el.sO(el.op);
		el.hide();
		Halloween.ghostSprites[i] = el;
	}
	setInterval("Halloween.ghostAni()", 40);
}
Halloween.ghostAni = function()
{
	for(var i=0 ;i<Halloween.ghostSprites.length;i++)
	{
		el=Halloween.ghostSprites[i];

		if(el.state == "off")
		{
			if(Math.random() > .99)
			{
				el.state="up";
				el.show();
			}
		}
		else if(el.state == "on")
		{
			if(Math.random() > .98)
				el.state="down";
		}
		else if(el.state == "up")
		{
			el.op += 2;
			el.sO(el.op);
			if(el.op==100)
				el.state = "on";
		}
		else if(el.state == "down")
		{
			el.op -= 2;
			if(el.op==0)
			{
				el.hide();
				el.state = "off";
			}
			else
				el.sO(el.op);
		}

		var X = (el.tx - el.x);
		var Y = (el.ty - el.y);
		var len = Math.sqrt(X*X+Y*Y);
		if(len < 1) len = 1;
		var dx = 20 * (X/len);
		var dy = 20 * (Y/len);
		var ddx = (dx - el.dx)/10;
		var ddy = (dy - el.dy)/10;
		el.dx += ddx;
		el.dy += ddy;
		el.sP(el.x+=el.dx,el.y+=el.dy);

		if(Math.random() >.95 )
		{
			el.tx = Math.random()*gX();
			el.ty = Math.random()*gY();
		}

	}
}
Halloween.ghostStart = function()
{
	if(Halloween.ghostLoad)Halloween.ghostLoad();
	Halloween.ghostStartAni();
}
Halloween.ghostOutput();
Halloween.ghostLoad=window.onload;
window.onload=Halloween.ghostStart;

</SCRIPT> 
¿Alguna idea de como hacerlo funcionar con el doctype?

Saludos y Gracias !!
  #2 (permalink)  
Antiguo 21/08/2011, 12:36
Avatar de zerokilled
Javascripter
 
Fecha de Ingreso: abril-2009
Ubicación: Isla del Encanto, La Borinqueña [+>==]
Mensajes: 8.050
Antigüedad: 15 años
Puntos: 1485
Respuesta: Problema con el JS y el DOCTYPE

buenas,
trata de dar todos los datos posibles. ¿cuál doctype estas usando?
__________________
la maldad es una virtud humana,
y la espiritualidad es la lucha del hombre contra su maldad.
  #3 (permalink)  
Antiguo 21/08/2011, 13:11
Avatar de djboro88  
Fecha de Ingreso: junio-2009
Ubicación: Valencia
Mensajes: 180
Antigüedad: 14 años, 10 meses
Puntos: 2
Respuesta: Problema con el JS y el DOCTYPE

Este es...

Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  #4 (permalink)  
Antiguo 21/08/2011, 13:17
Avatar de zerokilled
Javascripter
 
Fecha de Ingreso: abril-2009
Ubicación: Isla del Encanto, La Borinqueña [+>==]
Mensajes: 8.050
Antigüedad: 15 años
Puntos: 1485
Respuesta: Problema con el JS y el DOCTYPE

¿lo sirves como text/html o como application/xhtml+xml? ¿en cuál navegador te falla?
__________________
la maldad es una virtud humana,
y la espiritualidad es la lucha del hombre contra su maldad.
  #5 (permalink)  
Antiguo 21/08/2011, 13:44
Avatar de djboro88  
Fecha de Ingreso: junio-2009
Ubicación: Valencia
Mensajes: 180
Antigüedad: 14 años, 10 meses
Puntos: 2
Respuesta: Problema con el JS y el DOCTYPE

Hola...

Como text/html
Utilizo Firefox 6
  #6 (permalink)  
Antiguo 21/08/2011, 13:57
Avatar de zerokilled
Javascripter
 
Fecha de Ingreso: abril-2009
Ubicación: Isla del Encanto, La Borinqueña [+>==]
Mensajes: 8.050
Antigüedad: 15 años
Puntos: 1485
Respuesta: Problema con el JS y el DOCTYPE

pues te comento que no he hecho absolutamente nada. tan solo copie el script, declare el documento como xhtml transitional, y cargue el documento como text/html. el script esta funcionando bien al menos en firefox.
__________________
la maldad es una virtud humana,
y la espiritualidad es la lucha del hombre contra su maldad.
  #7 (permalink)  
Antiguo 22/08/2011, 00:18
Avatar de djboro88  
Fecha de Ingreso: junio-2009
Ubicación: Valencia
Mensajes: 180
Antigüedad: 14 años, 10 meses
Puntos: 2
Respuesta: Problema con el JS y el DOCTYPE

Estuve leyemdo por foros que añadiendo el tipo de valor funcionaba, es decir, del script

Código HTML:
<SCRIPT TYPE="text/javascript">

if(!window.Halloween) Halloween=new Object();
Halloween.ghostImages = new Array( 
	"<img src='boo.gif'>",
	"<img src='boo2.gif'>",
	"<img src='boo.gif'>",
	"<img src='boo2.gif'>"
);
var ns4 = document.layers;
var ie4 = document.all;
Halloween.makeLayer = function(id)
{
	var el = 	document.getElementById	? document.getElementById(id) :
			document.all 		? document.all[id] :
							  document.layers[id];
	if(ns4) el.style=el;
	el.sP=function(x,y){this.style.left = x;this.style.top=y;};
	el.show=function(){ this.style.visibility = "visible"; } 
	el.hide=function(){ this.style.visibility = "hidden"; } 
	if(ns4 || window.opera) 
		el.sO = function(pc){return 0;};
	else if(ie4)
		el.sO = function(pc)
		{
			if(this.style.filter=="")
				this.style.filter="alpha(opacity=100);";
			this.filters.alpha.opacity=pc;
		}
	else
		el.sO = function(pc){this.style.MozOpacity=pc/100;}

	return el;
}

if(window.innerWidth)
{
	gX=function(){return innerWidth;};
	gY=function(){return innerHeight;};
}
else
{
	gX=function(){return document.body.clientWidth-30 + "px";};
	gY=function(){return document.body.clientHeight-30 + "px";};
}
Halloween.ghostOutput=function()
{
	for(var i=0 ; i<Halloween.ghostImages.length ; i++)
		document.write(ns4 ? "<LAYER  NAME='gh"+i+"'>"+Halloween.ghostImages[i]+"</LAYER>" : 
					   "<DIV id='gh"+i+"' style='position:absolute'>"+Halloween.ghostImages[i]+"</DIV>" );
	
}
Halloween.ghostSprites = new Array();
Halloween.ghostStartAni = function()
{
	for(var i=0 ;i<Halloween.ghostImages.length;i++)
	{
		var el=Halloween.makeLayer("gh"+i);
		el.x=Math.random()*gX();
		el.y=Math.random()*gY();
		el.tx=Math.random()*gX();
		el.ty=Math.random()*gY();
		el.dx=-5+Math.random()*10;
		el.dy=-5+Math.random()*10;
		el.state="off";
		el.op=0;
		el.sO(el.op);
		el.hide();
		Halloween.ghostSprites[i] = el;
	}
	setInterval("Halloween.ghostAni()", 40);
}
Halloween.ghostAni = function()
{
	for(var i=0 ;i<Halloween.ghostSprites.length;i++)
	{
		el=Halloween.ghostSprites[i];

		if(el.state == "off")
		{
			if(Math.random() > .99)
			{
				el.state="up";
				el.show();
			}
		}
		else if(el.state == "on")
		{
			if(Math.random() > .98)
				el.state="down";
		}
		else if(el.state == "up")
		{
			el.op += 2;
			el.sO(el.op);
			if(el.op==100)
				el.state = "on";
		}
		else if(el.state == "down")
		{
			el.op -= 2;
			if(el.op==0)
			{
				el.hide();
				el.state = "off";
			}
			else
				el.sO(el.op);
		}

		var X = (el.tx - el.x);
		var Y = (el.ty - el.y);
		var len = Math.sqrt(X*X+Y*Y);
		if(len < 1) len = 1;
		var dx = 20 * (X/len);
		var dy = 20 * (Y/len);
		var ddx = (dx - el.dx)/10;
		var ddy = (dy - el.dy)/10;
		el.dx += ddx;
		el.dy += ddy;
		el.sP(el.x+=el.dx,el.y+=el.dy);

		if(Math.random() >.95 )
		{
			el.tx = Math.random()*gX();
			el.ty = Math.random()*gY();
		}

	}
}
Halloween.ghostStart = function()
{
	if(Halloween.ghostLoad)Halloween.ghostLoad();
	Halloween.ghostStartAni();
}
Halloween.ghostOutput();
Halloween.ghostLoad=window.onload;
window.onload=Halloween.ghostStart;

</SCRIPT> 
Añado +"px" a los siguientes...
Código HTML:
if(ns4) el.style=el;
	el.sP=function(x,y){this.style.left = x;this.style.top=y +"px";};
	el.show=function(){ this.style.visibility = "visible" +"px"; } 
	el.hide=function(){ this.style.visibility = "hidden" +"px"; }
Se mueven arriba y abajo (eje Y), pero solo en el lateral izquierdo, no se mueven libremente.

No se si me falta añadir algo más...

Etiquetas: doctype, fantasmas, volando
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:53.