Saltar al contenido principal
JAVASCRIPT



  By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Introduction
      JavaScript is the most popular scripting
language on the internet, and works in all major
browsers, such as Internet Explorer, Firefox,
Chrome, Opera, and Safari.



What You Should Already Know?
Before you continue you should have a basic
understanding of the following:
HTML and CSS


                 By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
What is JavaScript?
• scripting language
• add interactivity to HTML pages
•A scripting language is a lightweight programming language
•JavaScript is usually embedded directly into HTML pages
•JavaScript is an interpreted language (means that scripts execute
without preliminary compilation)

•Everyone can use JavaScript without purchasing a license

                     By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Java and JavaScript
•Java and JavaScript are not same.

•Java and JavaScript are two completely different languages in
both concept and design.




•Java (developed by Sun Microsystems) is a powerful and much
more complex programming language - in the same category as C

and C++.
                      By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
What Can JavaScript do?
•JavaScript gives HTML designers a programming tool - HTML authors
are normally not programmers, but JavaScript is a scripting language with a

very simple syntax. Almost anyone can put small "snippets" of code into their

HTML pages

•JavaScript can react to events - A JavaScript can be set to execute when
something happens, like when a page has finished loading or when a user

clicks on an HTML element

•JavaScript can read and write HTML elements - A JavaScript can read
and change the content of an HTML element

                          By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
What Can JavaScript do?
•JavaScript can be used to validate data - A JavaScript can be used to
validate form data before it is submitted to a server. This saves the server from

extra processing

•JavaScript can be used to detect the visitor's browser - A JavaScript can
be used to detect the visitor's browser, and - depending on the browser - load

another page specifically designed for that browser

•JavaScript can be used to create cookies - A JavaScript can be used to
store and retrieve information on the visitor's computer


                          By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Brief History of JavaScript
•JavaScript is an implementation of the ECMAScript language standard.
ECMA-262 is the official JavaScript standard.

•JavaScript was invented by Brendan Eich at Netscape (with Navigator 2.0),
and has appeared in all browsers since 1996.

•The official standardization was adopted by the ECMA organization (an
industry standardization association) in 1997.

•The ECMA standard (called ECMAScript-262) was approved as an
international ISO (ISO/IEC 16262) standard in 1998.

•The development is still in progress.
                           By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript: Display Date
<html>
          <head>
                 <title>Javascript</title>
          </head>
          <body>
                 <h1>My First Web Page</h1>
                 <script type="text/javascript">
                         document.write("<p>" + Date() + "</p>");
                 </script>
          </body>
</html>




                       By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
<script> Tag

•To insert a JavaScript into an HTML page, use the
<script> tag.

•Inside the <script> tag use the type attribute to define the
scripting language.

•The <script> and </script> tells where the JavaScript
starts and ends.

•Without the <script> tag(s), the browser will treat
"document.write" as pure text and just write it to the page.
                      By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript: comments
•    JavaScript comments can be used to make the code more readable.

•    Comments can be added to explain the JavaScript

•    There are Two types of comments in JavaScript:
          1) Single line comments
          2) Multi-Line comments
1)     Single line comments start with //.
e.g.      <script type="text/javascript">
                      // Write a heading
                      document.write("<h1>This is a heading</h1>");
                      // Write two paragraphs:
                      document.write("<p>This is a paragraph.</p>");
                      document.write("<p>This is another paragraph.</p>");
          </script>


                              By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript: comments
2) Multi-Line Comments
       Multi line comments start with /* and end with */.
e.g.
       <script type="text/javascript">
       /*
       The code below will write
       one heading and two paragraphs
       */
       document.write("<h1>This is a heading</h1>");
       document.write("<p>This is a paragraph.</p>");
       document.write("<p>This is another paragraph.</p>");
       </script>




                        By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript: comments
•   Use of JavaScript comments
    1) To Prevent Execution
    2) At the End of a Line
