Java Script Word Doc

Embed Size (px)

Citation preview

  • 8/10/2019 Java Script Word Doc

    1/71

    Java script

    JavaScript is the scripting language of the Web.JavaScript is used in millions of Web pages to add functionality, validate

    forms, detect browsers, and much more. JavaScript is easy to learn! You willenjoy it!

  • 8/10/2019 Java Script Word Doc

    2/71

    1. What is JavaScript?

  • 8/10/2019 Java Script Word Doc

    3/71

    We're going to study the JavaScript programming language, because it

    is a widelyused scripting language for web pages. With javaScript you

    can mae your "#$% page much attractive, dynamic and you can

    validate your html forms. &efore starting JavaScript you should have

    basic nowledge of "#$%.f you're ready, then,

    %et(s go through bit of code.

    )"#$%*

    )"+-*

    )##%+* irst Script)/##%+*

    )/"+-*

    )&0-Y*

    )S123# %4565+78javascript8*

    documtent.write98Welcome to Siliconindia8:;

    )/S123#*

    )/&0-Y*

    )/"#$%*

    0utput

    document.write98Welcome to Siliconndia8:

    -ocument is part of something called the -ocument 0bject $odel.

    -ocument refers to all the te=t and "#$% elements between the two

    &0-Y tags. nd that includes any attributes inside the &0-Y tag itself.

    %ie table, div bgcolor... #he write9 : method writes te=t 9and numbers

    as well: between the two &0-Y tags on your page. Javascript is case

    sensitive.

    -on't worry if you don't understand some of that the main point is

    that you are up and running, and you've written your ?rst script. #he

    journey has just started.

    t the moment, we have our script between the two &0-Y tags. nd it

    wors perfectly well here. &est practice is to ept S123#S in the

    "+- section of your "#$%. #his is because any code in the "+-

    section will be dealt with ?rst by the browser. nd besides, it's neater

    up there. You're not cluttering up your "#$% code with lots of

    JavaScript.

    )"#$%*

    )"+-*

    )##%+* irst Script)/##%+*

    )S123# %4565+ 78JavaScript8*

    document.write98Welcome to Siliconndia8:;

    )/S123#*

    )/"+-*

    )&0-Y*

    )/&0-Y*

    )/"#$%*

  • 8/10/2019 Java Script Word Doc

    5/71

    -id it mae a di@erence> 4o, it did not. &ut rest assured that your

    script was dealt with browser before anything in the &0-Y section.

    You can also put your scripts into "#$% tags. "ere's the

    document.write9: code inserted into a orm's &utton code We've learnt this %ots, actually. %et's have a

    loo at that document thing again.

    s was mentioned, document is a part of the -ocument 0bject $odel.

    We saw a method that can be used with document, but here's a couple

    more 93roperties and $ethods:.

    3roperties

    C bg1olor C fg1olor

    C title

    C location

    C images

    C forms

    $ethods

    C open9:

    C close9:

    C write9:

    C writeln9:

    #here are Buite a few more 3roperties and $ethods you can use with

    document. %et's see how you can use them in your own scripts.

    )&0-Y*

    )436# #Y3+ 7 &utton A%6+ 7 81olour 0ne8 0nclic 78document.bg1olor 7 '2ed'8*

    )436# #Y3+ 7 &utton A%6+ 7 81olour #wo8 0nclic 7

    8document.bg1olor 7 '&lue'8*

    )436# #Y3+ 7 &utton A%6+ 7 81olour #hree8 0nclic

  • 8/10/2019 Java Script Word Doc

    7/71

    78document.bg1olor7'5reen'8*

    )/&0-Y*

    When you're done, save your wor and view the results in your

    browser. 1lic the button and see what happens.

    #he bacground color of your web page should have changed color

    when you cliced a button. t did this because of the bg1olor poperty of

    the document object.

    document.bg1olor

    fter the eBuals sign, you can either type a name of a colour, or better

    use an "e=adecimal value8:

    Whichever button you clic, the answer will be stored in the variable

    1on?rmStatus. #he answer, in this case, is a &oolean value< it's either #rue or

    alse.

    You do the testing with an if statement. 0ur if statement was as simple as if

    statement get. t was this

  • 8/10/2019 Java Script Word Doc

    20/71

    what if the user cliced 1ancel> What happens then>

    f the 1ancel button is cliced, the 1on?rmStatus variable will be false. n

    that case, our alert bo= statement doesn't get e=ecuted the programme will

    just ignore that line and move on to the ne=t line of code.

    We can, however, add another statement to chec for a false condition.

    1hange your code to this8:

    if 91on?rmStatus 77 true:

    alert98Yes already registered. 8:

    T

    if 91on?rmStatus 77 false:

    alert9840, not yet registered.8:

    T

    )/S123#*

    8f it's true, do one thing; if it's false do another thing8

    i 7 else statements

    We saw in the last part of this 1ontrol low section that to test two conditions

    9true and false: two if statements were needed. &ut you don't have to use

    separate if statements. nstead, you can use the else word. #his is ideal for&oolean values. "ere's our code again, this time with an if U else statement.

    1on?rmStatus 7 con?rm98did u registered with Siliconindia>8:

    if 91on?rmStatus 77 true:

  • 8/10/2019 Java Script Word Doc

    21/71

    alert98Yes already registered. 8:;

    T

    else

    alert9840, not yet registered.8:;

    T

    #he format for an if U else statement is this8:

    T

    else if9age * KG:

    alert98t's tough being an adult8:

    T

    T

    )/S123#*

    We had another on1lic event for our command button, which called the

    message9 : function. When the message9 : is called, it ?rst stores the value in

    the te=t bo= into a variable called age.

    age 7 document.ge#est.t=tge.value

    4e=t, we need to test what is inside the age variable. We started the test

    with an if statement

    What we're not doing is returned the te=t from the chec bo=es. #he te=t for

    our chec bo=es was 81ool layout8, 8+asy to 4avigate8, etc. We're not

    passing these values bac when the Submit button is cliced. ll the same,

    we'll see how this is done.

    0n our orm, the 1hec bo= "#$% code was this