Tuesday, August 30, 2016

JavaScript 101!

Basics
    General:
    • It's case sensitive
    • End each statement with ;
    • Very forgiving if you forget to close the statement with ;
    • Not sensitive to white spaces (except in strings "" or line breaks.
    • /* comments */ or // comments and the rest
    • If you need it to create page, put in <head> otherwise, put it before </body>
    • var abc, _xxx, $yyy; var year=2020;  // JS a weakly typed language
    Control Flows:
    • if ( condition ) { statements }

Keywords
  • alert
  • console.log
  • true, false
  • var

Find ...
    var a = document.getElementById ("mainTitle");
    var b = document.getElementsByTagName ("li");
    var b = document.getElementsByClassName ("description");
    
    document.getElementById ("mainTitle").className = "highlight";
    
    // JQuery Methods and Alias
    jQuery(".discription").addClass("highlight");
    $("myclass").addClass("highlight");
    $("li").addclass("hightlight");
    $("p:contains('packages')").hide(4000);
    $("p").fadeOut(4000);
    
    jQuery("#maintitle").removeClass("highlight");
    jQuery("li:last").toggleClass("highlight");
    
    $("#pageID").click (function() {
        $("#pageID").text ("You clicked text!");
    });
    $("h2").click (function() {
        $(this).text ("You clicked here!");
    });
    $("p").click (function() {
        $(this).fadeOut (3000);
    });
    
    // Page load events - same as window.onload()
    // But don't have to worry about calling it multiple times
    $(document).ready(function () {
        $("#pageID").text ("DOM fully loaded.");
    });
    $(document).ready(function () {
        $("h1").css ("color", "red");
    });
    
    

    Example
    <script>
       console.log ("Download Git, Firebug and enable Console!")
    
       var myvar = 1000;
    
       newvar = 555; // skip "var" is okay but it is not a good practice.
    
       var lineone = "this is line one.";
    
       var just_the_num = lineone.slice(8);
    
       console.log(just_the_num);
    
       console.log(`Now we only have this part: ${just_the_num}`); 
    
    </script>
    
    

    Only in Node.js:
    
       // var path = require("path");
    
       // console.log(__filename);  // full pathname and file name
    
       // console.log(__dirname); // full pathname to current directory
    
       // alert(` remove the path: ${path.basename(__filename)}`);  
    // just the file name
    
    
    
    Or put the code in another file "greeting.js", and
    
    
    
    <script src="greeting.js" type="text/javascript">
    
        // alert (greeting.firstline); // won't work
    
    </script>
    
    

    Useful Links:

    Books:


No comments:

Post a Comment