1) To Prevent Execution: The comment is used to prevent the execution of
         i) single code line:
     e.g.               <script type="text/javascript">
            //document.write("<h1>This is a heading</h1>");
            document.write("<p>This is a paragraph.</p>");
            document.write("<p>This is another paragraph.</p>");

            </script>

         ii) code block:
     e.g. <script type="text/javascript">
         /*document.write("<h1>This is a heading</h1>");
         document.write("<p>This is a paragraph.</p>");
         document.write("<p>This is another paragraph.</p>");*/
         </script>
                                 By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript: comments


2) At the End of a Line :




e.g.     <script type="text/javascript">
         document.write("Hello"); // Write "Hello"
         document.write("World!"); // Write "World!"
         </script>




                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript: comments
•   Browsers that do not support JavaScript, will display JavaScript as page content.

•   To prevent them from doing this, and as a part of the JavaScript standard, the HTML
    comment tag should be used to "hide" the JavaScript.

•   Just add an HTML comment tag <!-- before the first JavaScript statement, and a -->
    (end of comment) after the last JavaScript statement, like this:
    <html>
                      <body>
                                 <script type="text/javascript">
                                 <!--

                                 document.write("<p>" + Date() + "</p>");
                                 //-->
                                 </script>
                      </body>
          /html>
The two forward slashes at the end of comment line (//) is the JavaScript comment symbol. This
    prevents JavaScript from executing the --> tag.


                                By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Placing JavaScript
1)Scripts in <head> and <body>:
       You can place an unlimited number of scripts in your document, and you
       can have scripts in both the body and the head section at the same time.
       It is a common practice to put all functions in the head section, or at the
       bottom of the page. This way they are all in one place and do not interfere
       with page content.


                  e.g.     JavaScript in <body>
                      <html>
                                   <body>
                                   <h1>My First Web Page</h1>
                                   <p id="demo"></p>
                                   <script type="text/javascript">

                     document.getElementById("demo").innerHTML=Date();
                                           </script>
                         </body>
                         </html>
                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Placing JavaScript
e.g.     JavaScript in <head>
<html>
     <head>
                 <script type="text/javascript">
                 function displayDate()
                           {

   document.getElementById("demo").innerHTML=Date();
                            }
                  </script>
         </head>
   <body>
         <h1>My First Web Page</h1>
         <p id="demo"></p>
   <button type="button" onclick="displayDate()">Display
   Date</button>
   </body>
   </html>
        By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Placing JavaScript
2) Using an External JavaScript :

JavaScript can also be placed in external files.

External JavaScript files often contain code to be used on several different web pages.

External JavaScript files have the file extension .js.

Note:     External script cannot contain the <script></script> tags

        To use an external script, point to the .js file in the "src"   attribute of the

          <script> tag:
     e.g. <html>
                     <head>
                              <script type="text/javascript" src=“abc.js"></script>
                    </head>
                    <body>
                    </body>
          </html>

                              By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Writing JavaScript
•   JavaScript is Case Sensitive

•   JavaScript Statements : JavaScript is a sequence of statements to be

    executed by the browser. A JavaScript statement is a command to a browser

    e.g. document.write("Hello World");


•   JavaScript Code:
    <script type="text/javascript">
                     document.write("<h1>This is a heading</h1>");
                     document.write("<p>This is a paragraph.</p>");
                     document.write("<p>This is another paragraph.</p>");
         </script>



                             By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Writing JavaScript
•   JavaScript Blocks:

e.g.

<script type="text/javascript">

         {

                 document.write("<h1>This is a heading</h1>");

                 document.write("<p>This is a paragraph.</p>");

                 document.write("<p>This is another paragraph.</p>");

    }

    </script>


                         By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Variables
•   Variables are "containers" for storing information.

•   JavaScript variables are used to hold
    i) values e.g. x=5
    ii) expressions e.g. z=x+y. .

•   A variable can have a short name, like x, or a more descriptive
    name, like myname.
Rules for JavaScript variable names:
•   Variable names are case sensitive (y and Y are two different
    variables)

•   Variable names must begin with a letter or the underscore
    character

                         By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Variables
