SlideShare a Scribd company logo
Introduction to
        JavaScript Development
                           The Magic of Dynamic Web Pages


Doncho Minkov
Technical Trainer
http://minkov.it
Telerik Software Academy
academy.telerik.com
Table of Contents
   Dynamic HTML
   How to Create DHTML?
     XHTML, CSS, JavaScript, DOM
   Intro to JavaScript
     History
     JavaScript in Web Pages
    JavaScript Syntax
   Pop-up boxes
    Debugging in JavaScript
                                               2
Dynamic HTML
Dynamic Behavior at the Client Side
What is DHTML?
 Dynamic HTML (DHTML)

  Makes possible a Web page to react and change
   in response to the user’s actions
 DHTML consists of HTML   + CSS + JavaScript

                    DHTML

  XHTML       CSS      JavaScript     DOM

                                                   4
DTHML = HTML + CSS + JavaScript
 HTML defines Web sitescontent through
 semantic tags (headings, paragraphs, lists, …)
 CSS defines 'rules'
                    or 'styles' for presenting
 every aspect of an HTML document
   Font (family, size, color, weight, etc.)
   Background (color, image, position, repeat)
   Position and layout (of any object on the page)
 JavaScript   defines dynamic behavior
   Programming logic for interaction with the
    user, to handle events, etc.
                                                      5
JavaScript
Dynamic Behavior in a Web Page
JavaScript
 JavaScript
           is a front-end scripting language
 developed by Netscape for dynamic content
   Lightweight, but with limited capabilities
   Can be used as object-oriented language
   Embedded in your HTML page
   Interpreted by the Web browser
 Client-side, mobile and desktop technology

 Simple and flexible

 Powerful to manipulate the DOM
                                                   7
JavaScript Advantages
 JavaScript   allows interactivity such as:
   Implementing form validation
   React to user actions, e.g. handle keys
   Changing an image on moving mouse over it
   Sections of a page appearing and disappearing
   Content loading and changing dynamically
   Performing complex calculations
   Custom HTML controls, e.g. scrollable table
   Implementing AJAX functionality
                                                    8
What Can JavaScript Do?
 Can handle events

 Can read and write HTML elements and
 modify the DOM tree
 Can validate   form data
 Can access / modify browser cookies

 Can detect the user’s   browser and OS
 Can be used as object-oriented language

 Can handle exceptions

 Can perform asynchronous     server calls (AJAX)
                                                     9
The First Script

<html>

<body>
  <script type="text/javascript">
     alert('Hello JavaScript!');
  </script>
</body>

</html>




                                                 10
First JavaScript
    Live Demo
Using JavaScript Code
 The JavaScript   code can be placed in:
   <script> tag in the head
   <script> tag in the body - not recommended
   External files, linked via <script> tag the head
    Files usually have .js extension
    <script src="scripts.js" type="text/javscript">
    <!– code placed here will not be executed! -->
    </script>

    Highly recommended
    The .js files get cached by the browser
                                                       12
JavaScript – When is Executed?
 JavaScriptcode is executed during the page
 loading or when the browser fires an event
   All statements are executed at page loading
   Some statements just define functions that can
    be called later
 Function calls
              or code can be attached as
 "event handlers" via tag attributes
   Executed when the event is fired by the browser
  <img src="logo.gif" onclick="alert('clicked!')" />

                                                       13
Calling a JavaScript Function
         from Event Handler – Example
<html>
<head>
<script type="text/javascript">
  function test (message) {
    alert(message);
  }
</script>
</head>

<body>
  <img src="logo.gif"
    onclick="test('clicked!')" />
</body>
</html>

                                           14
Event Handlers
    Live Demo
Using External Script Files
   Using external script files:
    <html>                       external-JavaScript.html
    <head>
      <script src="sample.js" type="text/javascript">
      </script>
    </head>          The <script> tag is always empty.
    <body>
      <button onclick="sample()" value="Call JavaScript
        function from sample.js" />
    </body>
    </html>

   External JavaScript file:
    function sample() {
      alert('Hello from sample.js!')
    }                                          sample.js
                                                            16
External JavaScript Files
         Live Demo
The JavaScript
   Syntax
