Foros del Web » Programando para Internet » Javascript »

Como se hace esto en Javascript?

Estas en el tema de Como se hace esto en Javascript? en el foro de Javascript en Foros del Web. Hola, yo en php compruebo que un campo tenga una condicion, si es asi, la imprimo, sino no po', jajaja pero no se hacerlo en ...
  #1 (permalink)  
Antiguo 20/02/2012, 08:50
 
Fecha de Ingreso: diciembre-2011
Ubicación: Chile
Mensajes: 36
Antigüedad: 12 años, 5 meses
Puntos: 2
Pregunta Como se hace esto en Javascript?

Hola, yo en php compruebo que un campo tenga una condicion, si es asi, la imprimo, sino no po', jajaja pero no se hacerlo en javascript y no se por donde buscar.

esto es lo que hago en php y necesito hacerlo en javascript.

<?php
if($variable1 !='') {
Echo '<li><img src="../imagenes/ico_absorbente_acustico.png" name="ico" width="96" height="36" id="ico" /></li>';
} ?>
  #2 (permalink)  
Antiguo 20/02/2012, 08:57
Avatar de caricatos
Moderador
 
Fecha de Ingreso: abril-2002
Ubicación: Torremolinos (Málaga)
Mensajes: 19.607
Antigüedad: 22 años
Puntos: 1284
Respuesta: Como se hace esto en Javascript?

Hola:

Seguro que para tu ejemplo sea más aconsejable php y pensar en esos condicionamientos con la página ya cargada... la sintaxis es igual, aunque con javascript no se suele usar el "$" para indicar que se trata de una variable...

Con javascript si escribes de forma similar sería usando document.write... pero no es aconsejable con la página cargada, porque esa instrucción escribe en el documento, y eso implica machacar el contenido existente... hay alternativas, el value en controles de formularios; innerHTML en los tags normales... con el DOM más puro usando appendChild + createTextNode...

Cuéntanos tu caso y te diremos las opciones (si no es un caso imposible)

Saludos
__________________
Por favor:
No hagan preguntas de temas de foros en mensajes privados... no las respondo
  #3 (permalink)  
Antiguo 20/02/2012, 09:39
 
Fecha de Ingreso: diciembre-2011
Ubicación: Chile
Mensajes: 36
Antigüedad: 12 años, 5 meses
Puntos: 2
Respuesta: Como se hace esto en Javascript?

hola, gracias por responder tan rapido, lo que pasa es que estoy haciendo una aplicacion en PHP+ JAVASCRIPT + APIv3 de GOOGLE MAP

Mediante la creación de un xml, saco los datos y coordenadas de las sucursales, estas sucursales tienen 19 atributos ( familias de productos que venden ). la creacion del xml la hago asi:

Código PHP:
Ver original
  1. <?php
  2. //conexion a la DB
  3.  
  4. // Start XML file, create parent node
  5.  
  6. $dom = new DOMDocument("1.0");
  7. $node = $dom->createElement("markers");
  8. $parnode = $dom->appendChild($node);
  9.  
  10.  
  11.  
  12. // Select all the rows in the markers table
  13.  
  14. $query = "SELECT * FROM tblsucursales WHERE 1";
  15. $result = mysql_query($query);
  16. //if (!$result) {  
  17. //  die('Invalid query: ' . mysql_error());
  18. //}
  19.  
  20. header("Content-type: text/xml");
  21.  
  22. // Iterate through the rows, adding XML nodes for each
  23.  
  24. $espacio= ' ';
  25.  
  26. while ($row = @mysql_fetch_assoc($result)){
  27.    
  28. $regg= Nombre_region_2($row['strRegion']) ;
  29. $segg= Nombre_Segmento($row['idSegmento']);
  30.      
  31.   // ADD TO XML DOCUMENT NODE  
  32.   $node = $dom->createElement("marker");  
  33.   $newnode = $parnode->appendChild($node);
  34.   $newnode->setAttribute("idd",$row['idSucursal']);  
  35.   $newnode->setAttribute("name",$row['strNombre']);
  36.   $newnode->setAttribute("address", $row['strDireccion'].$espacio.$row['strNumero']);
  37.   $newnode->setAttribute("comu", $row['strComuna']);  
  38.   $newnode->setAttribute("lat", $row['strLatitud']);  
  39.   $newnode->setAttribute("lng", $row['strLongitud']);
  40.   $newnode->setAttribute("ncon", $row['str_nombre_contacto']);
  41.   $newnode->setAttribute("econ", $row['str_email_contacto']);
  42.   $newnode->setAttribute("tcon", $row['str_telefono_contacto']);
  43.   $newnode->setAttribute("foto", $row['strFoto']);
  44.   $newnode->setAttribute("type", "sucursales");  
  45.   $newnode->setAttribute("regi", $regg);
  46.   $newnode->setAttribute("segm", $segg);
  47.   $newnode->setAttribute("dist", $row['nombre_distribuidor']);  
  48.   $newnode->setAttribute("atri01", $row['YC_Volcan']);
  49.   $newnode->setAttribute("atri02", $row['YC_Romeral']);
  50.   $newnode->setAttribute("atri03", $row['YC_Knauf']);
  51.   $newnode->setAttribute("atri04", $row['FC_Volcan']);
  52.   $newnode->setAttribute("atri05", $row['FC_Pizarreno']);
  53.   $newnode->setAttribute("atri06", $row['Ln_Volcan']);
  54.   $newnode->setAttribute("atri07", $row['Ln_Romeral']);
  55.   $newnode->setAttribute("atri08", $row['Ys_Volcan']);
  56.   $newnode->setAttribute("atri09", $row['Ys_Romeral']);
  57.   $newnode->setAttribute("atri10", $row['Tj_Volcan']);
  58.   $newnode->setAttribute("atri11", $row['Tj_Transaco']);
  59.   $newnode->setAttribute("atri12", $row['Ftro_Volcan']);
  60.   $newnode->setAttribute("atri13", $row['FC_Lizaflex']);
  61.   $newnode->setAttribute("atri14", $row['Ftro_Becton']);
  62.   $newnode->setAttribute("atri15", $row['Ftro_Dynal']);
  63.   $newnode->setAttribute("atri16", $row['Ftro_AsfalChile']);
  64.   $newnode->setAttribute("atri17", $row['Jp_Volcan']);
  65.   $newnode->setAttribute("atri18", $row['Jp_Solcrom']);
  66.   $newnode->setAttribute("atri19", $row['Jp_Romeral']);    
  67. }
  68.  
  69. echo $dom->saveXML();
  70. ?>

