WISDOMBAY

in4mation ... organized

Master Javascript by Typing Part 1

A series by Pramod S Nair for Wisdombay (originally published in www.spidercreationsindia.com)

Launch your Text Editor, type the above given code & save as second.html (or any other name you prefer). Now view the file that you created in browser & you will see a small button in the page. Clicking on the button will launch a dialog box that displays the message that you gave as parameter to the alert() function.



Analysing the code

The events are controlled by using Event handlers like onClick, onLoad etc. We will discuss them in detail later. Here the key code segment is the line
<input type="button" value="Click Me" onClick='alert("Yes Sir")'>
here we are creating a button and are adding a handler to it. The handler is added by writing the onClick event handler in the <input> tag. So when a click event is made on the button the code that is specified for onClick gets executed.

  • Adding code in the Body

    You can include JavaScript commands into a particular part of the page. This is useful if, for example, if you'd like JavaScript to write text directly into the browser. The following code is an example.

    <html>
    <head>
    <title>My third script</title>
    </head>
    <body>
    <h1> This page displays Date and Time </h1>
    <h3>
    <script language="Javascript">
    d = new Date(); //get a date object
    dt = d.getDate(); //get current day from date object
    m = d.getMonth()+1; //get month and add 1. since january is represented by 0
    y = d.getYear(); // get year
    document.write ("Today is " + dt + " / " + m + " / " + y)
    </script>
    </h3>
    </body>
    </html>


    Launch your Text Editor, type the above given code & save as second.html (or any other name you prefer). Now view the file that you created in browser & you will see a small button in the page. Clicking on the button will launch a dialog box that displays the message that you gave as parameter to the alert() function.



    Analysing the code

    Even though the code look a bit cryptic to newbies, just concentarte on
    document.write ("Today is " + dt + " / " + m + " / " + y)
    , this statement is used to write to the browser from a code block. The other things i will explain when you get enough exposure to DOM (Document Object Model) & OOPS( Object Oriented Programming). We will discuss them in later issues. In this third option of incorporating javascript you write the code just like you did in the first section. ie, between <script> & </script>


    SO WE CONCLUDE OUR FIRST PART HERE.

  • Google