net - UK (2020-04)

(Antfer) #1
selector that the web browser doesn’t
understand, it will ignore the enclosed
declarations and skip ahead to the next
selector. The web browser won’t throw an
error. It won’t stop parsing the CSS file at
that point and refuse to go any further.
Likewise, if you use a property or value
that the browser doesn’t understand,
it will skip over it and move on to the
next declaration. This has enabled the
vocabulary of CSS to grow over time,
without waiting for universal browser
support. You can use new selectors,
properties and values even if they’re not
available in every browser. The non-
supporting browsers ignore what they
don’t yet understand. They are liberal in
what they accept.

THE BEHAVIOUR
LAYER
Describing the structure and the
presentation of a document is
empowering but it has its limits. Using
HTML and CSS, you can craft beautiful,
accessible, semantic web pages but

they’re static. Interaction is bounded by
HTML’s vocabulary. Links and form fields
are interactive by default but if you want
to extend the interactivity of HTML you
need a scripting language.
In the early days of the web, a young
engineer named Brendan Eich created a
scripting language for web browsers. The
marketing department at his company,
Netscape, named it JavaScript. It’s a
terrible name. It sounds like JavaScript
has something to do with the Java
programming language. It doesn’t. But,
back then, Java was poised to take over
the world. Naming this new language
JavaScript made it sound hip.
HTML and CSS are declarative
languages. Rather than giving step-

by-step instructions, you describe the
outcome you want using the vocabulary
of the language (elements and attributes
in HTML, selectors, properties and
values in CSS). On the plus side, these
declarative languages have the loose
error handling that makes them
relatively easy to get started with. But,
on the downside, you’re always going to
be limited by the language’s vocabulary.
A scripting language is more powerful.
It enables you to specify precisely what
you want to happen. But that power and
precision comes at a price. A scripting
language can’t afford to have the same
lax error handling as a declarative
language. If you write some JavaScript
that a browser doesn’t understand, the
browser will throw an error. It will stop
parsing the script at that point and
refuse to parse any further. Postel’s Law
doesn’t hold true here. Browsers are not
at all liberal in what they accept when it
comes to JavaScript.
JavaScript’s stricter error handling
means that developers need to exercise

more care when writing it. If there’s a
particular browser API that’s available in
JavaScript, you need to think about what
will happen in a browser that hasn’t yet
shipped that API. That browser won’t
simply ignore any parts of your code that
use the new feature. Instead, you’ll need
to test for the existence of the API. This
kind of ‘feature detection’ isn’t needed
in the declarative languages of HTML
and CSS but it’s a necessary part of
writing responsible JavaScript.

Using HTML and CSS, you can


craft beautiful, accessible,


semantic web pages but they’re


static. Interaction is bounded by


HTML’s vocabulary


FEATURES
Connect the web’s building blocks

Free download pdf