Desde al atri01 al atri19 son los atributos, pueden ser NULL o "vacio"(nada) y/o "On"

Luego los recojo en la pagina que crea el mapa:

Código PHP:
Ver original
  1. <?php
  2. //conexion a la DB
  3.  
  4. //html del header
  5.  
  6.  
  7.     //<![CDATA[
  8.  
  9.  
  10.     var customIcons = {
  11.       sucursales: {
  12.         icon: './imagenes/Pin.png',
  13.       }
  14.     };
  15.  
  16.  
  17.     function load() {
  18.  
  19.       var map = new google.maps.Map(document.getElementById("map"), {
  20.         <?php coor_region($_GET['region']);?>
  21.       mapTypeId: 'roadmap'
  22.       });
  23.  
  24.       var infoWindow = new google.maps.InfoWindow({
  25.           maxWidth: 500
  26.       });
  27.  
  28.       downloadUrl("crea_xml.php", function(data) {
  29.         var xml = parseXml(data);
  30.         var markers = xml.documentElement.getElementsByTagName("marker");
  31.  
  32.  
  33.       for (var i = 0; i < markers.length; i++)
  34.  
  35.             {
  36.                 var point = new google.maps.LatLng( parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
  37.                 var idd = markers[i].getAttribute("idd");
  38.                 var name = markers[i].getAttribute("name");
  39.                 var address = markers[i].getAttribute("address");
  40.                 var type = markers[i].getAttribute("type");
  41.                 var tcon = markers[i].getAttribute("tcon");
  42.                 var ncon = markers[i].getAttribute("ncon");
  43.                 var econ = markers[i].getAttribute("econ");
  44.                 var foto = markers[i].getAttribute("foto");
  45.                 var comu = markers[i].getAttribute("comu");
  46.                 var regi = markers[i].getAttribute("regi");
  47.                 var segm = markers[i].getAttribute("segm");
  48.                 var dist = markers[i].getAttribute("dist");            
  49.                 var dist = markers[i].getAttribute("atri01");
  50.                 var dist = markers[i].getAttribute("atri02");
  51.                 var dist = markers[i].getAttribute("atri03");
  52.                 var dist = markers[i].getAttribute("atri04");
  53.                 var dist = markers[i].getAttribute("atri05");
  54.                 var dist = markers[i].getAttribute("atri06");
  55.                 var dist = markers[i].getAttribute("atri07");
  56.                 var dist = markers[i].getAttribute("atri08");
  57.                 var dist = markers[i].getAttribute("atri09");
  58.                 var dist = markers[i].getAttribute("atri10");
  59.                 var dist = markers[i].getAttribute("atri11");
  60.                 var dist = markers[i].getAttribute("atri12");
  61.                 var dist = markers[i].getAttribute("atri13");
  62.                 var dist = markers[i].getAttribute("atri14");
  63.                 var dist = markers[i].getAttribute("atri15");
  64.                 var dist = markers[i].getAttribute("atri16");
  65.                 var dist = markers[i].getAttribute("atri17");
  66.                 var dist = markers[i].getAttribute("atri18");
  67.                 var dist = markers[i].getAttribute("atri19");
  68.                        
  69.  
  70.  
  71.                 // Comenzamos con el HTML
  72.  
  73.                 var html    = [
  74.  
  75. '<div style="width:400px; height:300px;">',            
  76. '<div id="tabs">',
  77. '<p id="dir_titulo">' + address + '<br />',
  78. '<h5>' + comu + ', ' + regi + '</h5></p><div class="linea"></div>',
  79. '<ul>',
  80. '<li><a href="#tabs-1"> General </a></li>',
  81. '<li><a href="#tabs-2"> Contacto </a></li>',
  82. '</ul>',
  83. <!--INICIO TAB_GENERAL-->
  84. '<div id="tabs-1">',
  85. '<div class="imagen">',
  86. '<img src="' + foto + '" width="148" height="107">',
  87. '</div>',
  88. '<h4>' + dist + ' / ' + segm +' </h4>',
  89. '<h2>' + name + '</h2><br>',
  90. <!--INICIO FAMILIAS_BOX-->
  91. '<div id="familias_box">',
  92. '<ul>',
  93.  
  94. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  95.  
  96. ACA NECESITO COMPROBAR LAS VARIABLES (ATRIxx) Y IMPRIMIR EN PANTALLA
  97.  
  98. SI LA VARIABLE NO ESTA VACIA, IMPRIMIR: '<li>Ftro_AsfalChile</li>', , SI ESTA VACIA, NO HACER NADA
  99.  
  100. /////////////////////////////////////////////////
  101. ATRIBUTOS ESTATICOS (CAMBIAR POR DINAMICOS)
  102.  
  103. '<li>Ftro_AsfalChile</li>',
  104. '<li>Ftro_Dynal</li>',
  105. '<li>FC_Pizarreno</li>',
  106. '</ul>',
  107. '</div>',
  108. <!--FIN FAMILIAS_BOX-->
  109. '</div>',
  110. <!--FIN TAB_GENERAL-->
  111. <!--INICIO TAB_CONTACTO-->
  112. '<div id="tabs-2">',
  113. '<div class="imagen">',
  114. '<img src="' + foto + '" width="148" height="107">',
  115. '</div>',
  116. '<h4>' + name + '</h4>',
  117. '<h2>Información de Contacto</h2><br>',
  118. '<div id="familias_box">',
  119. '<ul>',
  120. '<li id="contacto">Encargado:</strong> ' + ncon + ' </li><br>',
  121. '<li id="contacto">Telefono :</strong> ' + tcon + ' </li><br>',
  122. '<li id="contacto">Email:</strong><a href="mailto:' + econ+ '"> ' + econ + '</a></li>',
  123. '</div>',
  124. '</div>',
  125. <!--FIN TAB_CONTACTO-->
  126. '</div>',
  127. '</div>'
  128.  
  129.                             ].join('');    
  130.  
  131.  
  132.                 var icon    = customIcons[type] || {};
  133.                 var marker = new google.maps.Marker(
  134.                         {
  135.                     map: map,
  136.                     position: point,
  137.                     icon: icon.icon,
  138.                     title: address + ', ' + comu,
  139.  
  140.                 });
  141.  
  142.                 bindInfoWindow(marker, map, infoWindow, html);                   
  143.  
  144.          }
  145.  
  146.       });
  147.     }
  148.  
  149.  
  150. function bindInfoWindow(marker, map, infoWindow, html) {
  151.   google.maps.event.addListener(marker, 'click', function() {
  152.     google.maps.event.addListenerOnce(infoWindow, 'domready', function() {
  153.       $("#tabs").tabs();
  154.     });
  155.     infoWindow.open(map, marker);
  156.     infoWindow.setContent(html);
  157.   });
  158. }
  159.  
  160.  
  161.     function downloadUrl(url, callback) {
  162.       var request = window.ActiveXObject ?
  163.           new ActiveXObject('Microsoft.XMLHTTP') :
  164.           new XMLHttpRequest;  
  165.  
  166.  
  167.  
  168.       request.onreadystatechange = function() {
  169.         if (request.readyState == 4) {
  170.           request.onreadystatechange = doNothing;
  171.           callback(request.responseText, request.status);
  172.         }
  173.  
  174.       };
  175.  
  176.  
  177.       request.open('GET', url, true);
  178.       request.send(null);
  179.     }
  180.  
  181.  
  182.  
  183.     function parseXml(str) {
  184.       if (window.ActiveXObject) {
  185.         var doc = new ActiveXObject('Microsoft.XMLDOM');
  186.         doc.loadXML(str);
  187.         return doc;
  188.  
  189.       } else if (window.DOMParser) {
  190.         return (new DOMParser).parseFromString(str, 'text/xml');
  191.       }
  192.  
  193.  
  194.     }
  195.  
  196.     function doNothing() {}
  197.  
  198.     //]]>
  199.  
  200.  
  201.   </script>

Eso es lo que tengo, puse en MAYUSCULAS donde debe hacerce lo que necesito, para que me avisen en caso de tener alguna precaucion extra ( esta api ya me tiene loco @_@)

saludos

Etiquetas: php
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 15:08.