Meet the DOM – A Checklist Aside

).

Explorer and Mozilla give intently matching outcomes, aside from background colour. Taking a look at


one, we see that Explorer shows a colour initially expressed as hexadecimal in hex; Mozilla shows it in

rgb

format utilizing decimal equivalents. Colours entered as names keep as names.

Concentrating on Mozilla, let’s look extra intently on the properties detected and the HTML that produced the kinds. Once we retrieve, say, division one’s fashion object with this JavaScript:

{Line wraps are marked thusly: ». – Ed.}

styleObject = »
  doc.getElementById( "d1" ).fashion;

we get the fashion object that’s connected to the tag. The fashion info outlined within the stylesheet isn’t “merged” in with the data, as was the case in Navigator 4.

Desk 1 exhibits the related properties and related HTML.

If we would have liked to make use of DHTML and JavaScript on just one web page, this wouldn’t be an issue. We’d merely put all of the fashion info into the tags and have it at our disposal.

What will we do if we have now a number of pages that use the identical kinds and JavaScript?

Utilizing Properties on A number of Pages#section5

When we have now a number of pages utilizing the identical kinds and JavaScript, we are able to:

1. Duplicate the kinds into the tags on all of the pages
This technique will definitely work, nevertheless it defeats the aim of favor sheets – the flexibility to separate fashion from content material.
2. Use JavaScript to set preliminary situations
We might set all of the

s to be invisible within the fashion sheet, after which explicitly set all of the preliminary fashion attributes from a shared JavaScript .js file. It is a cheap resolution when each web page begins out in the identical preliminary state.

3. Entry Fashion Objects in a Stylesheet
If we are able to discover a strategy to entry the kinds which can be within the stylesheet, we have now the best flexibility; we are able to use a mode throughout a number of pages, and, since we are able to entry them from JavaScript, we are able to learn the preliminary situations quite than use mounted code to set them.

Accessing Fashion Sheets#section6

From Mozilla, it’s potential to entry all of the fashion sheets. Ranging from the doc, right here’s the hierarchy for accessing a selected fashion inside a stylesheet:

  • styleSheets (record)
    • href (property)
    • cssRules (record)
        • kind (property)
        • selectorText (property)
        • fashion (property)

Filling in some particulars on the properties:

href
The URL of the stylesheet
kind
An integer telling which kind of rule that is. The one ones we’re concerned with are those of kind CSSRule.STYLE_RULE.
selectorText
fashion
The selector and the fashion.  Within the following stylesheet rule:

p {colour:blue; font-size:24pt;}

the selectorText could be the string p and the fashion could be an object akin to the properties within the braces.

JavaScript Entry to Fashion Sheets#section7

Right here is the JavaScript code that may retrieve a mode by its selector title. Observe that it really works solely on Mozilla; it doesn’t seem to work correctly in IE4.

   perform getStyleBySelector( selector )
   {
       var sheetList = doc.styleSheets;
       var ruleList;
       var i, j;
   
       /* look via stylesheets in 
        reverse order that they 
        seem within the doc */
       for (i=sheetList.length-1; i >= 0; i--)
       {
           ruleList = sheetList.cssRules;
           for (j=0; j<rulelist.size; j++)="" {="" if="" (rulelist[j].kind="=" <span="" class="linewrap">»
                CSSRule.STYLE_RULE »
                && »
                ruleList[j].selectorText == »
                  selector)
               {
                   return ruleList[j].fashion;
               }   
           }
       }
       return null;
   }
</rulelist.size;>

This can work with

any

selector, so you could find the fashion related to, say, the

 
tag by calling

getStyleBySelector(“h3”)
, the fashion related to a

 
Observe:

Mozilla M14 won’t appropriately discover a fashion whether it is in an imported stylesheet (utilizing

@

notation). It causes a JavaScript error.

  Instance of Fashion Sheet Entry in JavaScript#section8

Right here’s an instance that makes use of

getStyleBySelector()

