Buscar este blog

Mostrando entradas con la etiqueta DWEC. Mostrar todas las entradas
Mostrando entradas con la etiqueta DWEC. Mostrar todas las entradas

viernes, 11 de octubre de 2013

FUNCIONES JAVASCRIPT (i)

setAttribute--> Pone un atributo a un Nodo.
nodo.setAttribute("nombre atributo", atributo);


removeChild--> Borra un hijo a un Nodo.

nodo.removeChild(nombre del hijo a borrar);
function equis(){
            var dibujo = document.getElementById("ce");
            var hijo = document.getElementById("imagenCentral");
            dibujo.removeChild(hijo);
            alert("equis");


quirksmode.org/dom/core

martes, 1 de octubre de 2013

condition ? true: false;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>condition ? true: false;</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Geany 0.21" />
</head>
<body>
    <script>
    /*    var a=7;
        if (a==7){
            b = a;
        }
        else{
            b = 3;
        }
        //condition ? true: false;*/
        (a == 7) ? b =a: b=3;
        alert(b);
    </script>
</body>

</html>

viernes, 27 de septiembre de 2013

TRABAJAR CON JAVASCRIPT CON CSS

<!--COMO CAMBIAR UN CSS POR OTRO DISTINTO-->

<!DOCTYPE>
<html>
<head>
    <title>The best site ever</title>
    <link rel="stylesheet" href="css270913.css" id="css1" type="text/css" media=screen>
</head>

<body>
    <script>
    var mysection = document.createElement("section");
    mysection.id="PERRO";
    document.body.appendChild(mysection);
   
    var myheader = document.createElement("header");
    mysection.appendChild(myheader);
   
            var myH1 = document.createElement("h1");
            var textNode1 = document.createTextNode ("The best site ever");
            myH1.appendChild(textNode1);
            myheader.appendChild(myH1);
           
            var mydiv =document.createElement("div");
            mydiv.id="column1";
            var textNode2 = document.createTextNode ("just adding some content");
            mydiv.appendChild(textNode2);
            mysection.appendChild(mydiv);
           
                var myH2 = document.createElement("h2");
                var textNode3 = document.createTextNode ("well");
                myH2.appendChild(textNode3);
                mydiv.appendChild(myH2);
               
                    var myPe = document.createElement("p");
                    var textNode4 = document.createTextNode ("This was not so easy");
                    myPe.appendChild(textNode4);
                    myH2.appendChild(myPe);
       
        var mydiv2 =document.createElement("div");
        mydiv2.id="column2";
        mysection.appendChild(mydiv2);
       
        var mydiv3 =document.createElement("div");
        mydiv3.id="column3";
        mysection.appendChild(mydiv3);
       
        var divlimpiar=document.createElement("div");
        divlimpiar.id="limpiar";
        mysection.appendChild(divlimpiar);
       
        /*var aNode = document.body.children[1]
        var another = aNode.cloneNode(); //estamos clonando el Div entero
        document.body.appendChild(another);*/
       
        function changecss2() {
        document.getElementById('css1').href="css270913b.css";
        }
        function changecss1() {
        document.getElementById('css1').href="css270913.css";
        }
    </script>
    <button onclick="changecss2()">CHANGE TO CSS2!!</button>   
    <button onclick="changecss1()">CHANGE TO CSS1!!</button>   
       
</body>

</html>

css270913.css

#column1{
    width:200px;
    height:400px;
    float:left;
    background-color:#9a9;
}
#column2{
    width:200px;
    height:400px;   
    float:left;
    background-color:#a99;
}
#column3{
    width:200px;
    height:400px;
    float:left;
    background-color:#99a;
}
#limpiar{
    clear:both;

css270913b.css
#column1{
    width:100px;
    height:100px;
    float:left;
    background-color:#9a9;
}
#column2{
    width:200px;
    height:200px;   
    float:left;
    background-color:#a99;
}
#column3{
    width:400px;
    height:400px;
    float:left;
    background-color:#99a;
}
#limpiar{
    clear:both;

____________________________________________________________________




<!--COMO AGREGAR UN ESTILO CSS CON JAVASCRIPT-->