Declaring JavaScript Variables
•   Creating variables in JavaScript is most often referred to as "declaring"
    variables.
•   You declare JavaScript variables with the var keyword:
          var x;
          var carname;
•   After the declaration the variables are empty
•   assigning values
          var x=5;
          var carname="Volvo";
•   When you assign a text value to a variable, use quotes around the value.
•    If you redeclare a JavaScript variable, it will not lose its value.




                          By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Variables
•   A variable's value can change during the execution of a script. You can
   refer to a variable by its name to display or change its value.
e.g.
<html>
   <head>
         <title>Variables in JavaScript</title>
   </head>
   <body>
         <h1>Variables in JavaScript</h1>
         <script type="text/javascript">
                  var firstname;
                  firstname="xyz";
                  document.write(firstname);
                  document.write("<br>");
                  firstname="abc";
                  document.write(firstname);
                  document.write("<br>");
                  var firstname;
                  document.write(firstname);
         </script>
   </body></html>           By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Scope Of JavaScript Variables
Local JavaScript Variables
•   A variable declared within a JavaScript function becomes LOCAL and can
    only be accessed within that function. (the variable has local scope).

•   You can have local variables with the same name in different functions,
    because local variables are only recognized by the function in which they
    are declared.

•   Local variables are destroyed when you exit the function.
Global JavaScript Variables
•   Variables declared outside a function become GLOBAL, and all scripts
    and functions on the web page can access it.

•   Global variables are destroyed when you close the page.

•   If you declare a variable, without using "var", the variable always
    becomes GLOBAL.

                          By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Variables
Assigning Values to Undeclared JavaScript Variables:
If you assign values to variables that have not yet been declared, the
   variables will automatically be declared as global variables.
These statements:
        x=5;
        carname="Volvo";
will declare the variables x and carname as global variables (if they don't
   already exist).
JavaScript Arithmetic:
As with algebra, you can do arithmetic operations with JavaScript variables:
   y=x-5;
   z=y+5;


                          By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Assignments

Assignment No. 2:

1. Explain different Text Formatting Tags in HTML with example.

2. Describe local and remote links in HTML with example.

3. What are the different types of lists in HTML. Explain with examples.

4. Explain methods for adding graphics in HTML

5. Explain Table creation in HTML with appropriate examples.




                         By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Assignments

Assignment No. 3:

1.    Describe Image Maps in HTML with example.


2.    What are the different ways for defining Style Sheet properties in
     Cascading Style Sheet. Explain with appropriate examples.

3.    What are the different types of SELECTORS that can be used in
     Cascading Style Sheet. Explain with appropriate examples.

4.    Explain different elements in HTML forms that can be added to build
     interactivity.

5. Describe various HTML editors.



                           By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Data Types In JavaScript
There are six data types in JavaScript :
• Numbers
• Strings
• Booleans
• Null
• Undefined
• Objects

              By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Data Types In JavaScript
Numbers: Numbers in JavaScript consist of integers, floats, hexadecimals,
   octal i.e. all numbers are valid in JavaScript.
e.g. 4,51.50,-14,0xd
<html>
   <head>
        <title>Hexadecimal Numbers</title>
   </head>
   <body>
        <h1>My First Web Page</h1>
        <script type"text/css">
                 var h=0xe;
                 var i=0x2;
                 var j=h*i;
                 alert(j);
        </script>
   </body>
</html>

                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Data Types In JavaScript
Strings:
Strings consist of one or more characters surrounded by quotes.
e.g.      “Hello World”,
          “B”,
          “This is ‘another string’”


    If you need to use the same style of quotes both to enclose the string and within
    string, then the quotes must be escaped. A single backslash character escapes
    the quote
e.g.

•   ‘i’m using single quote both outside and within this example. They’re neat.’

•   “This is a ”great” example of using ”double quotes” within a string.”


                             By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
The table below lists other special characters that can
be added to a text string with the backslash sign:


     Code                    Outputs
     '                      single quote
     "                      double quote
                           backslash
     n                      new line
     r                      carriage return
     t                      tab
     b                      backspace
     f                      form feed

                  By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Data Types In JavaScript
