Anda di halaman 1dari 9

CONTENIDO

Escribir texto ............................................................................................................................................................................... 3


Alerta........................................................................................................................................................................................... 3
Variables...................................................................................................................................................................................... 3
Prompts ....................................................................................................................................................................................... 3
Arrays .......................................................................................................................................................................................... 3
If Statement ................................................................................................................................................................................ 3
If else statement ......................................................................................................................................................................... 3
if statement ................................................................................................................................................................................. 4
For Loop ...................................................................................................................................................................................... 4
While Loop .................................................................................................................................................................................. 4
Break and Continue ..................................................................................................................................................................... 4
Methods and Properties ............................................................................................................................................................. 5
Math Objects............................................................................................................................................................................... 5
Event Handler.............................................................................................................................................................................. 5
Bulding Objects ........................................................................................................................................................................... 6
Object Initializer .......................................................................................................................................................................... 6
Basic Login ................................................................................................................................................................................... 6
Object Orientated Programming (OOP) ...................................................................................................................................... 7
Document Object Model (DOM) ................................................................................................................................................. 8

ESCRIBIR TEXTO

<script type="text/javascript">
document.write("Hola mundo");
</script>

ALERTA

<script type="text/javascript">
alert("Hola mundo");
</script>

VARIABLES

<script type="text/javascript">
var test = "El resultado de esta suma es: ";
var num1 = 10;
var num2= 5;
var resultado = (num1 + num2);
alert(test+resultado);
</script>

PROMPTS

<script type="text/javascript">
var nombre = promt("Introduzca su nombre");
</script>

ARRAYS