<!DOCTYPE>
<html>
<head>
    <title>The best site ever</title>
    <link rel="stylesheet" href="css270913.css" id="css1" type="text/css" media=screen>
</head>

<body>
    <script>
    var mysection = document.createElement("section");
    mysection.id="PERRO";
    document.body.appendChild(mysection);
   
    var myheader = document.createElement("header");
    mysection.appendChild(myheader);
   
            var myH1 = document.createElement("h1");
            var textNode1 = document.createTextNode ("The best site ever");
            myH1.appendChild(textNode1);
            myheader.appendChild(myH1);
           
            var mydiv =document.createElement("div");
            mydiv.id="column1";
            var textNode2 = document.createTextNode ("just adding some content");
            mydiv.appendChild(textNode2);
            mysection.appendChild(mydiv);
           
                var myH2 = document.createElement("h2");
                var textNode3 = document.createTextNode ("well");
                myH2.appendChild(textNode3);
                mydiv.appendChild(myH2);
               
                    var myPe = document.createElement("p");
                    var textNode4 = document.createTextNode ("This was not so easy");
                    myPe.appendChild(textNode4);
                    myH2.appendChild(myPe);
       
        var mydiv2 =document.createElement("div");
        mydiv2.id="column2";
        mysection.appendChild(mydiv2);
       
        var mydiv3 =document.createElement("div");
        mydiv3.id="column3";
        mysection.appendChild(mydiv3);
       
        var divlimpiar=document.createElement("div");
        divlimpiar.id="limpiar";
        mysection.appendChild(divlimpiar);
       
        /*var aNode = document.body.children[1]
        var another = aNode.cloneNode(); //estamos clonando el Div entero
        document.body.appendChild(another);*/
       
        function changecss2() {
        document.getElementById('css1').href="css270913b.css";
        }
       
        function changecss1() {
        document.getElementById('css1').href="css270913.css";
        }
        function addcss(css){
           
            var cssnuevo= document.createElement("link");
            cssnuevo.setAttribute("rel", "stylesheet");
            cssnuevo.setAttribute("type", "text/css");
            cssnuevo.setAttribute("href", css);
            document.head.appendChild(cssnuevo);
           
           
            }
    </script>
    <button onclick="changecss2()">CHANGE TO CSS2!!</button>   
    <button onclick="changecss1()">CHANGE TO CSS1!!</button>   
    <button onclick="addcss('cssii.css')">ADD CSS!!</button>       
</body>

</html>


cssii.css

*{
 
     background-color:#C8FE2E;
}

jueves, 26 de septiembre de 2013

CREAR PAGINA HTML CON JAVASCRIPT

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>CREAR PAGINA CON SCRIPTS 26 septiembre 2013</title>
   
</head>

<body>
    <script>
    var myH1 = document.createElement("h1");
    var textNode1 = document.createTextNode ("An HTML Document");
    myH1.appendChild(textNode1);
    document.body.appendChild(myH1);
   
    var myP = document.createElement("p");
    var textNode2 = document.createTextNode ("This is a");
    myP.appendChild(textNode2);
    document.body.appendChild(myP);
   
   
   
    var textNode3 = document.createTextNode (" simple ");
    var textNode4 = document.createTextNode ("document");
   
    var myI = document.createElement ("i");
    myI.appendChild(textNode3);
   
    myP.appendChild(myI);
    myP.appendChild(textNode4);

    </script>
</body>

</html>


_________________________________________________________________
EJERCICIO 2:


<!DOCTYPE>
<html>
<head>
    <title>The best site ever</title>
<style>
#column1{
    width:200px;
    height:400px;
    float:left;
    background-color:#9a9;
}
#column2{
    width:200px;
    height:400px;   
    float:left;
    background-color:#a99;
}
#column3{
    width:200px;
    height:400px;
    float:left;
    background-color:#99a;
}
</style>
</head>