Booleans:
  Booleans have only two values, true and false.
  Booleans are bit of a hidden data type in JavaScript i.e. you
  don’t work with Booleans in the same way that you work
  with strings and numbers. You simply use an expression
  that evaluates to a Boolean value.
e.g.
       If (mynumber>18) {
              //do something
       }
                    By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Data Types In JavaScript
Null :
   Null is special data type in JavaScript. Null is simply
  nothing. It represents and evaluates to false.
Undefined:
  Undefined is a state, sometimes used like a value, to
  represent a variable that hasn’t yet contained a value.
Objects:
  JavaScript objects are a collection of properties, each of
  which contains a primitive value. You can define your own
  objects using JavaScript, and there are several build-in
  objects as well.   By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Operators
  The ECMA-262 standard defines assorted operators of
  various forms. These include:
1. Additive operators
2. Multiplicative operators
3. Bitwise operators
4. Equality operators
5. Relational operators
6. Unary operators
7. Assignment operators


                     By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Operators
1. Additive operators:

 Operator        Description               Example           Result
 +               Addition                  x=y+2             x=7         y=5
 -               Subtraction               x=y-2             x=3         y=5

     The Addition operator operates in different ways depending on the
     type of value being added. When a string is used, the addition
     operator concatenates the left and right arguments.
 e.g. var str1=“hello”;
      var str2=“ world”;
      var result=str1+str2; // result will be the string “hello world”


                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Operators
2. Multiplicative operators :




 Operator Description               Example        Result
 *          Multiplication          x=y*2          x=10       y=5
 /          Division                x=y/2          x=2.5      y=5
 %          Modulus (division       x=y%2          x=1        y=5
            remainder)




                    By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Operators
3. Bitwise operators :
   Operator Description
   &          AND
   |          OR
   ^          XOR
   ~          NOT
   <<         Shift Left
   >>         Shift Right




                    By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Operators
4. Equality operators :
   Operator Description
   ==         Tests whether two expressions are equal
   !=         Tests whether two expressions are not equal
   ===        Tests whether two expressions are equal
              using stricter methods
   !==        Tests whether two expressions are not equal
              using stricter methods


   The stricter of two(===) requires not only that the values
   of a given expression are equal, but the types as well.



                    By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Operators
5. Relational operators :
  Operator     Description
  >            Greater than
  <            Less than
  >=           Greater than or equal to
  <=           Less than or equal to
  in           Tests whether a value is found in an
               expression
  instanceof   Tests whether an expression is an instance of
               an object




                      By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Operators
6. Unary operators :
  Operator   Description
  delete     Removes a property
  void       Returns Undefined
  typeof     Returns a string representing the data type
  ++         Increments a number
  --         decrements a number
  +          Converts the operand to a number
  -          Negates the operand
  ~          Bitwise NOT
  !          Logical NOT



                    By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Operators
7. Assignment operators :
 Operator       Example             Same As            Result
 =              x=y                                    x=5
 +=             x+=y                x=x+y              x=15
 -=             x-=y                x=x-y              x=5
 *=             x*=y                x=x*y              x=50
 /=             x/=y                x=x/y              x=2
 %=             x%=y                x=x%y              x=0




                  By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Conditional Statements
       Conditional statements are used to perform different
actions based on different conditions.
In JavaScript we have the following conditional statements:

• if statement - use this statement to execute some code only if
a specified condition is true

• if...else statement - use this statement to execute some code if
the condition is true and another code if the condition is false

• if...else if....else statement - use this statement to select one
of many blocks of code to be executed

• switch statement - use this statement to select one of many
blocks of code to be executed
                       By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Conditional Statements
if Statement
Use the if statement to execute some code only if a specified condition is true.
Syntax : if (condition)
            {
            code to be executed if condition is true
            }