JavaScript Syntax
 The JavaScript   syntax is similar to C#
   Operators (+, *, =, !=, &&, ++, …)
   Variables (typeless)
   Conditional statements (if, else)
   Loops (for, while)
   Arrays (my_array[]) and associative arrays
    (my_array['abc'])
   Functions (can return value)
   Function variables (like the C# delegates)
                                                 19
Standard Popup Boxes
 Alert box with text and [OK] button

   Just a message shown in a dialog box:
    alert("Some text here");

 Confirmation box

   Contains text, [OK] button and [Cancel] button:
    confirm("Are you sure?");

 Prompt box

   Contains text, input field with default value:
    prompt ("enter amount", 10);
                                                      20
Popup Boxes
  Live Demo
The Built-In
Browser Objects
Built-in Browser Objects
 The browser provides some read-only data via:

  window
    The top node of the DOM tree
    Represents the browser's window
  document
    holds information the current loaded document
  screen
    Holds the user’s display properties
  browser
    Holds information about the browser
                                                     23
DOM Hierarchy – Example


                            window



navigator   screen      document        history   location


                     form      form


                        button        form



                                                             24
Opening New Window – Example
 window.open()

                                window-open.html
 var newWindow = window.open("", "sampleWindow",
   "width=300, height=100, menubar=yes,
   status=yes, resizable=yes");

 newWindow.document.write(
   "<html><head><title>
   Sample Title</title>
   </head><body><h1>Sample
   Text</h1></body>");
 newWindow.status =
   "Hello folks";


                                                   25
The Navigator Object

       alert(window.navigator.userAgent);



The browser   The navigator in the   The userAgent
  window       browser window         (browser ID)




                                                     26
The Screen Object
 The screen object contains   information about
 the display

     window.moveTo(0, 0);
     x = screen.availWidth;
     y = screen.availHeight;
     window.resizeTo(x, y);




                                                   27
Document and Location
 document object

  Provides some built-in arrays of specific objects
   on the currently loaded Web page
  document.links[0].href = "yahoo.com";
  document.write(
    "This is some <b>bold text</b>");
 document.location

  Used to access the currently open URL or
   redirect the browser
  document.location = "http://www.yahoo.com/";
                                                       28
Built-In Browser Objects
         Live Demo
Other JavaScript Objects
The Math Object
 The Math object provides some mathematical
 functions
                                   math.html
  for (i=1; i<=20; i++) {
    var x = Math.random();
    x = 10*x + 1;
    x = Math.floor(x);
    document.write(
      "Random number (" +
      i + ") in range " +
      "1..10 --> " + x +
      "<br/>");
  }

                                               31
The Date Object
 The Date object provides date / calendar
 functions
                                     dates.html
  var now = new Date();
  var result = "It is now " + now;
  document.getElementById("timeField")
    .innerText = result;
  ...
  <p id="timeField"></p>




                                                  32
Timers: setTimeout()
   Make something happen (once) after a fixed
    delay

     var timer = setTimeout('bang()', 5000);

                   5 seconds after this statement
                   executes, this function is called

     clearTimeout(timer);

           Cancels the timer
                                                       33
Timers: setInterval()
   Make something happen repeatedly at fixed
    intervals

    var timer = setInterval('clock()', 1000);

                        This function is called
                      continuously per 1 second.

    clearInterval(timer);

             Stop the timer.

                                                   34
Timer – Example
timer-demo.html
<script type="text/javascript">
  function timerFunc() {
    var now = new Date();
    var hour = now.getHours();
    var min = now.getMinutes();
    var sec = now.getSeconds();
    document.getElementById("clock").value =
      "" + hour + ":" + min + ":" + sec;
  }
  setInterval('timerFunc()', 1000);
</script>
<input type="text" id="clock" />

                                               35
Other JavaScript Objects
         Live Demo
Debugging JavaScript
Debugging JavaScript
 Modern browsers have JavaScript   console
 where errors in scripts are reported
   Errors may differ across browsers
 Several tools to debug JavaScript

   Microsoft Script Editor
    Add-on for Internet Explorer
    Supports breakpoints, watches
    JavaScript statement debugger; opens the script
     editor

                                                       38
Firebug
 Firebug – Firefox add-on for debugging
 JavaScript, CSS, HTML
   Supports breakpoints, watches, JavaScript
    console editor
   Very useful for CSS and HTML too
    You can edit all the document real-time: CSS,
     HTML, etc
    Shows how CSS rules apply to element
   Shows Ajax requests and responses
   Firebug is written mostly in JavaScript
                                                      39
Firebug (2)




              40
JavaScript Console Object
 The console object exists
                          only if there is a
 debugging tool that supports it
  Used to write log messages at runtime
 Methods of the console object:

  debug(message)
  info(message)
  log(message)
  warn(message)
  error(message)
                                               41
Introduction JavaScript
             Development




Questions?
01 Introduction - JavaScript Development

More Related Content

What's hot (18)

PDF
JavaScript and BOM events
Jussi Pohjolainen
 
ODP
Dojo: Beautiful Web Apps, Fast
Gabriel Hamilton
 
PPTX
Jquery dojo slides
helenmga
 
PDF
Arquitetando seu aplicativo Android com Jetpack
Nelson Glauber Leal
 
PDF
Evolve your coding with some BDD
Ortus Solutions, Corp
 
PDF
Tapestry 5: Java Power, Scripting Ease
Howard Lewis Ship
 
PDF
Codemash-Tapestry.pdf
Howard Lewis Ship
 
PDF
Skills Matter Itbo April2010 Tapestry
Skills Matter
 
PPT
Jquery
adm_exoplatform
 
PPTX
Présentation et bonnes pratiques du pattern MVVM - MIC Belgique
Denis Voituron
 
PPTX
A test framework out of the box - Geb for Web and mobile
GlobalLogic Ukraine
 
PPTX
Java script
Adrian Caetano
 
PDF
Alfredo-PUMEX
tutorialsruby
 
PDF
RicoLiveGrid
tutorialsruby
 
PDF
Web2 - jQuery
voicerepublic
 
PPTX
A Rich Web Experience with jQuery, Ajax and .NET
James Johnson
 
PDF
td_mxc_rubyrails_shin
tutorialsruby
 
PDF
Web2.0 with jQuery in English
Lau Bech Lauritzen
 
JavaScript and BOM events
Jussi Pohjolainen
 
Dojo: Beautiful Web Apps, Fast
Gabriel Hamilton
 
Jquery dojo slides
helenmga
 
Arquitetando seu aplicativo Android com Jetpack
Nelson Glauber Leal
 
Evolve your coding with some BDD
Ortus Solutions, Corp
 
Tapestry 5: Java Power, Scripting Ease
Howard Lewis Ship
 
Codemash-Tapestry.pdf
Howard Lewis Ship
 
Skills Matter Itbo April2010 Tapestry
Skills Matter
 
Présentation et bonnes pratiques du pattern MVVM - MIC Belgique
Denis Voituron
 
A test framework out of the box - Geb for Web and mobile
GlobalLogic Ukraine
 
Java script
Adrian Caetano
 
Alfredo-PUMEX
tutorialsruby
 
RicoLiveGrid
tutorialsruby
 
Web2 - jQuery
voicerepublic
 
A Rich Web Experience with jQuery, Ajax and .NET
James Johnson
 
td_mxc_rubyrails_shin
tutorialsruby
 
Web2.0 with jQuery in English
Lau Bech Lauritzen
 

Viewers also liked (17)

PPTX
Learn Javascript Basics
Khushiar
 
PPT
JAVA SCRIPT
Go4Guru
 
PPT
JavaScript Introduction
Charles Russell
 
PPTX
An introduction to javascript
tonyh1
 
PDF
An Introduction to JavaScript: Week One
Event Handler
 
PPTX
JavaScript Introduction
Designveloper
 
PPTX
Front-end development introduction (JavaScript). Part 2
Oleksii Prohonnyi
 
PPT
The JavaScript Programming Language
Raghavan Mohan
 
PPT
introduction to javascript
Kumar
 
PDF
Basics of JavaScript
Bala Narayanan
 
PPTX
Java script
reddivarihareesh
 
PDF
Introduction to web programming with JavaScript
T11 Sessions
 
PDF
Reactive Programming with JavaScript
Codemotion
 
PDF
Introduction to JavaScript
Bryan Basham
 
PPT
Javascript
guest03a6e6
 
PPT
JavaScript - An Introduction
Manvendra Singh
 
PPTX
JavaScript code academy - introduction
Jaroslav Kubíček
 
Learn Javascript Basics
Khushiar
 
JAVA SCRIPT
Go4Guru
 
JavaScript Introduction
Charles Russell
 
An introduction to javascript
tonyh1
 
An Introduction to JavaScript: Week One
Event Handler
 
JavaScript Introduction
Designveloper
 
Front-end development introduction (JavaScript). Part 2
Oleksii Prohonnyi
 
The JavaScript Programming Language
Raghavan Mohan
 
introduction to javascript
Kumar
 
Basics of JavaScript
Bala Narayanan
 
Java script
reddivarihareesh
 
Introduction to web programming with JavaScript
T11 Sessions
 
Reactive Programming with JavaScript
Codemotion
 
Introduction to JavaScript
Bryan Basham
 
Javascript
guest03a6e6
 
JavaScript - An Introduction
Manvendra Singh
 
JavaScript code academy - introduction
Jaroslav Kubíček
 
Ad

Similar to 01 Introduction - JavaScript Development (20)

PDF
Java script
Ramesh Kumar
 
PPTX
JavaScript lesson 1.pptx
MuqaddarNiazi1
 
PDF
8.-Javascript-report powerpoint presentation
JohnLagman3
 
PDF
JavaScript
tutorialsruby
 
PDF
JavaScript
tutorialsruby
 
PPT
Learn javascript easy steps
prince Loffar
 
PPTX
Javascript
Sun Technlogies
 
PDF
WT UNIT 2 presentation :client side technologies JavaScript And Dom
SrushtiGhise
 
PDF
Lecture7
Majid Taghiloo
 
PPTX
Learning About JavaScript (…and its little buddy, JQuery!)
Julie Meloni
 
PDF
Training javascript 2012 hcmut
University of Technology
 
PPTX
Java Script basics and DOM
Sukrit Gupta
 
PPT
Java script
vishal choudhary
 
PDF
Kann JavaScript elegant sein?
jbandi
 
PPT
lecture 6 javascript event and event handling.ppt
ULADATZ
 
PPTX
Java script Basic
Jaya Kumari
 
PPT
INTRO TO JAVASCRIPT basic to adcance.ppt
testvarun21
 
PPTX
Javascript note for engineering notes.pptx
engineeradda55
 
Java script
Ramesh Kumar
 
JavaScript lesson 1.pptx
MuqaddarNiazi1
 
8.-Javascript-report powerpoint presentation
JohnLagman3
 
JavaScript
tutorialsruby
 
JavaScript
tutorialsruby
 
Learn javascript easy steps
prince Loffar
 
Javascript
Sun Technlogies
 
WT UNIT 2 presentation :client side technologies JavaScript And Dom
SrushtiGhise
 
Lecture7
Majid Taghiloo
 
Learning About JavaScript (…and its little buddy, JQuery!)
Julie Meloni
 
Training javascript 2012 hcmut
University of Technology
 
Java Script basics and DOM
Sukrit Gupta
 
Java script
vishal choudhary
 
Kann JavaScript elegant sein?
jbandi
 
lecture 6 javascript event and event handling.ppt
ULADATZ
 
Java script Basic
Jaya Kumari
 
INTRO TO JAVASCRIPT basic to adcance.ppt
testvarun21
 
Javascript note for engineering notes.pptx
engineeradda55
 
Ad

More from Tommy Vercety (9)

ODP
09. Strings
Tommy Vercety
 
ODP
08. Objects
Tommy Vercety
 
ODP
07. Functions
Tommy Vercety
 
ODP
06. Arrays
Tommy Vercety
 
ODP
05. Loops
Tommy Vercety
 
ODP
04. Conditional Statements
Tommy Vercety
 
ODP
03. Operators - Expressions
Tommy Vercety
 
PPT
02. Data Type and Variables
Tommy Vercety
 
PPTX
00 JavaScript Part 1 Course - Introduction
Tommy Vercety
 
09. Strings
Tommy Vercety
 
08. Objects
Tommy Vercety
 
07. Functions
Tommy Vercety
 
06. Arrays
Tommy Vercety
 
05. Loops
Tommy Vercety
 
04. Conditional Statements
Tommy Vercety
 
03. Operators - Expressions
Tommy Vercety
 
02. Data Type and Variables
Tommy Vercety
 
00 JavaScript Part 1 Course - Introduction
Tommy Vercety
 

Recently uploaded (20)

PPTX
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Introduction to Indian Writing in English
Trushali Dodiya
 
infertility, types,causes, impact, and management
Ritu480198
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
epi editorial commitee meeting presentation
MIPLM
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 

01 Introduction - JavaScript Development

  • 1. Introduction to JavaScript Development The Magic of Dynamic Web Pages Doncho Minkov Technical Trainer http://minkov.it Telerik Software Academy academy.telerik.com
  • 2. Table of Contents  Dynamic HTML  How to Create DHTML?  XHTML, CSS, JavaScript, DOM  Intro to JavaScript  History  JavaScript in Web Pages  JavaScript Syntax  Pop-up boxes  Debugging in JavaScript 2
  • 3. Dynamic HTML Dynamic Behavior at the Client Side
  • 4. What is DHTML?  Dynamic HTML (DHTML)  Makes possible a Web page to react and change in response to the user’s actions  DHTML consists of HTML + CSS + JavaScript DHTML XHTML CSS JavaScript DOM 4
  • 5. DTHML = HTML + CSS + JavaScript  HTML defines Web sitescontent through semantic tags (headings, paragraphs, lists, …)  CSS defines 'rules' or 'styles' for presenting every aspect of an HTML document  Font (family, size, color, weight, etc.)  Background (color, image, position, repeat)  Position and layout (of any object on the page)  JavaScript defines dynamic behavior  Programming logic for interaction with the user, to handle events, etc. 5
  • 7. JavaScript  JavaScript is a front-end scripting language developed by Netscape for dynamic content  Lightweight, but with limited capabilities  Can be used as object-oriented language  Embedded in your HTML page  Interpreted by the Web browser  Client-side, mobile and desktop technology  Simple and flexible  Powerful to manipulate the DOM 7
  • 8. JavaScript Advantages  JavaScript allows interactivity such as:  Implementing form validation  React to user actions, e.g. handle keys  Changing an image on moving mouse over it  Sections of a page appearing and disappearing  Content loading and changing dynamically  Performing complex calculations  Custom HTML controls, e.g. scrollable table  Implementing AJAX functionality 8
  • 9. What Can JavaScript Do?  Can handle events  Can read and write HTML elements and modify the DOM tree  Can validate form data  Can access / modify browser cookies  Can detect the user’s browser and OS  Can be used as object-oriented language  Can handle exceptions  Can perform asynchronous server calls (AJAX) 9
  • 10. The First Script <html> <body> <script type="text/javascript"> alert('Hello JavaScript!'); </script> </body> </html> 10
  • 11. First JavaScript Live Demo
  • 12. Using JavaScript Code  The JavaScript code can be placed in:  <script> tag in the head  <script> tag in the body - not recommended  External files, linked via <script> tag the head  Files usually have .js extension <script src="scripts.js" type="text/javscript"> <!– code placed here will not be executed! --> </script>  Highly recommended  The .js files get cached by the browser 12
  • 13. JavaScript – When is Executed?  JavaScriptcode is executed during the page loading or when the browser fires an event  All statements are executed at page loading  Some statements just define functions that can be called later  Function calls or code can be attached as "event handlers" via tag attributes  Executed when the event is fired by the browser <img src="logo.gif" onclick="alert('clicked!')" /> 13
  • 14. Calling a JavaScript Function from Event Handler – Example <html> <head> <script type="text/javascript"> function test (message) { alert(message); } </script> </head> <body> <img src="logo.gif" onclick="test('clicked!')" /> </body> </html> 14
  • 15. Event Handlers Live Demo
  • 16. Using External Script Files  Using external script files: <html> external-JavaScript.html <head> <script src="sample.js" type="text/javascript"> </script> </head> The <script> tag is always empty. <body> <button onclick="sample()" value="Call JavaScript function from sample.js" /> </body> </html>  External JavaScript file: function sample() { alert('Hello from sample.js!') } sample.js 16
  • 18. The JavaScript Syntax
  • 19. JavaScript Syntax  The JavaScript syntax is similar to C#  Operators (+, *, =, !=, &&, ++, …)  Variables (typeless)  Conditional statements (if, else)  Loops (for, while)  Arrays (my_array[]) and associative arrays (my_array['abc'])  Functions (can return value)  Function variables (like the C# delegates) 19
  • 20. Standard Popup Boxes  Alert box with text and [OK] button  Just a message shown in a dialog box: alert("Some text here");  Confirmation box  Contains text, [OK] button and [Cancel] button: confirm("Are you sure?");  Prompt box  Contains text, input field with default value: prompt ("enter amount", 10); 20
  • 21. Popup Boxes Live Demo
  • 23. Built-in Browser Objects  The browser provides some read-only data via:  window  The top node of the DOM tree  Represents the browser's window  document  holds information the current loaded document  screen  Holds the user’s display properties  browser  Holds information about the browser 23
  • 24. DOM Hierarchy – Example window navigator screen document history location form form button form 24
  • 25. Opening New Window – Example  window.open() window-open.html var newWindow = window.open("", "sampleWindow", "width=300, height=100, menubar=yes, status=yes, resizable=yes"); newWindow.document.write( "<html><head><title> Sample Title</title> </head><body><h1>Sample Text</h1></body>"); newWindow.status = "Hello folks"; 25
  • 26. The Navigator Object alert(window.navigator.userAgent); The browser The navigator in the The userAgent window browser window (browser ID) 26
  • 27. The Screen Object  The screen object contains information about the display window.moveTo(0, 0); x = screen.availWidth; y = screen.availHeight; window.resizeTo(x, y); 27
  • 28. Document and Location  document object  Provides some built-in arrays of specific objects on the currently loaded Web page document.links[0].href = "yahoo.com"; document.write( "This is some <b>bold text</b>");  document.location  Used to access the currently open URL or redirect the browser document.location = "http://www.yahoo.com/"; 28
  • 31. The Math Object  The Math object provides some mathematical functions math.html for (i=1; i<=20; i++) { var x = Math.random(); x = 10*x + 1; x = Math.floor(x); document.write( "Random number (" + i + ") in range " + "1..10 --> " + x + "<br/>"); } 31
  • 32. The Date Object  The Date object provides date / calendar functions dates.html var now = new Date(); var result = "It is now " + now; document.getElementById("timeField") .innerText = result; ... <p id="timeField"></p> 32
  • 33. Timers: setTimeout()  Make something happen (once) after a fixed delay var timer = setTimeout('bang()', 5000); 5 seconds after this statement executes, this function is called clearTimeout(timer); Cancels the timer 33
  • 34. Timers: setInterval()  Make something happen repeatedly at fixed intervals var timer = setInterval('clock()', 1000); This function is called continuously per 1 second. clearInterval(timer); Stop the timer. 34
  • 35. Timer – Example timer-demo.html <script type="text/javascript"> function timerFunc() { var now = new Date(); var hour = now.getHours(); var min = now.getMinutes(); var sec = now.getSeconds(); document.getElementById("clock").value = "" + hour + ":" + min + ":" + sec; } setInterval('timerFunc()', 1000); </script> <input type="text" id="clock" /> 35
  • 38. Debugging JavaScript  Modern browsers have JavaScript console where errors in scripts are reported  Errors may differ across browsers  Several tools to debug JavaScript  Microsoft Script Editor  Add-on for Internet Explorer  Supports breakpoints, watches  JavaScript statement debugger; opens the script editor 38
  • 39. Firebug  Firebug – Firefox add-on for debugging JavaScript, CSS, HTML  Supports breakpoints, watches, JavaScript console editor  Very useful for CSS and HTML too  You can edit all the document real-time: CSS, HTML, etc  Shows how CSS rules apply to element  Shows Ajax requests and responses  Firebug is written mostly in JavaScript 39
  • 41. JavaScript Console Object  The console object exists only if there is a debugging tool that supports it  Used to write log messages at runtime  Methods of the console object:  debug(message)  info(message)  log(message)  warn(message)  error(message) 41
  • 42. Introduction JavaScript Development Questions?