<body>
    <script>
    var mysection = document.createElement("section");
    document.body.appendChild(mysection);
   
    var myheader = document.createElement("header");
    mysection.appendChild(myheader);
   
            var myH1 = document.createElement("h1");
            var textNode1 = document.createTextNode ("The best site ever");
            myH1.appendChild(textNode1);
            myheader.appendChild(myH1);
           
            var mydiv =document.createElement("div");
            mydiv.id="column1";
            var textNode2 = document.createTextNode ("just adding some content");
            mydiv.appendChild(textNode2);
            myheader.appendChild(mydiv);
           
                var myH2 = document.createElement("h2");
                var textNode3 = document.createTextNode ("well");
                myH2.appendChild(textNode3);
                mydiv.appendChild(myH2);
               
                    var myPe = document.createElement("p");
                    var textNode4 = document.createTextNode ("This was not so easy");
                    myPe.appendChild(textNode4);
                    myH2.appendChild(myPe);
       
        var mydiv2 =document.createElement("div");
        mydiv2.id="column2";
        myheader.appendChild(mydiv2);
       
        var mydiv3 =document.createElement("div");
        mydiv3.id="column3";
        myheader.appendChild(mydiv3);
    </script>
</body>

</html>

 ________________________________________________________________________________

EJERCICIO 3 (duplicar página):

<!DOCTYPE>
<html>
<head>
    <title>The best site ever</title>
<style>
#column1{
    width:200px;
    height:400px;
    float:left;
    background-color:#9a9;
}
#column2{
    width:200px;
    height:400px;   
    float:left;
    background-color:#a99;
}
#column3{
    width:200px;
    height:400px;
    float:left;
    background-color:#99a;
}
#limpiar{
    clear:both;
}
</style>
</head>

<body>
    <script>
    var mysection = document.createElement("section");
    mysection.id="PERRO";
    document.body.appendChild(mysection);
   
    var myheader = document.createElement("header");
    mysection.appendChild(myheader);
   
            var myH1 = document.createElement("h1");
            var textNode1 = document.createTextNode ("The best site ever");
            myH1.appendChild(textNode1);
            myheader.appendChild(myH1);
           
            var mydiv =document.createElement("div");
            mydiv.id="column1";
            var textNode2 = document.createTextNode ("just adding some content");
            mydiv.appendChild(textNode2);
            mysection.appendChild(mydiv);
           
                var myH2 = document.createElement("h2");
                var textNode3 = document.createTextNode ("well");
                myH2.appendChild(textNode3);
                mydiv.appendChild(myH2);
               
                    var myPe = document.createElement("p");
                    var textNode4 = document.createTextNode ("This was not so easy");
                    myPe.appendChild(textNode4);
                    myH2.appendChild(myPe);
       
        var mydiv2 =document.createElement("div");
        mydiv2.id="column2";
        mysection.appendChild(mydiv2);
       
        var mydiv3 =document.createElement("div");
        mydiv3.id="column3";
        mysection.appendChild(mydiv3);
       
        var divlimpiar=document.createElement("div");
        divlimpiar.id="limpiar";
        mysection.appendChild(divlimpiar);
       
        var aNode = document.body.children[1]
        var another = aNode.cloneNode(); //estamos clonando el Div entero
        document.body.appendChild(another);
    </script>
</body>

</html>

WORKING WITH NODES

VARIABLES AND SCOPE:

<!DOCTYPE>
<html>

<head>
 <title>Variables and Scope of the Variables</title>
 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<body>
 
<header>
<h1>
 Variables and Scope of the Variables
</h1>
</header>
Hello

<script>
 var global_es;
    function test(){
        var message = "HOLA"; //LOCAL variable (because of the var)
        alert(message);
        mensaje2 = "OTRO"; //GLOBAL VARIABLE
    }

    test();
    
    alert(mensaje2);
    var message3 = 6;
    function pr(){
  alert(message3);  
 }
 
    alert(message); //ERROR
    console.log(message);
    
    function test2(){
        message2 = "hi"; //global variable
    }
    test2();
    alert(message2); // message2 is a global variable
 console.log(message2);
     
       
</script> 
<br>
BYE
<script>
alert("SEGUIMOS "+message3);
</script>
</body>

</html>
 
 ____________________________________________________________________________
 
CREATE A NODE AND APPEND
<!DOCTYPE>
<html>

<head>
 <title>Create a Node with a text node inside and put it into a div</title>
 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<script>
 function alertContent(){
  var aNode = document.getElementById("myId");   // get a node by its ID
  var newNode = document.createElement("a");     // create a "a" element (<a>)
  var newText = document.createTextNode(" NEW content");  //create text node
  newNode.appendChild(newText);  //append a Child to the node to have this <a>NEW content</a>
  aNode.appendChild(newNode);    //append a Child to the <div id="myId"> the node that we created
 }
