HTML CSS JavaScript Basics
HTML CSS JavaScript Basics
Table of Contents
1.
Introduction to HTML
How the Web Works? What is a Web Page? My First HTML Page
HTML in Details
The <!DOCTYPE> Declaration The <head> Section: Title, Meta, Script, Style
2
HTML in Details
The <body> Section Text Styling and Formatting Tags Hyperlinks: <a>, Hyperlinks and Sections
Images: <img>
Lists: <ol>, <ul> and <dl>
3. 4. 5.
HTML
Language
extension
HTML files can be created with text editors:
Netscape Composer
Microsoft Word
Visual Studio
HTML Basics
Text, Images, Tables, Forms
HTML Structure
HTML describes structure using two main sections: <head> and <body>
The HTML source code should be formatted to increase readability and facilitate debugging.
Every block element should start on a new line.
Every nested (block) element should be indented. Browsers ignore multiple whitespaces in the page source, so formatting is harmless.
sacrificed
10
An HTML element consists of an opening tag, a closing tag and the content inside.
11
HTML header
12
HTML body
13
This text is <em>emphasized.</em> <br />new line<br /> This one is <strong>more emphasized.</strong>
14
15
16
Tags Attributes
Tags can have attributes
Example:
Content of title attribute is displayed as hint when the element is hovered with the mouse
Some elements have obligatory attributes
17
headings.html
<!DOCTYPE HTML> <html> <head><title>Headings and paragraphs</title></head> <body> <h1>Heading 1</h1> <h2>Sub heading 2</h2> <h3>Sub heading 3</h3>
<p>This is my first paragraph</p> <p>This is my second paragraph</p> <div style="background:skyblue"> This is a div</div> </body> </html>
19
headings.html
<!DOCTYPE HTML> <html> <head><title>Headings and paragraphs</title></head> <body> <h1>Heading 1</h1> <h2>Sub heading 2</h2> <h3>Sub heading 3</h3>
<p>This is my first paragraph</p> <p>This is my second paragraph</p> <div style="background:skyblue"> This is a div</div> </body> </html>
20
Introduction to HTML
HTML Document Structure in Depth
Preface
It is important to have the correct vision
and
22
Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Tags and attribute names must be in lowercase All tags must be closed (<br/>, <img/>) while HTML allows <br> and <img> and implies missing closing tags (<p>par1 <p>par2) XHTML allows only one root <html> element (HTML allows more than one)
24
are deprecated in XHTML, most are moved to CSS minimization is forbidden, e.g.
Starts after the <!doctype> declaration Begins with <head> and ends with </head> Contains
<meta>
<script>
<style> <!- comments -->
26
Used to specify a title in the window title bar Search engines and people rely on titles
27
28
languages:
scripts-example.html
30
The <style> element embeds formatting information (CSS styles) into an HTML page
<html> style-example.html <head> <style type="text/css"> p { font-size: 12pt; line-height: 12pt; } p:first-letter { font-size: 200%; } span { text-transform: uppercase; } </style> </head> <body> <p>Styles demo.<br /> <span>Test uppercase</span>. </p> </body> </html>
31
<html></html> tags
Comments start with <!-- and end with -->
<!- Telerik Logo (a JPG file) --> <img src="logo.jpg" alt=Telerik Logo"> <!- Hyperlink to the web site --> <a href="http://telerik.com/">Telerik</a> <!- Show the news table --> <table class="newstable"> ...
32
<html> <head><title>Test page</title></head> <body> <!-- This is the Web page body --> </body> </html>
33
Text Formatting
Text formatting tags modify the text between the opening tag and the closing tag
Ex. <b>Hello</b> makes Hello bold
<b></b> <i></i> <u></u> <sup></sup> <sub></sub> <strong></strong> <em></em> <pre></pre> <blockquote></blockquote> <del></del> bold italicized underlined Samplesuperscript Samplesubscript strong emphasized Preformatted text Quoted text block Deleted text strike through
34
apply-now.html
index.html
Hyperlinks Example
hyperlinks.html
<a href="form.html">Fill Our Form</a> <br /> <a href="../parent.html">Parent</a> <br /> <a href="stuff/cat.html">Catalog</a> <br /> <a href="http://www.devbg.org" target="_blank">BASD</a> <br /> <a href="mailto:bugs@example.com?subject=Bug Report">Please report bugs here (by e-mail only)</a> <br /> <a href="apply-now.html"><img src="apply-now-button.jpg /></a> <br /> <a href="../english/index.html">Switch to English version</a> <br />
41
42
Image attributes:
src alt height width border Location of image file (relative or absolute) Substitute text for display (e.g. in text mode) Number of pixels of the height Number of pixels of the width Size of border, 0 for no border
Example:
<img src="./php.png" alt="PHP Logo" />
45
Miscellaneous Tags
<hr />: Draws a horizontal
<hr size="5" width="70%" />
rule (line):
<center></center>: Deprecated!
<center>Hello World!</center>
<font></font>: Deprecated!
<font size="3" color="blue">Font3</font> <font size="+4" color="blue">Font+4</font>
46
<ul></ul>:
Attribute values
o Apple
o Orange o Pear
Apple
Orange Pear
49
<dl>
Pairs of text and associated definition; text is in <dt> tag, definition in <dd> tag
<dl> <dt>HTML</dt> <dd>A markup language </dd> <dt>CSS</dt> <dd>Language used to </dd> </dl>
Lists Example
<ol type="1"> <li>Apple</li> <li>Orange</li> <li>Grapefruit</li> </ol> <ul type="disc"> <li>Apple</li> <li>Orange</li> <li>Grapefruit</li> </ul> <dl> <dt>HTML</dt> <dd>A markup lang</dd> </dl>
lists.html
51
HTML Entity
© ® ™ < > &
Symbol
< > & "
52
Em Dash
Quotation Mark Euro British Pound Japanese Yen
—
" € £ ¥
53
54
after them
<div> is a block element Other block elements are <table>, <hr>, headings, lists, <p> and etc.
Inline elements dont break the text before
div-and-span.html
<div style="font-size:24px; color:red">DIV example</div>
<p>This one is <span style="color:red; fontweight:bold">only a test</span>.</p>
57
of text
span.html
<p>This one is <span style="color:red; fontweight:bold">only a test</span>.</p> <p>This one is another <span style="font-size:32px; font-weight:bold">TEST</span>.</p>
58
HTML Tables
HTML Tables
Tables represent tabular
data
A table consists of one or several rows Each row has one or more columns
Tables comprised of several core tags:
<table></table>: begin / end the table <tr></tr>: create a table row <td></td>: create tabular data (cell)
Tables should not be used for layout. Use CSS
61
<tbody> denotes collection of table rows that contain the very data
<tfoot> denotes table footer but comes BEFORE the <tbody> tag <colgroup> and <col> define columns (most often used to set column widths)
64
Nested Tables
Table data cells (<td>) can contain nested tables (tables within tables):
<table> <tr> <td>Contact:</td> <td> <table> <tr> <td>First Name</td> <td>Last Name</td> </tr> </table> </td> </tr> </table>
nested-tables.html
67
cellspacing
cellpadding
cell cell
cell cell
cell cell
cell cell
table-cells.html
<html> <head><title>Table Cells</title></head> <body> <table cellspacing="15" cellpadding="0"> <tr><td>First</td> <td>Second</td></tr> </table> <br/> <table cellspacing="0" cellpadding="10"> <tr><td>First</td><td>Second</td></tr> </table> </body> </html>
69
table-cells.html
<html> <head><title>Table Cells</title></head> <body> <table cellspacing="15" cellpadding="0"> <tr><td>First</td> <td>Second</td></tr> </table> <br/> <table cellspacing="0" cellpadding="10"> <tr><td>First</td><td>Second</td></tr> </table> </body> </html>
70
colspan
colspan="1"
cell[1,2]
rowspan
rowspan="1"
cell[1,2]
colspan="1" cell[1,1]
rowspan="2"
cell[2,1] rowspan="1"
72
Cell[1,2] Cell[2,2]
Cell[1,3]
Cell[3,2]
Cell[2,3]
73
HTML Forms
Entering User Data from a Web Page
HTML Forms
Forms are the primary
Example:
The method" attribute tells how the form data should be sent via GET or POST request
The "action" attribute tells where the form data should be sent
75
Form Fields
Fieldsets
Checkboxes:
<input type="checkbox" name="fruit" value="apple" />
Radio buttons:
<input type="radio" name="title" value="Mr." />
Radio buttons can be grouped, allowing only one to be selected from a group:
<input type="radio" name="city" value="Lom" /> <input type="radio" name="city" value="Ruse" />
78
<select name="gender"> <option value="Value 1" selected="selected">Male</option> <option value="Value 2">Female</option> <option value="Value 3">Other</option> </select>
Submit button:
79
Image button acts like submit but image is displayed and click coordinates are sent
<input type="image" src="submit.gif" name="submitBtn" alt="Submit" />
Password input a text field which masks the entered text with * signs
<input type="password" name="pass" />
Multiple select field displays the list of items in multiple lines, instead of one
<select name="products" multiple="multiple"> <option value="Value 1" selected="selected">keyboard</option> <option value="Value 2">mouse</option> <option value="Value 3">speakers</option> </select>
81
82
Labels
Form labels are used to associate an explanatory text to a form field using the field's ID.
<label for="fn">First Name</label> <input type="text" id="fn" />
Clicking on a label focuses its associated field (checkboxes are toggled, radio buttons are checked) Labels are both a usability and accessibility feature and are required in order to pass accessibility validation.
83
84
86
TabIndex
The tabindex HTML attribute
controls the order in which form fields and hyperlinks are focused when repeatedly pressing the TAB key
tabindex="0" (zero) - "natural" order
If X > Y, then elements with tabindex="X" are iterated before elements with tabindex="Y"
Elements with negative tabindex are skipped, however, this is not defined in the standard
<input type="text" tabindex="10" />
87
HTML Frames
<frameset>, <frame> and <iframe>
HTML Frames
Frames provide a way to show multiple HTML
ages of HTML development, but now their usage is rejected (browsers, search engines, etc.)
A <noframes> element is used to provide content for non-compatible agents.
89
<frameset cols="180px,*,150px"> <frame src="left.html" /> <frame src="middle.html" /> <frame src="right.html" /> </frameset>
</html>
Note the target attribute applied to the <a> elements in the left frame.
90
91
Table of Contents
What is CSS? Styling
with Cascading Stylesheets (CSS) Selectors and style definitions Linking HTML and CSS Fonts, Backgrounds, Borders The Box Model Alignment, Z-Index, Margin, Padding Positioning and Floating Elements Visibility, Display, Overflow CSS Development Tools
93
Indent
94
CSS Intro
Styling with Cascading Stylesheets
CSS Introduction
Improve flexibility
Due to CSS, all HTML presentation tags and attributes are deprecated, e.g. font, center, etc.
97
for different
media
On-screen
In print
Why Cascading?
Priority
Child elements in the HTML DOM tree inherit styles from their parent
Can override them
99
100
Text-related and list-related properties are inherited - color, font-size, font-family, line-height, text-align, list-style, etc
Box-related and positioning styles are not inherited - width, height, border, margin, padding, position, float, etc
<a> elements do not inherit color and text-
decoration
101
http://css.maxdesign.com.au/
Selectors
Selectors determine which element the rule
applies to:
All elements of specific type (tag) Those that mach a specific attribute (id, class)
Elements may be matched depending on how they are nested in the document tree (HTML)
Examples:
.header a { color: green } #menu>li { padding-top: 8px }
103
Selectors (2)
By element id:
#element_id { color: #ff0000; }
This will match <h1> tags, elements with class link, and element with id top-link
104
Selectors (3)
Pseudo-classes
105
Selectors (4)
This will match all <a> tags that are inside of <p>
This will match all siblings with class name link that appear immediately after <img> tag
106
Selectors (5)
This will match all elements with class error, direct children of <p> tag
This will match all <img> tags with alt attribute containing the word logo
.class1.class2 (no space) - matches elements with both (all) classes applied at the same time
107
108
Used when there is no CSS information or any other style information in the document
Caution: default styles
differ in browsers
E.g. margins, paddings and font sizes differ most often and usually developers reset them
* { margin: 0; padding: 0; } body, h1, p, ul, li { margin: 0; padding: 0; }
109
Simplifies the HTML document Improves page load speed as the CSS file is cached
111
http://www.slideshare.net/maxdesign/css-cascade-1658158
114
CSS Specificity
CSS specificity is used to determine the
precedence of CSS style declarations with the same origin. Selectors are what matters
Simple calculation: #id = 100, .class = 10, :pseudo = 10, [attr] = 10, tag = 1, * = 0 Same number of points? Order matters. See also:
http://www.smashingmagazine.com/2007/07/27/css-specificity-thingsyou-should-know/ http://css.maxdesign.com.au/selectutorial/advanced_conflict.htm
115
Embedded Styles
Embedded in the HTML in the <style> tag:
<style type="text/css">
118
119
External linking
Separate pages can all use a shared style sheet Only modify a single file to change the styles across your entire Web site (see http://www.csszengarden.com/)
Ancient browsers do not recognize @import Use @import in an external CSS file to workaround the IE 32 CSS file limit
121
color specifies the color of the text font-size size of font: xx-small, x-small, small, medium, large, x-large, xx-large, smaller, larger or numeric value font-family comma separated font names
Example: verdana, sans-serif, etc. The browser loads the first one that is available
font-weight can be normal, bold, bolder, lighter or a number in range [100 900]
126
the font
other content
Values: left, right, center, justify
127
Shorthand rule for setting multiple font properties at the same time
font:italic normal bold 12px/16px verdana
Backgrounds
background-image
fixed / scroll
129
Backgrounds (2)
background-position: specifies vertical
background: shorthand rule for setting background properties at the same time:
background: #FFF0C0 url("back.gif") no-repeat fixed top;
is equal to writing:
background-color: #FFF0C0; background-image: url("back.gif"); background-repeat: no-repeat; background-attachment: fixed; background-position: top;
Some browsers will not apply BOTH color and image for background if using shorthand rule
131
Background-image or <img>?
Background images allow
All
images that are not part of the page content (and are used only for "beautification") should be moved to the CSS
132
Borders
border-width: thin, medium, thick or
or RGB value
for
properties at once:
border: 1px solid red
is equal to writing:
border-width:1px; border-color:red; border-style:solid;
Specify different borders for the sides
When to avoid
135
138
IE Quirks Mode
When using quirks mode (pages with no DOCTYPE or with a HTML 4 Transitional DOCTYPE), Internet Explorer violates the box model standard
139
Positioning
position: defines the positioning of the
relative relative position according to where the element would appear with static position
absolute position according to the innermost positioned parent element fixed same as absolute, but ignores page scrolling
140
Positioning (2)
Margin VS relative positioning Fixed and absolutely
positioned elements do not influence the page normal flow and usually stay on top of other elements
Their position and size is ignored when calculating the size of parent element or position of surrounding elements Overlaid according to their z-index
Inline fixed or absolutely positioned elements can apply height like block-level elements
141
Positioning (3)
top, left, bottom, right: specifies offset of
positioned elements
Understanding stacking context
Each positioned element creates a stacking context. Elements in different stacking contexts are overlapped according to the stacking order of their containers. For example, there is no way for #A1 and #A2 (children of #A) to be placed over #B without increasing the z-index of #A.
142
Also used for content of table cells (which apply middle alignment by default)
143
Float
float: the element floats to one side
left: places the element on the left and following content on the right right: places the element on the right and following content on the left floated elements should come before the content that will wrap around them in the code
Float (2)
How floated elements are positioned
145
Clear
clear
Sets the sides of the element where other floating elements are NOT allowed Used to "drop" elements below floated ones or expand a container, which contains only floated children Possible values: left, right, both
Clearing floats
additional element (<div>) with a clear style
146
Clear (2)
Clearing floats (continued) :after { content: ""; display: block; clear: both; height: 0; } Triggering hasLayout in IE expands a container of floated elements
display: inline-block; zoom: 1;
147
Opacity
opacity: specifies the opacity of the element
Floating point number from 0 to 1 For old Mozilla browsers use moz-opacity For IE use filter:alpha(opacity=value) where value is from 0 to 100; also, "binary and script behaviors" must be enabled and hasLayout must be triggered, e.g. with zoom:1
148
Visibility
visibility
Determines whether the element is visible hidden: element is not rendered, but still occupies place on the page (similar to opacity:0) visible: element is rendered normally
149
Display
display: controls the display
of the element and the way it is rendered and if breaks should be placed before and after the element
inline: no breaks are placed before and after (<span> is an inline element) block: breaks are placed before AND after the element (<div> is a block element)
150
Display (2)
display: controls the display
of the element and the way it is rendered and if breaks should be placed before and after the element
none: element is hidden and its dimensions are not used to calculate the surrounding elements rendering (differs from visibility: hidden!) There are some more possible values, but not all browsers support them
Specific displays like table-cell and table-row
151
Overflow
overflow: defines the behavior of element when content needs more space than you have specified by the size properties or for other reasons. Values: visible (default) content spills out of the element
152
specifies the look of the mouse cursor when placed over the element
Values: crosshair, help, pointer, progress, move, hair, col-resize, rowresize, text, wait, copy, drop, and others
presentation tags
Your pages load faster, because browsers
154
Maintenance Example
Title Title Title Title Some random Some random text here. You text here. You Some random cant read it cant read it Title text here. You Title Some random anyway! Har har anyway! Har har cant read it Title text here. You Title Some random har! Use Css. Some random har! Use Css. anyway! Har har cant read it text here. You text here. You Some random Some random har! Use Css. anyway! Har har cant read it cant read it text here. You text here. You Title har! Use Css. anyway! Har har anyway! Har har cant read it cant read it Title Title Some random har! Use Css. har! Use Css. anyway! Har har anyway! Har har text here. You Some random Some random har! Use Css. har! Use Css. cant read it text here. You text here. You Title anyway! Har har cant read it cant read it Title Some random har! Use Css. anyway! Har har anyway! Har har Title Title Title text here. You Some random har! Use Css. har! Use Css. Title cant read it text here. You Some random Some random Some random anyway! Har har cant read it Some random text here. You text here. You Title text here. You har! Use Css. anyway! Har har text here. You cant read it cant read it cant read it Title Some random har! Use Css. cant read it anyway! Har har anyway! Har har anyway! Har har text here. You Title Some random anyway! Har har har! Use Css. har! Use Css. har! Use Css. cant read it Title Title text here. You har! Use Css. Some random anyway! Har har cant read it Title text here. You Some random Some random har! Use Css. anyway! Har har cant read it text here. You text here. You Some random har! Use Css. anyway! Har har cant read it cant read it text here. You har! Use Css. anyway! Har har anyway! Har har cant read it Title Title har! Use Css. har! Use Css. anyway! Har har Some random Some random har! Use Css. Title text here. You text here. You Title cant read it cant read it Title Some random Some random anyway! Har har anyway! Har har text here. You Title Title Title text here. You Some random har! Use Css. har! Use Css. cant read it cant read it text here. You Some random Some random Some random anyway! Har har anyway! Har har cant read it Title text here. You text here. You text here. You har! Use Css. har! Use Css. anyway! Har har cant read it cant read it cant read it Some random har! Use Css. anyway! Har har anyway! Har har anyway! Har har text here. YouTitle Title har! Use Css. har! Use Css. har! Use Css. cant read it Some random Some random anyway! Har har Title text here. You Title Title text here. You har! Use Css. cant read it cant read it Some random Some random Some random anyway! Har har anyway! Har har Title text here. You text here. You text here. You har! Use Css. har! Use Css. cant read it cant read it cant read it Some random anyway! Har har anyway! Har har anyway! Har har Title text here. You Title Title har! Use Css. har! Use Css. har! Use Css. cant read it Title Title Title Some random Some random Some random anyway! Har har text here. You text here. You text here. You Some random Some random Some random har! Use Css. cant read it cant read it cant read it text here. You text here. You Title text here. You anyway! Har har anyway! Har har anyway! Har har cant read it cant read it cant read it Some random har! Use Css. har! Use Css. har! Use Css. anyway! Har har anyway! Har har anyway! Har har text here. You har! Use Css. har! Use Css. har! Use Css. cant read it Title Title anyway! Har har Title Title Some random Some random har! Use Css. text here. You text here. You Some random Some random cant read it cant read it Title text here. You text here. You anyway! Har har anyway! Har har cant read it cant read it Title Some random har! Use Css. har! Use Css. anyway! Har har anyway! Har har text here. You Title Some random har! Use Css. har! Use Css. cant read it Title text here. You Title Some random anyway! Har har cant read it text here. You Title Some random Some random har! Use Css. anyway! Har har cant read it text here. You text here. You Some random har! Use Css. anyway! Har har cant read it cant read it Title Title text here. You har! Use Css. anyway! Har har anyway! Har har cant read it Some random Some random har! Use Css. har! Use Css. anyway! Har har text here. You text here. You Title Title har! Use Css. cant read it cant read it Some random Some random anyway! Har har anyway! Har har text here. You text here. You har! Use Css. har! Use Css. cant read it cant read it anyway! Har har anyway! Har har har! Use Css. har! Use Css.
CSS file
155
CSS Editor
156
157
158
Introduction to JavaScript
Table of Contents
160
Introduction to JavaScript
In <body> part
In external .js file
161
JavaScript Syntax
JavaScript operators JavaScript Data Types JavaScript Pop-up boxes
alert, confirm and prompt
DHTML
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 users actions
DHTML = HTML + CSS + JavaScript
JavaScript
Programming logic for interaction with the user, to handle events, etc.
165
JavaScript
Dynamic Behavior in a Web Page
JavaScript
JavaScript
Client-side technology
JavaScript Advantages
JavaScript
browser and OS
Can be used as object-oriented language Can handle exceptions Can perform asynchronous
170
171
<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
172
code 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
sample.js
175
JavaScript Syntax
The JavaScript
Operators (+, *, =, !=, &&, ++, ) Variables (typeless) Conditional statements (if, else)
Data Types
JavaScript
data types:
var myName = "You can use both single or double quotes for strings";
Arrays
var my_array = [1, 5.3, "aaa"];
Associative
Everything is Object
Every variable
String Operations
The + operator joins strings
string1 = "fat "; string2 = "cats"; alert(string1 + string2);
// fat cats
What is "9" + 9?
alert("9" + 9); // 99
Converting string
to number:
// 18
alert(parseInt("9") + 9);
180
Confirmation box
Prompt box
184
185
Symbol
> < >= <= ==
Meaning
Greater than Less than Greater than or equal to Less than or equal to Equal
!=
Not equal
186
conditional-statements.html
var a = 0; var b = true; if (typeof(a)=="undefined" || typeof(b)=="undefined") { document.write("Variable a or b is undefined."); } else if (!a && b) { document.write("a==0; b==true;"); } else { document.write("a==" + a + "; b==" + b + ";"); }
187
Switch Statement
The switch statement works like in C#:
switch (variable) { switch-statements.html case 1: // do something break; case 'a': // do something else break; case 3.14: // another code break; default: // something completely different }
188
Loops
Like in C#
for loop while loop do while loop
var counter; for (counter=0; counter<4; counter++) { alert(counter); } while (counter < 5) { alert(++counter); loops.html }
189
Functions
Code structure
Every HTML element is accessible via the JavaScript DOM API Most DOM objects can be manipulated by the programmer
The event model lets a document to react when the user does something on the page
Advantages
Create interactive pages
Updates the objects of a page without reloading it
193
Accessing Elements
Access elements via their ID attribute
var elem = document.getElementById("some_id")
DOM Manipulation
Once we access an element, we can read and
197
element.firstChild
element.lastChild
198
Warning:
Events are fired by the Browser and are sent to the specified JavaScript event handler function Can be set with HTML attributes:
<img src="test.gif" onclick="imageClicked()" />
201
It brings information about the event Contains the type of the event (mouse click, key press, etc.)
Data about the location where the event has been fired (e.g. mouse coordinates)
Holds a reference to the event sender
E.g. the button that was clicked
202
Some browsers do not send this object, but place it in the document.event
Some of the names of the events object properties are browser-specific
203
onsubmit
Allows you to cancel a form submission
events
onload, onunload
Allowed only for the <body> element Fires when all content on the page was loaded / unloaded
205
onload.html
<html> <head> <script type="text/javascript"> function greet() { alert("Loaded."); } </script> </head> <body onload="greet()" > </body> </html>
206
window
The top node of the DOM tree
Represents the browser's window
document
holds information the current loaded document
screen
Holds the users display properties
browser
Holds information about the browser
208
window
navigator
screen
document
history
location
form
form form
button
209
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";
210
211
information about
the display
window.moveTo(0, 0); x = screen.availWidth; y = screen.availHeight; window.resizeTo(x, y);
212
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
functions
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/>"); }
math.html
215
functions
dates.html
var now = new Date(); var result = "It is now " + now; document.getElementById("timeField") .innerText = result; ... <p id="timeField"></p>
216
Timers: setTimeout()
Timers: setInterval()
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" />
219
Debugging JavaScript
Debugging JavaScript
Modern browsers have JavaScript
Several tools
to debug JavaScript
Firebug
Firebug
Firebug (2)
223
debug(message)
info(message) log(message) warn(message) error(message)
224
Questions?