e.g. <html>
         <head>
                   <title>If statement</title>
         </head>
         <body>
                   <script type="text/javascript">
                               var d = new Date();
                               var time = d.getHours();
                               if (time < 10)
                                 {
                                 document.write("<b>Good morning</b>");
                                 }
                   </script>
                   <p>This example demonstrates the If statement.</p>
                   <p>If the time on your browser is less than 10, you will get a "Good
                               morning" greeting.</p>
         </body></html>

                              By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Conditional Statements
If...else Statement :
        Use the if....else statement to execute some code if a condition is
true and another code if the condition is not true.

Syntax :
             if (condition)
              {
              code to be executed if condition is true
              }
             else
              {
              code to be executed if condition is not true
              }

                          By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Conditional Statements
<html>
          <head>
                    <title>If else statement</title>
          </head>
          <body>
                    <script type="text/javascript">
                               var d = new Date();
                               var time = d.getHours();
                               if (time < 10)
                               {
                                           document.write("<b>Good morning</b>");
                               }
                               else
                               {
                                           document.write("<b>Good day</b>");
                               }
                    </script>
                    <p>This example demonstrates the If...Else statement.         </p>
                    <p>
                               If the time on your browser is less than 10,
                               you will get a "Good morning" greeting.
                               Otherwise you will get a "Good day" greeting.
                    </p>
          </body>
</html>


                               By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Conditional Statements
If...else if...else Statement :
        Use the if....else if...else statement to select one of several blocks of
code to be executed.

Syntax :         if (condition1)
                  {
                  code to be executed if condition1 is true
                  }
                 else if (condition2)
                  {
                  code to be executed if condition2 is true
                  }
                 else
                  {
                  code to be executed if neither condition1 nor condition2 is true
                  }        By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Conditional Statements
<html>
          <head>
                    <title>If else if statement</title>
          </head>
          <body>
                    <script type="text/javascript">
                              var d = new Date();
                              var time = d.getHours();
                              if (time<10)
                              {
                                          document.write("<b>Good morning</b>");
                              }
                              else if (time>=10 && time<16)
                              {
                                          document.write("<b>Good day</b>");
                              }
                              else
                              {
                                          document.write("<b>Hello World!</b>");
                              }
                    </script>
                    <p>This example demonstrates the if..else if...else statement.</p>
          </body>
</html>                       By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Conditional Statements
The JavaScript Switch Statement
Use the switch statement to select one of many blocks of code to be
executed.

Syntax :        switch(n)
                {
                case 1:
                              execute code block 1
                             break;
                case 2:
                              execute code block 2
                             break;
                default:
                             code to be executed if n is different from case 1 and 2
                }           By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Conditional Statements
<html>
         <head>
                   <title>switch statement</title>
         </head>
         <body>
                   <script type="text/javascript">
                              var d=new Date();
                              var theDay=d.getDay();
                              switch (theDay)
                              {
                              case 5:
                                          document.write("<b>Finally Friday</b>");
                                          break;
                              case 6:
                                          document.write("<b>Super Saturday</b>");
                                          break;
                              case 0:
                                          document.write("<b>Sleepy Sunday</b>");
                                          break;
                              default:
                                          document.write("<b>I'm really looking forward to this
                                                   weekend!</b>");
                              }
                   </script>
                   <p>This JavaScript will generate a different greeting based on what day it
is.
                   Note that Sunday=0, Monday=1, Tuesday=2, etc.</p>
         </body>              By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Dialog boxes


JavaScript has three kind of dialog boxes:

1) Alert box.

2) Confirm box.

3) Prompt box.




                  By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Dialog boxes
1) Alert box : An alert box is often used if you want to make sure information
comes through to the user. When an alert box pops up, the user will have to click
"OK" to proceed.
Syntax :             alert("some text");
Example :
<html>
           <head>
                     <script type="text/javascript">
                     function show_alert()
                     {
                               alert("Hello! I am an alert box!");
                     }
                     </script>
           </head>
           <body>
                     <input type="button" onclick="show_alert()" value="Show alert box">
           </body>
</html>
                              By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Dialog boxes

2) Confirm box :

           A confirm box is often used if you want the user to verify or accept