</script>

<body>
 
 <header>
  <h1>
   Create a Node with a text node inside and put it into a div
  </h1>
 </header>

 <div id="myId">
  SOME CONTENT
  <button onclick="alertContent()">
   show the name of the 
  </button>
 </div>
</body>

</html>

____________________________________________________________________________
CLONE NODE

<!DOCTYPE>
<html>

<head>
 <title>Variables and Scope of the Variables</title>
 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<script>
 function alertContent(){
  var aNode = document.getElementById("myId");
  var another = aNode.cloneNode();
  document.body.appendChild(another);
 }
</script>

<body>
 
 <header>
  <h1>
   Variables and Scope of the Variables
  </h1>
 </header>

 <div id="myId">
  SOME CONTENT
  <button onclick="alertContent()">
   show the name of the 
  </button>
 </div>
</body>

</html>




----------------------------------------------------
 VERSION 2.0

<!DOCTYPE>
<html>

<head>
 <title>Variables and Scope of the Variables</title>
 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<script>
 function alertContent(){
  var aNode = document.getElementById("myId");
  var another = aNode.firstChild.cloneNode();
  document.body.appendChild(another);
 }
</script>

<body>
 
 <header>
  <h1>
   Variables and Scope of the Variables
  </h1>
 </header>

 <div id="myId">
  SOME CONTENT
  <button onclick="alertContent()">
   show the name of the 
  </button>
 </div>
</body>

</html>
 
____________________________________________________________________________
CHANGE CSS

<!DOCTYPE html>

<html>
<head>
<style>
.classOne{
    background-color:#9A9;
    height:100px;
}

.classTwo{
 font-size:20px;
 
 width:350px;
}
</style>

<script>
function changeCssClass(){
    var aClassNode = document.getElementsByClassName("classOne");
 aClassNode[0].className += " classTwo";  // we select aClassNode first child and put in the className the same one and a new one (classOne classTwo)
}
</script>
</head>

<body">
<div class="classOne">
With this code we can change the class or add a new class to a Node
</div>
<button onclick="changeCssClass()">
Change CSS!!
</button>

</body>
</html>

____________________________________________________________________________

CARS TRAINS AND PLANES

 
<!DOCTYPE html>
<html>
<head>
<title>||Working with elements||</title>
</head>

<script>
var my_div = null;
var newDiv = null;
var c = 0;
var p = 0;
var t = 0;


function addElementV() {
  // create a new div element
  // and give it some content
  var newDiv = document.createElement("div");
  var newContent = document.createTextNode("NEW " + arguments[0] + " ! " + arguments[1]);
  newDiv.style.backgroundColor = "#CCC";
  newDiv.appendChild(newContent); //add the text node to the newly created div.

  // add the newly created element and its content into the DOM
  myCarDiv = document.getElementById(arguments[0]);
  myCarDiv.appendChild(newDiv);
  
}

function borrarVehic(){
 var myVehicDiv = document.getElementById(arguments[0]);
 myVehicDiv.removeChild(myVehicDiv.lastChild);
 
}
</script>

<body>
<a href="http://www.w3schools.com/dom/dom_nodes_navigate.asp"> DOM NODES NAVIGATION -- http://www.w3schools.com/dom/dom_nodes_navigate.asp </a><br><br>
 <button onclick="c++; addElementV('car', c)"> NEW CAR!</button>
 <button onclick="p++;addElementV('plane', p)"> NEW PLANE!</button>
 <button onclick="t++;addElementV('train', t)"> NEW TRAIN!</button>
 <button onclick="borrarVehic('car', 2)"> DELETE CAR!</button>
 <button onclick="borrarVehic('plane')"> DELETE PLANE!</button>
 <button onclick="borrarVehic('train')"> DELETE TRAIN!</button>

<div id="car" class="vehiculos" style="background-color:#f99">
 CARS
</div>
<div id="plane" class="vehiculos" style="background-color:#99f">PLANES</div>
<div id="train" class="vehiculos" style="background-color:#9f9">TRAINS</div>
</body>
</html>
 






