Java Script - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Java Script

Description:

Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript Comments – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 22
Provided by: muh68
Category:
Tags: java | script

less

Transcript and Presenter's Notes

Title: Java Script


1
Java Script
  • About Java Script
  • Document Object Model
  • Incorporating JavaScript
  • Adding JavaScript to HTML
  • Embedding a Javascript
  • External Scripts
  • Javascript Comments
  • Javascript rules
  • Javascript Variables

2
About Java Script
  • Scripting language
  • Gives developer control over pages
  • Change content
  • Scrolling messages
  • Image rollovers
  • Validate content of a form
  • Make calculations
  • Detect the browser in use and display different
    content
  • Alert and Redirect user
  • Pop-up Windows
  • Detect a plug-in and notify user if plug-in is
    installed
  • Loops
  • Perform a repetitive task display a series of
    pictures
  • Case sensitive

3
What is DOM?
  • Document Object Model (DOM)
  • The Document Object Model is a platform- and
    language-neutral interface that will allow
    programs and scripts to dynamically access and
    update the content, structure and style of
    documents.

4
Document Object Model
  • An object is something (Web page)
  • Objects perform methods (show an image)
  • Methods have properties which can be modified
  • Image
  • File
  • Height/Width
  • Manipulate properties with JavaScript

5
Incorporating JavaScript
  • Use the ltSCRIPTgt tag
  • Insert into your Web page
  • Within ltHEADgt tags
  • Within ltBODYgt tags
  • Ignore JavaScript
  • lt! --
  • // -- gt

6
Adding JavaScript to HTML
  • Inlineltscript languageJavaScriptgtlt!Hides
    script from old browsers Code goes
    here//?ltscriptgt
  • External Fileltscript languageJavaScript
    srcfile.js gtlt/scriptgt

7
Embedding a Javascript
  • Individual commands on a single line can be
    separated with semi-colons
  • Best practice is to use one command per line, and
    end line with semi-colon
  • Start withltSCRIPT LANGUAGEJavascriptgt
  • End withlt/SCRIPTgt

8
Example of Embedded Script
  • Script bounded by SCRIPT tag
  • Document and location are objects
  • Write is a method (you can tell that by the
    parentheses)

ltSCRIPT LANGUAGEJavaScriptgt document.write("Hello
world!ltBRgt") lt/SCRIPTgt
9
External Scripts
  • Javascripts need not be in the html page on which
    they will be displayed
  • By convention, external scripts end in .js
  • They contain no raw HTML (although you can used a
    document.write to pass HTML to the browser)
  • Use a src statement to pull it into the html
    pageltSCRIPT LANGUAGE"JavaScript"
    src"date_modified.js"gt

10
Javascript Comments
  • Single line comments start with ////this is a
    comment
  • Multiple line comments start with / and end with
    //This is a multiple line comment so you can
    drone on and on and one as much as you care to/

11
Javascript rules
  • It is (generally) case sensitive
  • Separate statements.
  • Individual lines
  • Semicolons (eg. A 2 B 3
  • Reserved word (cannot be used as identifiers)
    break, do, if, else.

12
Javascript Variables
  • Cannot begin with a number
  • Cannot be a reserved word
  • Can only contain letters, numbers or underscores
  • Should be declared by var statement (you can get
    away without it sometimes, but its better to do
    it as a matter of habit).

13
Conversation
  • A variable of one type can be used as if it were
    another.
  • If there's a conflict, javascript doesn't produce
    an exception
  • stringnumber goes to strings
  • booleanstring goes to strings
  • numberboolean goes to numbers
  • Explicit conversions
  • string to an integer, use the parseInt method.
  • string to a number, use the parseFloat method.

14
Variables and Data Types
  • JavaScript is a loosely typed language
  • Data types are converted during execution as
    needed
  • Data typing only matters during operations
  • 6 67 667 String
  • 6 67 73

15
Variables and Data Types
  • Numbers
  • Integer and floating-point numbers.
  • Booleans
  • True or false. Can not use as 1 or 0 but 0
    false
  • Strings
  • Anything surrounded by or ex. My String
    My String
  • Object
  • myObj new Object()
  • Null
  • Not the same as zero - no value at all.
  • Undefined
  • The Variable has been created but no value has
    been assigned to it

16
Operators
  • Arithmetic (the usual suspects)
  • ,-,,/,,--,
  • Comparison
  • , !, gt, gt, lt, lt
  • Boolean
  • , , !

17
Functions
  • The function definition is a statement which
    describes the function its name, any values
    (known as "arguments") which it accepts incoming,
    and the statements of which the function is
    comprised.
  • function funcName(argument1,argument2,etc)
    statements

18
Functions
  • Examplefunction foo(myString) document.write(m
    yString)foo(Computers are fun)

19
JavaScript Objects
  • To declare an objectvar myObj new Object()
  • To set propertiesmyObj.name blah

20
Real Life Examples
  • Form Validation
  • Create function to run on forms onSubmit event
  • ltform name"myForm" id"myForm" action""
    method"get" onsubmit"return checkForm(this)"gt
  • ltinput type"text" name"firstName"
    id"firstName" value"" /gt
  • ltinput type"submit" /gt
  • lt/formgt

21
Real Life Examples
  • Form Validation
  • function checkForm(theForm) if(theForm"firstNam
    e".value "") alert("You must enter your
    first name") theForm"firstName".focus() re
    turn false
  • return true
Write a Comment
User Comments (0)
About PowerShow.com