something. When a confirm box pops up, the user will have to click either

"OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true.

If the user clicks "Cancel", the box returns false.

Syntax :


           confirm("sometext");


                           By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Dialog boxes
<html>
          <head>
                    <script type="text/javascript">
                    function show_confirm()
                    {
                              var r=confirm("Press a button!");
                              if (r==true)
                                {
                                           alert("You pressed OK!");
                                }
                              else
                                {
                                         alert("You pressed Cancel!");
                                }
                    }
                    </script>
          </head>
          <body>
                    <input type="button" onclick="show_confirm()" value="Show a
                    confirm box" />
          </body>
</html>
                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Dialog boxes
2) Prompt box :
        A prompt box is often used if you want the user to input a value
before entering a page.
When a prompt box pops up, the user will have to click either "OK" or
"Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks
"Cancel" the box returns null.



Syntax :
        prompt("sometext","defaultvalue");


                          By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Dialog boxes
<html>
          <head>
                    <script type="text/javascript">
                    function show_prompt()
                    {
                              var name=prompt("Please enter your name","abc");
                              if (name!=null && name!="")
                                {
                                         document.write("Hello " + name + "! How
                                                are you today?");
                                }
                    }
                    </script>
          </head>
          <body>
                    <input type="button" onclick="show_prompt()" value="Show
                                      prompt box" />
          </body>
</html>

                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Functions
            A JavaScript function is a collection of statements, either
named or unnamed, that can be called from elsewhere within a
JavaScript program.
• To keep the browser from executing a script when the page loads, you can put
your script into a function.

• A function contains code that will be executed by an event or by a call to the
function.

• You may call a function from anywhere within a page (or even from other
pages if the function is embedded in an external .js file).

• Functions can be defined both in the <head> and in the <body> section of a
document. However, to assure that a function is read/loaded by the browser
before it is called, it could be wise to put functions in the <head> section.

                               By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Functions

Syntax of Function:

         function functionName(argument1,argument2,...,argumentX)
         {
         //statements go here;
         }




                     By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Functions
<html>
          <head>
                    <title>JavaScript Function</title>
                    <script type="text/javascript">
                              function displaymessage()
                              {
                              alert("Hello World!");
                              }
                    </script>
          </head>
          <body>
                    <form>
                              <input type="button" value="Click me!"
                              onclick="displaymessage()">
                    </form>
          </body>
</html>


                              By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Functions
The return Statement : When a function finishes executing its code, a
return value can be passed back to the caller.
Example:
         <html>
                   <head>
                             <script type="text/javascript">
                                       function multiplyNums(a,b)
                                       {
                                                 return a*b;
                                       }
                             </script>
                   </head>
                   <body>
                             <script type="text/javascript">
                                       document.write(multiplyNums(4,3));
                             </script>
                   </body>
         </html>

                             By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Arrays
   An array is a special variable, which can hold more than one value, at a
   time.
Defining Arrays:
1) regular array :
   e.g. var empNames=new Array(); // regular array (add an optional integer
           empNames[0]=“John";     // argument to control array's size)
           empNames[1]=“Smith";
           empNames[2]=“Mark";
2) condensed array :
   e.g. var empNames=new Array(“John",“Smith",“Mark"); // condensed array
3) literal array :
   e.g. var empNames=["John",”Smith",“Mark "]; // literal array

                         By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Arrays
Access an Array :


   e.g. document.write(empNames[0]);


Modify Values in an Array :


   e.g. empNames[0]=“Rajesh”;




                        By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Controlling Flow With Loops In
                 JavaScript

In JavaScript, there are two different kind of loops:

• for - loops through a block of code a specified number
of times

• while - loops through a block of code while a
specified condition is true



                   By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Loops In JavaScript

for loops : The for loop is used when you know in advance how many times
the script should run.

Syntax :

for (variable=startvalue;variable<=endvalue;variable=variable+increment)

{

code to be executed

}




                          By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
