09 javascript

Upload: dolby3d

Post on 08-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 09 Javascript

    1/22

    assan an

  • 8/7/2019 09 Javascript

    2/22

    JavaScript

    JavaScript is thescripting language of the

    Web.

    JavaScript is used in millions of Web pages

    to add functionality, validate forms, detectbrowsers, and much more.

    8/9/2010 2Hassan Khan

  • 8/7/2019 09 Javascript

    3/22

    What is JavaScript?

    JavaScript was designed to add interactivity to HTML

    JavaScript is a scripting language

    A scripting language is a lightweight programminglanguage

    JavaScript is usually embedded directly into HTMLpages

    ava cr p s an n erpre e anguage means a

    scripts execute without preliminary compilation) Everyone can use JavaScript without purchasing a

    license

    8/9/2010 3Hassan Khan

  • 8/7/2019 09 Javascript

    4/22

    Example

    document.write("This is my first JavaScript!");

    8/9/2010 4Hassan Khan

  • 8/7/2019 09 Javascript

    5/22

    How to Handle Simple

    Browsers

    Browsers that do not support JavaScript, will display

    .

    The HTML comment tag should be used to "hide" the

    JavaScript.

    Just add an HTML comment ta (end of comment) afterthe last JavaScript statement.

    8/9/2010 5Hassan Khan

  • 8/7/2019 09 Javascript

    6/22

    Example

    the JavaScript comment symbol. This preventsJavaScript from executing the --> tag.

    8/9/2010 6Hassan Khan

  • 8/7/2019 09 Javascript

    7/22

    What can a JavaScript do?

    JavaScript gives HTML designers a programming

    oo

    JavaScript can put dynamic text into an HTML pageJavaScript can react to events

    JavaScript can read and write HTML elements

    JavaScript can be used to validate data

    ava cr p can e use o e ec e v s or s

    browser JavaScript can be used to create cookies

    8/9/2010 7Hassan Khan

  • 8/7/2019 09 Javascript

    8/22

    How To Use?

    tag is used to insert a JavaScript into

    an HTML page.

    Between tag

    e ween < ea > ag

    8/9/2010 8Hassan Khan

  • 8/7/2019 09 Javascript

    9/22

    How To Use (Cont.)?

    JavaScripts in the body section will be

    executed WHILE the page loads.

    JavaScripts in the head section will be.

    8/9/2010 9Hassan Khan

  • 8/7/2019 09 Javascript

    10/22

    JavaScript Statements

    JavaScript is case sensitive.

    Use of semicolon(;) in the end of statement isoptional.

    document.write("Hello");

    document.write("Hello")

    8/9/2010 10Hassan Khan

  • 8/7/2019 09 Javascript

    11/22

    JavaScript Comments

    Single line comments start with //.

    Multi line comments start with /* and end with.

    8/9/2010 11Hassan Khan

  • 8/7/2019 09 Javascript

    12/22

    JavaScript Variables JavaScript variables are used to hold values

    x=5, y=6, z=x+y

    Rules for JavaScript variable names:

    Variable names are case sensitive (y and Y

    are two different variables)

    Variable names must begin with a letter orthe underscore character

    8/9/2010 12Hassan Khan

  • 8/7/2019 09 Javascript

    13/22

    JavaScript Variables

    Declaration

    var x;var carname;

    Assign Values

    x= ;

    carname=Toyota";

    8/9/2010 13Hassan Khan

  • 8/7/2019 09 Javascript

    14/22

    JavaScript Arithmetic Operators

    Y=5

    8/9/2010 14Hassan Khan

  • 8/7/2019 09 Javascript

    15/22

    JavaScript Assignment Operators

    x = 5 and y=10

    8/9/2010 15Hassan Khan

  • 8/7/2019 09 Javascript

    16/22

    + Operator and Strings

    To add two or more string variables together,

    use the + operator.

    8/9/2010 16Hassan Khan

  • 8/7/2019 09 Javascript

    17/22

    Comparison Operators

    8/9/2010 17Hassan Khan

  • 8/7/2019 09 Javascript

    18/22

    Logical Operators

    8/9/2010 18Hassan Khan

  • 8/7/2019 09 Javascript

    19/22

    If...Else Statements

    used to perform different actions based on different

    . if statement - use this statement to execute some

    code only if a specified condition is true

    if...else statement - use this statement to executesome code if the condition is true and another codeif the condition is false

    ...e se ....e se s a emen - use s s a emen o

    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

    8/9/2010 19Hassan Khan

  • 8/7/2019 09 Javascript

    20/22

    Example (If)

    8/9/2010 20Hassan Khan

  • 8/7/2019 09 Javascript

    21/22

    Example (If...else)

    8/9/2010 21Hassan Khan

  • 8/7/2019 09 Javascript

    22/22

    Example(If...else if...else)

    8/9/2010 22Hassan Khan