Monday, February 1, 2016

javascript basic

Arry

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       
        <script type="text/javascript">
           
            var earning = new Array();
           
            earning[2011] = 65100;
            earning[2012] = 7500;
            earning[2013] = 75000;
            earning[2014] = 750000;
           
//            alert(earning[2011]);
           
var cars = [
    "Saab",
    "Volvo",
    "BMW"];            
      
    alert(cars.length);
   
var cars1 = {
    1:"Saab",
    2:"Volvo",
    3:"BMW"
    };            
            alert(cars1[2]);
        </script>
       
    </head>
    <body>
    </body>
</html>


events


<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
       
        <input type="text" autofocus="">
        <input type="text" name="email" autocomplete="off"/>
       
        <input onblur="alert('test')">
        <select onchange="alert(this.value)">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
        </select>
       
        <button onfocus="alert('yes')">focus</button>
       
        <input type="text" onkeypress="alert(this.value)">
        <input type="text" onkeyup="alert(this.value)">
        <input type="text" onkeydown="alert(this.value)">
       
    </body>
</html>


fUNCTION



<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">

    function input(value) {
                document.getElementById('output').value += value;
            }
            function Clear() {
                document.getElementById('output').value = '';
            }
            function GetResult() {
                var input = document.getElementById('output').value;
                var cal = eval(input);
                document.getElementById('output').value = cal;

//                document.getElementById('output').value = eval(document.getElementById('output').value);
            }
            function Backword() {
                var input = document.getElementById('output').value;
                var length = input.length;
                var substr = input.substring(0, length - 1);
                document.getElementById('output').value = substr;
            }                       
        </script>
    </head>
    <body>
        <input type="text" id="output"><br/>
        <button onclick="input(this.value)" value="1">1</button>
        <button onclick="input(this.value)" value="2">2</button>
        <button onclick="input(this.value)" value="3">3</button>
        <button onclick="input(this.value)" value="4">4</button><br>
        <button onclick="input(this.value)" value="5">5</button>
        <button onclick="input(this.value)" value="6">6</button>
        <button onclick="input(this.value)" value="7">7</button>
        <button onclick="input(this.value)" value="8">8</button><br/>
        <button onclick="input(this.value)" value="9">9</button>
        <button onclick="input(this.value)" value="0">0</button>
        <button onclick="input(this.value)" value="00">00</button>
        <button onclick="input(this.value)" value=".">.</button>
        <button onclick="input(this.value)" value="+">+</button><br/>
        <button onclick="input(this.value)" value="-">-</button>
        <button onclick="input(this.value)" value="*">*</button>
        <button onclick="input(this.value)" value="/">/</button>
        <button onclick="GetResult()" >=</button><br/>
        <button onclick="Clear()" >C</button>
        <button onclick="Backword()" >&lAarr;</button>       
    </body>
</html>


If else


<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       
        <script type="text/javascript">
           
            var number = 101;
            alert(number);
           
            if(number<100){
                alert('fasdf');
            }
            else if(number===101){
                alert('else if work');
            }
            else{
               alert('no') ;
            }
           
        </script>
       
    </head>
    <body>
    </body>
</html>


Looping


<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">

            var earning = new Array();
            earning[2011] = 65100;
            earning[2012] = 7500;
            earning[2013] = 75000;
            earning[2014] = 750000;

            alert(earning.length);

//            var a="Value="+10;

            //for(initialize,condition,increment or decrecment)
//            i+=2
            var i;
            for (i = 2011; i <= 2014; i++) {
//                alert("Earning of " + i + ": " + earning[i]);
            }


        </script>
    </head>
    <body>
    </body>
</html>


test boolian

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       
        <script type="text/javascript">
           
            var a = 10;
            var b = 'a';
           
            var c = a+b;
           
//            alert(c);
           
            // new]
            
             var d = 10;
             d+=15;
//             alert(d);
           
           
           
            var e = 4;
            var str = '45';
           
            e+=str;
           
//            alert(e);
           
            var t = 6e2;
//            alert(t);
           
            var bol = true;
//            alert(bol);
           
            if(bol){
                alert('yes');
            }
        </script>
       
    </head>
    <body>
        <div>TODO write content</div>
    </body>
</html>

test if else

<!DOCTYPE html>
<html>
<head>
    <title>Javascript</title>
    <script type="text/javascript">
        // document.write("Hello");
        // function txt(){
        //     document.getElementById('jstext').innerHTML = "Hello adam";
        // }
        // function rev(){
        //     document.getElementById('jstext').innerHTML = "";
        // }
        function grade(){
            var point = document.getElementById('mark').value;
            if(point<=39){
                document.getElementById('output').innerHTML = "fail";
            }else if(point<=49){
                document.getElementById('output').innerHTML = "c";
            }
            else if(point<=59){
                document.getElementById('output').innerHTML = "B";
            }
            else if(point<=69){
                document.getElementById('output').innerHTML = "B+";
            }
            else if(point<=79){
                document.getElementById('output').innerHTML = "A";
            }
            else if(point >=80 ){
                document.getElementById('output').innerHTML = "A+";
            }
           
        }

    </script>
</head>
<body>
    <!-- <div id="jstext" style="width:500px; height:200px; border:2px solid;" onmouseover="txt()" ondblclick="rev()">
    </div> -->
        <input type="text" name="mark" id="mark" placeholder="Mark">
        <button onclick="grade()">View</button>
   
    <div class="output" id="output">
        Waiting for result
    </div>
</body>
</html>


on click event 
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>


<p id="demo">can change</p>
<button type="button" onclick="document.getElementById ('demo').innerHTML = 'hello'";)>

click me
  

</button>



</body>
</html>

No comments:

Post a Comment