for Loop Example
<html>
          <body>
                    <script type="text/javascript">
                    var i=0;
                    for (i=0;i<=5;i++)
                    {
                               document.write("The number is " + i);
                               document.write("<br />");
                    }
                    </script>
                    <p>Explanation:</p>
                    <p>This for loop starts with i=0.</p>
                    <p>As long as <b>i</b> is less than, or equal to 5, the loop will
                               continue to run.</p>
                    <p><b>i</b> will increase by 1 each time the loop runs.</p>
          </body>
</html>



                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Loops In JavaScript

while loops :

           The while loop loops through a block of code while a specified condition

is true.

Syntax :

               while (variable<=endvalue)

                {

                 code to be executed

                }


                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
while Loop Example
<html>
          <body>
                    <script type="text/javascript">
                              i=0;
                              while (i<=5)
                              {
                                        document.write("The number is " + i);
                                        document.write("<br />");
                                        i++;
                              }
                    </script>
                    <p>Explanation:</p>
                    <p><b>i</b> is equal to 0.</p>
                    <p>While <b>i</b> is less than , or equal to, 5, the loop will
                    continue to run.</p>
                    <p><b>i</b> will increase by 1 each time the loop runs.</p>
          </body>
</html>

                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Loops In JavaScript

Do…while loops :            The do...while loop is a variant of the while loop. This

loop will execute the block of code ONCE, and then it will repeat the loop as long

as the specified condition is true

Syntax :

              do

               {

                code to be executed

                }

              while (variable<=endvalue);

                           By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Do…while Loop Example
<html>
          <body>
                    <script type="text/javascript">
                              i = 0;
                              do
                              {
                                        document.write("The number is " + i);
                                        document.write("<br />");
                                        i++;
                              }
                              while (i <= 5)
                    </script>
                    <p>Explanation:</p>
                    <p><b>i</b> equal to 0.</p>
                    <p>The loop will run</p>
                    <p><b>i</b> will increase by 1 each time the loop runs.</p>
                    <p>While <b>i</b> is less than , or equal to, 5, the loop will
                                        continue to run.</p>
          </body>
</html>

                             By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Break and Continue Statements
The break Statement
The break statement will break the loop and continue executing the code that
follows after the loop (if any).
Example:            <html>
                              <body>
                                        <script type="text/javascript">
                                        var i=0;
                                        for (i=0;i<=10;i++)
                                          {
                                                    if (i==3)
                                                      {
                                                              break;
                                                      }
                                          document.write("The number is " + i);
                                          document.write("<br />");
                                        }
                                        </script>
                              </body>
                    </html>
                              By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Break and Continue Statements
The continue statement will break the current loop and continue with the
next value.
Example: <html>
                  <body>
                            <script type="text/javascript">
                                     var i=0
                                     for (i=0;i<=10;i++)
                                      {
                                               if (i==3)
                                               {
                                                         continue;
                                               }
                                      document.write("The number is " + i);
                                      document.write("<br />");
                                      }
                            </script>
                  </body>
        </html>
                           By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Objects
JavaScript is a Object based Language. It allows you to define your own objects.
An object is just a special kind of data. An object has properties and methods.

• Properties :
         Properties are the values associated with an object.
e.g.     <script type="text/javascript">
                     var txt="Hello World!";
                     document.write(txt.length);
         </script>
        Output:=12
• Methods :
        Methods are the actions that can be performed on objects.
e.g.    <script type="text/javascript">
                  var str="Hello world!";
                  document.write(str.toUpperCase());
        </script>
        Output:=HELLO WORLD!



                             By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Type casting/
                    Data type Conversion
1) Number Conversions
2) String Conversions
3) Boolean Conversion
1) Number Conversions:
   The Number() function converts the object argument to a number that
   represents the object's value. If the value cannot be converted to a legal
   number, NaN is returned.
Syntax:
   Number(object) ;

Parameter    Description
             Optional. A JavaScript object. If no argument is provided, it
object
                returns 0.

                           By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Type casting