martes, 24 de septiembre de 2013

CREATE ELEMENT

Recomendado instalar:
firebug
webdeveloper
wappalyzer


Developer Mozilla --> document create element.


var element = document.createElement(tagname);


https://developer.mozilla.org/en-US/docs/Web/API/document.createElement
 ___________________________________________________________________________
<!DOCTYPE html>

<body>

<header>
  <h1>1 Title</h1>
  <nav><ul>
  </ul></nav>
</header>

<section id='content'>
  <article class='blog-post'>
    <h1>2 Blog Post</h1>
  </article>
</section>
<script>
    var nodeInfo = document.createElement("p"); //create a <p> node
    var nodeText = document.createTextNode("info"); // text node
    nodeInfo.appendChild(nodeText);// append the text node to the p node
    //(the text node becomes child of the p node)
    document.body.appendChild(nodeInfo); // append the p node to the body node
    //(the p node becomes child of the body node)
    //var esteElemento = document.getElementById("content");
    //document.body.insertBefore(nodeInfo, esteElemento);
</script>
    <br>
3 Aqui sigue la pagina

<script>
    nodeInfo.appendChild(nodeText);// append the text node to the p node
    //(the text node becomes child of the p node)
    document.body.appendChild(nodeInfo); // append the p node to the body node
    //(the p node becomes child of the body node)
    //var esteElemento = document.getElementById("content");
    //document.body.insertBefore(nodeInfo, esteElemento);
</script>
</body>

</html>


_________________________________________________________________________


<!DOCTYPE html>
<html>
<head>
<title>||Working with elements||</title>
</head>

<script>
var my_div = null;
var newDiv = null;

function addElement () {
  // create a new div element
  // and give it some content
  var newDiv = document.createElement("div");
  var newContent = document.createTextNode("Hi there and greetings!");

  newDiv.appendChild(newContent); //add the text node to the newly created div.

  // add the newly created element and its content into the DOM
  my_div = document.getElementById("org_div1");
  document.body.insertBefore(newDiv, my_div);
}

</script>

<body onload="addElement()">
<div id='org_div1'> The text above has been created dynamically.</div>
<div id='org_div1'> The text above has been created dynamically.</div>
<div id='org_div1'> The text above has been created dynamically.</div>
<div id='org_div1'> The text above has been created dynamically.</div>
</body>
</html>
________________________________________________________________________







<!DOCTYPE html>
<html>
<head>
<title>||Working with elements||</title>
</head>

<script>
var my_div = null;
var newDiv = null;
var coche=0;
var avion=0;
var barco=0;




function agregarcoche() {
  // create a new div element
  // and give it some content
  var newDiv = document.createElement("div");
  var newContent = document.createTextNode("Coche nuevo numero" + coche++);
  newDiv.appendChild(newContent); //add the text node to the newly created div.
  // add the newly created element and its content into the DOM
  my_div = document.getElementById("org_divc");
  document.body.insertBefore(newDiv, my_div);
}
function agregaravion() {
  // create a new div element
  // and give it some content
  var newDiv = document.createElement("div");
  var newContent = document.createTextNode("Avion nuevo numero" + avion++);
  newDiv.appendChild(newContent); //add the text node to the newly created div.
  // add the newly created element and its content into the DOM
  my_div = document.getElementById("org_diva");
  document.body.insertBefore(newDiv, my_div);
}
function agregarbarco() {
  // create a new div element
  // and give it some content
  var newDiv = document.createElement("div");
  var newContent = document.createTextNode("Barco nuevo numero" + barco++);
  newDiv.appendChild(newContent); //add the text node to the newly created div.
  // add the newly created element and its content into the DOM
  my_div = document.getElementById("org_divb");
  document.body.insertBefore(newDiv, my_div);
}

</script>

<body onload="addElement()">
    <button onclick="agregarcoche()">+coche</button>   
        <div id='org_divc'> Estos son los coches creados.</div>
    <button onclick="agregaravion()">+avion</button>
        <div id='org_diva'> Estos son los aviones creados.</div>
    <button onclick="agregarbarco()">+barco</button>
        <div id='org_divb'> Estos son los barcos creados.</div>
</body>
</html>