<script type="text/javascript">
var comida = new Array("cheese","beef","bread");
var ciudades = new Array;
ciudades[0]="Bucaramanga";
ciudades[1]="Duitama"
ciudades[2]="Kyoto"
document.write("My favorite food is:
"+comida[0]+"...");
document.write(ciudades)
</script>

IF STATEMENT

<script type="text/javascript">
var edad = prompt("Ingresa tu edad");
if(edad < 30)
{
alert("Ests muy joven");
}
</script>

IF ELSE STATEMENT

<script type="text/javascript">

My favorite food is: cheese...Bucaramanga,Duitama,Kyoto

var edad = prompt("Ingresa tu edad");


if(edad < 30)
{
alert("Ests muy joven");
}
else
{
alert("Ests muy viejo");
}
</script>

IF STATEMENT

<script type="text/javascript">
var edad = prompt("Ingresa tu edad");
if(edad == 30)
{
alert("Tu edad es 30");
}
else if (edad == 20)
{
alert("Tu edad es 20");
}
else
{
alert("No tienes ni 20 , ni 30");
}
</script>

FOR LOOP

<script type="text/javascript">
var cars = new Array("Renault","Chevrolet","Mini");
for (i=0;i<cars.length;i++)

Renault
Chevrolet
Mini

{
document.write(cars[i]+"<br />");
}
</script>

WHILE LOOP

<script type="text/javascript">
var i = 0;
while (i<=5)
{
document.write("Hola "+i+"<br />");
i=i+1;
}
</script>

BREAK AND CONTINUE

<script type="text/javascript">

Hola 0
Hola 1
Hola 2
Hola 3
Hola 4
Hola 5

for(i=0;i<=10;i++)
{
if (i==5)
{
break;
}

this is the number 0


this is the number 1
this is the number 2
this is the number 3
this is the number 4

document.write("this is the number "+i+"<br />");


}
</script>

<script type="text/javascript">
for(i=0;i<=10;i++)
{
if (i==5)
{
continue;

this is the number 0


this is the number 1
this is the number 2
this is the number 3
this is the number 4
this is the number 6
this is the number 7
this is the number 8
this is the number 9
this is the number 10

}
document.write("this is the number "+i+"<br />");
}
</script>

METHODS AND PROPERTIES

Properties(Check w3school.com for more properties)

<script type="text/javascript">
var x = "Harol";
document.write(x.length);
</script>

Methods(Check w3school.com for more methods)

<script type="text/javascript">
var x = "Hola mi nombre es Harol";

HOLA MI NOMBRE ES HAROL


Hola mi nombre es Harol
18
nomb

document.write(x.toUpperCase()+"<br />");
document.write(x.fontcolor("blue")+"<br />");
document.write(x.indexOf("Harol")+"<br />");
document.write(x.match("nomb"));
</script>

MATH OBJECTS

<script type="text/javascript">
document.write(Math.round(3.2)+"<br />");
document.write((Math.random()*100)+1+"<br />");
document.write(Math.floor((Math.random()*100)+1));
</script>

EVENT HANDLER

3
68.363random number 1 to 100
29..random number 1 to 100 with no decimal numbers.

Events Handlers(Check w3school.com for more handlers)

<head>
<script type="text/javascript">
function test (){
alert('this works');
}
</script>
<head/>
<body>
<form>
<input type="button" name="submit" value="submit" onclick="test();" />
</form>
</body>

BULDING OBJECTS

<head>
<script type="text/javascript">
function myhero (name, species, strength){
this.name = name;
this.species = species;
this.strength = strength;
}

The name of the hero is Superman


The species is kriptonian
The strength is 300

var hero = new myhero("Superman","kriptonian",300);


document.write("The name of the hero is "+hero.name+"<br
/>");
document.write("The species is "+hero.species+"<br />");
document.write("The strength is "+hero.strength);
</script>
<head/>

OBJECT INITIALIZER

<head>
<script type="text/javascript">
var hero =
{ name:"Superman",species:"kriptonian",strength:300}
document.write("The name of the hero is "+hero.name+"<br
/>");
document.write("The species is "+hero.species+"<br />");
document.write("The strength is "+hero.strength);
</script>
<head/>
<body>
</body>

BASIC LOGIN

<head>
<script type="text/javascript">

The name of the hero is Superman


The species is kriptonian
The strength is 300

var register = prompt("Enter password to register,


please");
function success(){
document.write("Login succesfull");
}
function failed(){
document.write("Invalid Credentials");
}
if(register != "")
{
var user = prompt("Valid password, confirm password
to precede");
if(register==user){
success();
}else{
failed();
}
}else{
alert("Error, insert password");
}
</script>
<head/>

OBJECT ORIENTATED PROGRAMMING (OOP)

<head>
<script type="text/javascript">

var num1 = prompt("Ingrese nmero 1");


var num2 = prompt("Ingrese nmero 2");

function Calculadora(){
this.multiplicacion = function(val1, val2){
return (val1*val2);
}
this.resta = function(val1, val2){
var res = (val1 - val2);
return (sum);
}
}

var calc = new Calculadora();


alert(calc.multiplicacion(num1,num2));
alert(calc.resta(num1,num2));

</script>
<head/>

Login succesfull

DOCUMENT OBJECT MODEL (DOM)

PROJECT
<html>
<head>
<title>Form</title>
<script type="text/javascript">
function register(){
var x = new Array();
x[0] = document.getElementById('name').value;
x[1] = document.getElementById('lname').value;
x[2] = document.getElementById('email').value;
x[3] = document.getElementById('password').value;
x[4] = document.getElementById('cpassword').value;

var h = new
h[0]="<span
h[1]="<span
h[2]="<span
h[3]="<span
h[4]="<span

Array();
style='color:red'>Please
style='color:red'>Please
style='color:red'>Please
style='color:red'>Please
style='color:red'>Please

type
type
type
type
type

your
your
your
your
your

name</span>";
last name</span>";
email </span>";
password</span>";
password again</span>";

var divs = new Array("mname","mlname","memail","mpassword","mcpassword");


for(i in x){
var error = h[i];
var div = divs[i];
if(x[i]==""){
document.getElementById(div).innerHTML = error;
}else{
document.getElementById(div).innerHTML = "Ok";
}
}
}

function pass(){
var first = document.getElementById('password').value;
var second = document.getElementById('cpassword').value;
if (first==second){
document.getElementById('mcpassword').innerHTML = "Passwords matched";
}else{
document.getElementById('mcpassword').innerHTML = "<span style 'color:red'>Your password does not match, try
again</span>";
}
}

</script>
<head/>
<body>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="name" onkeyup="register()" /></td>
<td><div id="mname" ></div></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lname" id="lname" onkeyup="register()" /></td>
<td><div id="mlname" ></div></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" id="email" onkeyup="register()" /></td>

<td><div id="memail" ></div></td>


</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password" id="password" onkeyup="register()" /></td>
<td><div id="mpassword" ></div></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><input type="text" name="cpassword" id="cpassword" onkeyup="pass()" /></td>
<td><div id="mcpassword" ></div></td>
</tr>
</table>
<br/>
<input type="submit" name="submit" value="submit" />
</body>
</html>

Anda mungkin juga menyukai