1) Number Conversion:
<html>
    <head>
         <title>Conversion to Number</title>
    </head>
    <body>
         <script type="text/javascript">
                   var test1= new Boolean(true);
                   var test2= new Boolean(false);
                   var test3= new Date();
                   var test4= new String("999");
                   var test5= new String("999 888");
                   document.write(Number(test1)+ "<br>");
                   document.write(Number(test2)+ "<br>");
                   document.write(Number(test3)+ "<br>");
                   document.write(Number(test4)+ "<br>");
                   document.write(Number(test5)+ "<br>");
         </script>
    </body>
</html>


                          By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
Note: If the parameter is a Date object, the Number() function returns the
number of milliseconds since midnight January 1, 1970 UTC.



                         By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Type casting/
                  Data type Conversion
2) String Conversions :



  The String() function converts the value of an object to a string.


Syntax:
               String(object);


   Parameter                           Description
   object                              Required. A JavaScript object




                       By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Type casting
<html>
          <head>
                    <title>Conversion to Number</title>
          </head>
          <body>
                    <script type="text/javascript">
                              var test1= new Boolean(1);
                              var test2= new Boolean(0);
                              var test3= new Boolean(true);
                              var test4= new Boolean(false);
                              var test5= new Date();
                              var test6= new String("999 888");
                              var test7=12345;
                              document.write(String(test1)+ "<br />");
                              document.write(String(test2)+ "<br />");
                              document.write(String(test3)+ "<br />");
                              document.write(String(test4)+ "<br />");
                              document.write(String(test5)+ "<br />");
                              document.write(String(test6)+ "<br />");
                              document.write(String(test7)+ "<br />");
                    </script>
          </body>
</html>
                            By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Events

Events are actions that can be detected by JavaScript.
We define the events in the HTML tags.
Examples of events:

    •A mouse click
    •A web page or an image loading
    •Mousing over a hot spot on the web page
    •Selecting an input field in an HTML form
    •Submitting an HTML form
    •A keystroke
Note: Events are normally used in combination with functions, and the
function will not be executed before the event occurs!


                       By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Events

Attribute    The event occurs when...

onblur       An element loses focus
onchange     The content of a field changes
onclick      Mouse clicks an object
ondblclick   Mouse double-clicks an object
             An error occurs when loading a document or an
onerror
                image
onfocus      An element gets focus
onkeydown    A keyboard key is pressed
onkeypress   A keyboard key is pressed or held down
onkeyup      A keyboard key is released
onload       A page or image is finished loading



              By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Events

Attribute     The event occurs when...


onmousedown   A mouse button is pressed

onmousemove   The mouse is moved

onmouseout    The mouse is moved off an element

onmouseover   The mouse is moved over an element

onmouseup     A mouse button is released

onresize      A window or frame is resized

onselect      Text is selected

onunload      The user exits the page


                By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Events Example
<html>
    <head>
        <script type="text/javascript">
            function upperCase()
            {
            var x=document.getElementById("fname").value
            document.getElementById("fname").value=x.toUpperCase()
            }
        </script>
    </head>
    <body>

               Enter your name:
               <input type="text" id="fname" onblur="upperCase()">

    </body>
</html>


                     By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Built-in Objects
String Object:
           The String object is used to manipulate a stored piece of
text. String objects are created with new String().


Syntax :
       var txt = new String("string");
                or
       var txt = "string";


String Object Properties :
Property       Description
length         Returns the length of a string



                             By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER
JavaScript Built-in Objects
Method            Description
charAt()          Returns the character at the specified index
charCodeAt()      Returns the Unicode of the character at the specified index
                  Joins two or more strings, and returns a copy of the joined
concat()
                  strings
fromCharCode()    Converts Unicode values to characters
                  Returns the position of the first found occurrence of a
indexOf()
                  specified value in a string

                  Returns the position of the last found occurrence of a
lastIndexOf()
                  specified value in a string

                  Searches for a match between a regular expression and a
match()
                  string, and returns the matches

                  Searches for a match between a substring (or regular
replace()         expression) and a string, and replaces the matched substring
                  with a new substring



                         By Mrs. Geetanjali A. Bhosale. Assistant Professor, SIBER