Ver Mensaje Individual
  #7 (permalink)  
Antiguo 31/03/2011, 03:01
jmsolerdenia
 
Fecha de Ingreso: enero-2011
Mensajes: 28
Antigüedad: 13 años, 3 meses
Puntos: 0
Respuesta: Como no perder valor de parámetros pasados a funciones javascript

Buenas de nuevo, ahora me ha surgido otro problema, os lo pongo a continuación. Como os he dicho antes, dependiendo del valor de p hago una cosa u otra, vale hasta ahí todo correcto. Lo que pasa es que ahora sólo me imprime una vez en el html dependiendo de p. Os pongo el código js y html para que lo veais.

Cita:
function leerBD(p){
alert(p);
var url = "ajax.php";
var params = "p="+p;
var http = new XMLHttpRequest();
var h3;
var pr;
var enlace;
var subbox;
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.

if(http.readyState == 4 && http.status == 200) {
p2 = p;
alert("yejee"+p2);
var registros=http.responseText;
alert(registros);
alert("arriba de if");

alert(p);
if(p==1){

alert("en if");
/*Declaraciones*/
h3 = document.createElement("h3");
h2 = document.createElement("h2");
pr = document.createElement("p");
enlace = document.getElementById("enlacesub1");
pr.innerHTML=registros;
var subbox = document.getElementById("subbox1");
subbox.appendChild(pr);
h_3.innerHTML = "Experimento 1";
enlace.parentNode.insertBefore(h3,enlace); //insertamos antes del enlace


}//if

if(p==2){
alert("en if");
/*Declaraciones*/
h3 = document.createElement("h3");
h2 = document.createElement("h2");
pr = document.createElement("p");
enlace = document.getElementById("enlacesub2");
pr.innerHTML=registros;
subbox = document.getElementById("subbox2");
subbox.appendChild(pr);
h3.innerHTML = "Experimento 2";
enlace.parentNode.insertBefore(h3,enlace); //insertamos antes del enlace
}

}

}
http.send(params);
}
y el código html

Cita:
<script>
leerBD(1);
leerBD(2);
</script>
entonces si p=1 me debería de imprimir en div concreto, si p=2 en otro, pero todo a la vez, el problema está en que sólo me imprime si p=2.

Muchas gracias