to dynamically change the fashion of a category. The textual content under incorporates warnings and options outlined as clsses by the fashion sheet under; by clicking the buttons you possibly can see these elements of the highlighted.

<fashion kind="textual content/css">
.warning { colour: black; }
.function { colour: black; }
</fashion>

  Mix-O-Matic#section9

Thanks for selecting Mix-O-Matic as your new meals processor. The Mix-O-Matic has a big number of attachments that assist you to stir, whip, mix, and purée your meals.

We’ve even added a mild rinse cycle to your delicate greens!

Your Mix-O-Matic requires a 110 volt AC energy provide in the USA, and a 220 volt AC energy provide in Europe.

A 12-volt DC adapater is included so that you could use the Mix-O-Matic in your automobile
, though

you shouldn’t function this machine whereas driving or intoxicated
.

All of the metallic elements are coated with Cruft-B-Gone®, making clean-up a breeze.

Don’t immerse the blender in water whereas it’s plugged in.

We hope you get pleasure from utilizing your Mix-O-Matic as a lot because the grossly underpaid staff in some unnamed third world nation loved making it.

<!–
if (!isNav6)
{
doc.writeln(”

These buttons work solely with Netscape 6 or ” +
“Mozilla.

“);
}
//–>

    JavaScript DOM Utilities#section10

I’ve written a number of utilities to work with the DOM in Mozilla. Chances are you’ll

see a textual content model of that file right here.

Beneath is an inventory of the perform names, preceded with a quick commentary of what every one does. You may even see them in motion on the subsequent web page.

/*
 * Browser model snooper; determines 
 * your browser (Navigator 4, Navigator 6, 
 * or Web Explorer 4/5)
 */
perform setBrowser()/*
 *
 * Given a selector string, return a mode
 * object by looking via 
 * stylesheets. 
 * Return null if none discovered
 */
perform getStyleBySelector( selector )/*
 *
 * Given an id and a property (as strings),
 * return the given property of that id.  
 * Navigator 6 will first search for the property 
 * in a tag; if not discovered, it can look 
 * via the stylesheet.
 *
 * Observe: don't precede the id with a # -- it 
 * will likely be appended when looking the 
 * stylesheets
 *
 */
perform getIdProperty( id, property )/*
 *
 * Given an id and a property (as strings), 
 * set the given property of that id to the 
 * worth supplied.
 *
 */
perform setIdProperty( id, property, worth )/*
 *
 * Transfer a given id to place (xValue, yValue)
 * In Navigator 6 and IE 4/5, 
 * size models are preserved.
 *
 */
perform moveTo( id, x, y )/*
 *
 * Transfer a given id to 
 * (currentX + xValue, currentY + yValue)
 * In Navigator 6 and IE 4/5, 
 * size models are preserved.
 *
 */
perform moveBy( id, x, y)/*
 *
 * Retrieve background colour for a given id.
 * The worth returned will likely be in hex format 
 * (#rrggbb)
 *
 */ 
perform getBackgroundColor( id )/*
 *
 * Return a division’s doc
 * 
 */
perform getDocument( divName )

    CSS Property Checks#section11

CSS Properties Examples (1)

CSS Properties Examples (2)

As these introductory examples counsel, the DOM is a outstanding device that may make your internet pages way more dynamic. And it does so with out resorting to proprietary tags or “improvements” that work solely in a single browser. In the meanwhile, Netscape 6 is the one browser that totally helps the DOM – and Netscape 6 continues to be in growth. As soon as it’s launched, it’s onerous to think about that different browsers gained’t be pressured to catch up.

As you start to discover and work with the DOM, take outing to thank the oldsters at

Mozilla

for doing the heavy lifting and delivering this factor. Additionally, you will wish to learn up on the DOM on the W3C’s

Doc Object Mannequin

pages. And if you happen to’d prefer to see all browser makers providing us this device, contemplate lending your assist to

The Internet Requirements Undertaking, a gaggle that desires to see W3C requirements like this totally carried out in all internet browsers. Meantime, code on, and have enjoyable!

Leave a Comment