DHTML and JavaScript
Gilorien
Cascading Style Sheets, JavaScript Style Sheets, & JavaScripted Layers plus advanced JavaScript 1.2 & 1.3
Prentice Hall PTR Upper Saddle River, NJ 07458 www.phptr.com
Contents
Preface xvi Regarding HTML Syntax xviii Regarding JavaScript Syntax xviii Regarding Section Header Capitalization xix About the CD-ROM xx
Part I Dynamic HTML
Chapter 1 Style Sheets 1 Introduction to Style Sheets 4 1.1) Style Sheets Overview 4 1.2) The Golden Rules for Styles 6 1.3) Cascading Style Sheets & JavaScript Style Sheets 8 Cascading Style Sheets 8 1.4) CSS Syntax Property Chart 11 1.5) CSS & JavaScript Syntax Property Comparison Chart 12 1.6) CSS Layer Properties and
Just one more thing: While it isn't required, most people put the CSS Style Definition parameters inside of a Comment Tag () so that they will be hidden from browsers that don't recognize Style Sheets or the STYLE Tags. However, be careful that you don't put the STYLE Tags themselves inside the Comment Tag or they will be ignored even by Style-Savvy browsers like Navigator. Here's how the last example would look when properly placed within a Comment Tag:
Special Notice: It is absolutely critical that you code your syntax correctly. Pay special attention to your colons and semicolons so that they are in the correct place. This is how the browser knows how to distinguish between just an ordinary piece of text and CSS NAME: VALUE; syntax. Do NOT put the VALUE choice inside of double quote marks like you do for regular HTML Attributes. The Style Sheet Property Names are case-sensitive. 10 Part I — Dynamic HTML
So far we have created a Style Sheet, but we haven't actually applied it to any Elements in the BODY of the document yet. This is the easy part because it works just like normal HTML that you're used to. Expanding on the above example one last time, here is what it looks like when you apply the Style defined for the H2 in the Style Sheet to an actual H2 heading in the BODY section of the document.
Example 1-1: Sample201.html
Style Sheet Use:
This is a level-two heading that is aqua and sized at 14 points.
Notice that this text which is not inside the H2 tags is rendered with the default colors of the BODY Tag.
I've included the following charts because when you're trying to organize all this theory inside your head, it's very useful to have the options that you use in the real world at your fingertips to improve how you remember how it all fits together. Just look it over to get a feel for what the possibilities are and then use it for reference when you need a quick memory jog. If you want a printout of all these charts, then print Charts.pdf, in the PDF-Files folder on the CD-ROM. Other charts are in the Charts folder in HTML format. CSS Syntax Property Chart
Here's a list of all the Property Names and Values that are used to format the content in your Style Sheets using Cascading Style Sheet Syntax. Check the explanatory notes at the top of page 13 if deciphering the chart is initially unwieldy. CHAPTER 1 — Style Sheets 11
∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ Property All All Name Possible Possible CSS Syntax Categories Values font-size absolute-size xx-small, x-small, small, medium, large, x-large, xx-large relative-size larger, smaller length | percentage 10pt, 12pt, 14pt, 20pt, 24pt,... 20%, 25%, 50%, 80%, 120%,150%, 200%,... font-family any system font Helvetica, Times, Geneva, Courier,... font-weight keyword |number normal, bold, bolder, lighter | 100-900 font-style keyword normal, italic line-height number multiplied by a number or decimal length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% keyword normal text-decoration keyword none, underline, line-through, blink text-transform keyword capitalize, uppercase, lowercase, none text-align keyword left, right, center, justify text-indent length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% margin length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% keyword auto (is available for all 5 margin Properties) margin (example) { margin: 24pt 30pt 30pt 17pt; } sets each margin to diff. value The order is: top right bottom left margin-top length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% margin-right length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% margin-bottom length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% margin-left length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% padding length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% padding (example) { padding: 25px 20px 45px 35px; } sets each padding to diff. value padding-top length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% padding-right length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% padding-bottom length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% padding-left length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% color colorvalue none, name, #$$$$$$, rgb(0-255,0-255,0-255), rgb(?%,?%,?%) background-color colorvalue none, name, #$$$$$$, rgb(0-255,0-255,0-255), rgb(?%,?%,?%) the 16 color names are aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, yellow background-image imageurl url( ) { background-image: url(JPEG-Images/ExampleImage.jpeg); } border-style keyword none, solid, double, inset, outset, groove, ridge border-color colorvalue none, name, #$$$$$$, rgb(0-255,0-255,0-255), rgb(?%,?%,?%) border-width length |percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% border-width same { border-width: 20px 30px 40px 50px; } sets each width to diff. value border-top-width length |percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% border-right-width length |percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% border-bottom-width length |percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% border-left-width length |percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% width length |percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n% keyword auto float keyword left, right, center, none clear keyword none, left, right, both display keyword block, inline, list-item list-style-type keyword disc, circle, square, decimal, lower-roman, upper-roman, lower-alpha, keyword upper-alpha, none white-space keyword normal, pre { white-space: pre; } { white-space: normal; } ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ 12 Part I — Dynamic HTML CSS & JavaScript Syntax Property Comparison Chart ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ Property Property Simple Name Name CSS Syntax CSS Syntax JavaScript Syntax Examples font-size fontSize { font-size: 14pt; } font-family fontFamily { font-family: Helvetica, Times, Geneva, Courier; } font-weight fontWeight { font-weight: bold; } font-style fontStyle { font-style: italic; } line-height lineHeight { line-height: 22pt; } text-decoration textDecoration { text-decoration: underline; } text-transform textTransform { text-transform: uppercase; } text-align textAlign { text-align: right; } text-indent textIndent { text-indent: 40px; } margin margins( ) { margin: 75px; } sets all 4 margins to same value margin margins( ) { margin: 24pt 30pt 30pt 17pt; } sets each margin to diff. value The order is: top right bottom left margin-top marginTop { margin-top: 40mm; } margin-right marginRight { margin-right: 4cm; } margin-bottom marginBottom { margin-bottom: 12pc; } margin-left marginLeft { margin-left: 1in; } padding paddings( ) { padding: 25px; } padding paddings( ) { padding: 25pt 20px 45pt 35px; } padding-top paddingTop { padding-top:.5in; } padding-right paddingRight { padding-right: 25pt; } padding-bottom paddingBottom { padding-bottom: 15pt; } padding-left paddingLeft { padding-left: 5pt; } color color { color: blue; } { color:#0000ff; } color color { color: rgb(0%, 0%, 100%); } { color: rgb(0,20, 255); } background-color backgroundColor { background-color: maroon; } same options as color background-image backgroundImage { background-image: url(JPEG-Images/ExampleImage.jpeg); } border-style borderStyle { border-style: groove; } border-color borderColor { border-color:#335a77; } border-color borderColor { border-color: rgb(20%, 50%, 70%); } border-color borderColor { border-color: rgb(255, 20, 150); } border-width borderWidths( ) { border-width: 20px; } border-width borderWidths( ) { border-width: 20px 30px 40px 50px; } border-top-width borderTopWidth { border-top-width: 20px; } border-right-width borderRightWidth { border-right-width: 30px; } border-bottom-width borderBottomWidth{ border-bottom-width: 40px; } border-left-width borderLeftWidth { border-left-width: 50px; } width width { width: 50%; } { width: 500px; } { width: 7in; } float align { float: left; } { float: right; } { float: center; } clear clear { clear: left; } { clear: right; } { clear: both; } display display { display: block; } { display: list-item; } list-style-type listStyleType { list-style-type: square; } { list-style-type: upper-roman; } white-space whiteSpace { white-space: pre; } { white-space: normal; } ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
absolute units relative units pt -- points pc -- picas em -- the height of the element's font px -- pixels in -- inches ex -- half the height of the element's font mm -- millimeters cm -- centimeters px -- pixels, relative to rendering surface CHAPTER 1 — Style Sheets 13
Note that in the chart on pages 11 and 195, for any of the Properties that have the scenario:
Possible Possible Categories Values
length | percentage em, ex, px, pt, pc, in, mm, cm | 1%-1000%, ...n%
It means that if you use a length type Value, then it should be a number suffixed by one of these units of measurement: em, ex, px, pt, pc, in, mm, cm, like this: "200px".
If you use a percentage type Value then it should be a number that is followed by a percent (%) sign, like this: "75%". CSS Layer Properties and
Notice that the preceding two charts do not include the Properties that are used to POSITION your content. Those are listed in the chart below within the Layers model. Also note that there are no corresponding Properties in CSS Syntax for the LAYER Attributes of PAGEX, PAGEY, ABOVE and BELOW or the five Event Handlers. ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
Property Attribute Simple Simple Name Name CSS
Layer Object Simple Read-Only Property JavaScript Syntax or Name Examples Read/Write JavaScript Syntax (assume there is a Layer named myLayer1) document document.write(document.myLayer1.document); Read-Only name myVar=document.name; Read-Only left document.myLayer1.left=20; Read-Write top document.myLayer1.top=30; Read-Write pageX document.myLayer1.pageX=10; Read-Write pageY document.myLayer1.pageY=15; Read-Write zIndex document.myLayer1.zIndex=7; Read-Write visibility document.myLayer1.visibility="hide"; Read-Write clip.top document.myLayer1.clip.top=50; Read-Write clip.left document.myLayer1.clip.left=-200; Read-Write clip.right document.myLayer1.clip.right=700; Read-Write clip.bottom document.myLayer1.clip.bottom=500; Read-Write clip.width document.myLayer1.clip.width=640; Read-Write clip.height document.myLayer1.clip.height=480; Read-Write background.src document.myLayer1.background.src="myImage.jpeg"; Read-Write bgColor document.myLayer1.bgColor = "#ff0000"; Read-Write document.myLayer1.bgColor = "green"; Read-Write document.myLayer1.bgColor = null; Read-Write siblingAbove document.myLayer1.siblingAbove; Read-Only siblingBelow document.myLayer1.siblingBelow; Read-Only above document.myLayer1.above; Read-Only below document.myLayer1.below; Read-Only parentLayer document.myLayer1.parentLayer; Read-Only src document.myLayer1.src="myFile.html"; Read-Write ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ Generic { font-family: } Names Chart
These are the generic Name Values for the font-family Property, followed by an example of each that the browser might select from the user's System Fonts:
Generic font-family Names Example
serif Times sans-serif Helvetica cursive Zapf-Chancery fantasy Western monospace Courier CHAPTER 1 — Style Sheets 15 Styles Chart for HTML Elements
This is a list of all the Elements, including those in HTML 4.0, that you can use the STYLE Attribute or the CLASS Attribute or the ID Attribute with: ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
A ACRONYM ADDRESS B BIG BLOCKQUOTE BODY BR BUTTON CAPTION CENTER CITE CODE COL COLGROUP DD DEL DFN DIR DIV DL DT EM FIELDSET FORM H1 H2 H3 H4 H5 H6 HR I IMG INPUT INS ISINDEX KBD LABEL LEGEND LI LINK MAP MENU OBJECT OL OPTION P PRE Q S SAMP SELECT SMALL SPAN STRIKE STRONG SUB SUP TABLE TBODY TD TEXTAREA TFOOT TH THEAD TR TT U UL VAR XMP ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
You can also use them with the following Elements, but at the time of this writing, they will only work in Navigator 4.0+, but more than likely by the time you read this all the other major browsers will recognize them also. If you use cutting-edge code then you should create parallel code to include in Elements like
ILAYER LAYER ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
Special Notice: By default, a LAYER is transparent in the sense that other LAYERs that are beneath it will show through except in the places where the actual content, such as text or images, reside. 16 Part I — Dynamic HTML
The
Attributes Defined:
TYPE="text/CSS" specifies a Style Sheet in the Cascading Style Sheet Syntax. TYPE="text/JavaScript" specifies a Style Sheet in the JavaScript Syntax. SRC="URL" specifies the URL of an external Style Sheet to load. See pages 116-123.
Parameters Defined:
Element specifies an HTML Element to apply a Style to.
PropertyName specifies a CSS Property Name followed by a colon and then a Value, which is the specific value parameter for that Property and is followed by a semicolon, and together they form a Name:Value; pair that is enclosed within curly braces that defines a Style. If you use multiple Name:Value; pairs to define a Style they would all be enclosed within the curly braces as a complete Style Definition for that Element. className specifies a CLASS of STYLE that can be accessed with the CLASS Attribute of most HTML Elements. CHAPTER 1 — Style Sheets 17
IDname specifies a named Style Exception which is identified as such by the preceding hashmark(#) and which can be called to apply to an Element by the ID Attribute.
Element Element ... this is a special way to apply Styles that is called CONTEXTUAL APPLICATION or you could say that it's implemented by specifying CONTEXTUAL SELECTION CRITERIA. When you have sequential Elements in your definition, or put another way, one Element followed by another Element, like this:
DIV BLOCKQUOTE then the Style that you define will only apply to BLOCKQUOTE Elements that are also contained within DIV Elements. The following mini-example demonstrates this by defining a Style that creates a border that is 20 pixels thick and makes the text blue for only BLOCKQUOTE Elements inside DIV Elements. (See Sample202.html on CD-ROM.)
DIV BLOCKQUOTE { color: blue; border-width: 20px; }
The next example defines three Styles. It first creates a Style for all PARAGRAPH Elements, which will have green text at a point size of 20. Next, a Style creates bold red text for all BLOCKQUOTE Elements and another Style creates a purple border that is 10 pixels thick and makes the text blue for all BLOCKQUOTE Elements, which are also inside DIV Elements.
P { color: green; font-size: 20pt; } BLOCKQUOTE { color: red; font-weight: bold; } DIV BLOCKQUOTE { color: blue; border-width: 10px; border-color: purple; }
Here's the complete code:
Example 1-3: Sample203.html
18 Part I — Dynamic HTML
Style Sheet Use for Contextual Selection Criteria:
Notice how the P Element has green text sized at 20 point.
There are two Blockquotes in this example. This is the first one and as this Style demonstrates, it has bold and red text for all BLOCKQUOTE Elements.
This Style creates a purple border that is 10 pixels thick and overrides the red text setting above but does not change the Bold parameter which applies to all BLOCKQUOTE Elements, even CONTEXTUAL ones. The result is that the text is Bold and Blue for all BLOCKQUOTE Elements inside of DIV Elements like this one.
The concept of CONTEXTUAL SELECTION CRITERIA is covered in much more detail later in this chapter starting at page 93.
Special Notice:
Putting Comments inside Style Sheets is different from putting them inside HTML code. First, they are not Elements and cannot go inside their own Tag. The syntax is also different: For CSS Syntax you include Comments like this:
/* This is a comment. It can be on as many lines as you need. */
For JavaScript Syntax you have two options and can include Comments like this:
/* This is a comment. It can be on as many lines as you need. */ // This is a comment that can only be on one line
Just in case you forgot, here is an HTML Comment Element:
CHAPTER 1 — Style Sheets 19
Example 1-3 Sample203.html 20 Part I — Dynamic HTML
∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
There are multiple ways to apply CSS Styles to your documents:
•
This is Sample 204
This last paragraph is outside of both the H1 and DIV Elements so it is covered by the BODY Element. Notice the margins set in the BODY Element. If you resize the window you can see the bottom margin.