JavaScript and HTML DOM

WORKING WITH THE DOM

In many cases, working with the DOM is fairly straightforward, making it easy to re-create with
JavaScript what normally would be created using HTML code.


 
Dynamic Scripts
The <script> element is used to insert JavaScript code into the page, either using by the src attribute
to include an external fi le or by including text inside the element itself. Dynamic scripts are those
that don’t exist when the page is loaded but are included later by using the DOM. As with the
HTML element, there are two ways to do this: pulling in an external fi le or inserting text directly.
Dynamically loading an external JavaScript fi le works as you would expect. Consider the following
<script> element:

<script type=”text/javascript” src=”client.js”></script>
This <script> element includes the text for the Chapter 9 client-detection script. The DOM code to
create this node is as follows:
var script = document.createElement(“script”);
script.type = “text/javascript”;
script.src = “client.js”;
document.body.appendChild(script);

JavaScript values, variables

If you try to declare a variable that already exists, JavaScript will treat it as a simple assignment statement and assign any new value in the declaration statement to the variable. If the duplicate declaration has no assignment, then nothing happens. If you try to assign a value to a non-existent variable, JavaScript will create the variable for you.

<script type="text/javascript">
//Commented lines starting with the double


//First we will declare a few variables
//and assign values to them


myTxt = "Do IT";
mynum =13;
//Note that myTxt is a string and mynum is numeric.

//Next we will display these to the user.

document.write(myTxt);

//To concatenate strings in JavaScript, use the '+' operator 
document.write("My favourite number is "+ mynum);
</script>

Include Inline JavaScript code

To include inline JavaScript code, place JavaScript code inside the <script> element directly, as
follows:
<script type=”text/javascript”>
function sayHi(){
alert(“Hi!”);
}
</script>

When using inline JavaScript code, keep in mind that you cannot have the string “</script>” anywhere
in your code. For example, the following code causes an error when loaded into a browser:
<script type=”text/javascript”>
function sayScript(){
alert(“</script>”);
}
</script>

Because of the way that inline scripts are parsed, the browser sees the string “</script>” as if it
were the closing </script> tag. This problem can be avoided easily by escaping the “/” character,
as in this example:
<script type=”text/javascript”>
function sayScript(){
alert(“<\/script>”);
}
</script>

What Is JavaScript

What Is JavaScript? — Explains the origins of JavaScript: where it came from, how it
evolved, and what it is today. Concepts introduced include the relationship between
JavaScript and ECMAScript, the Document Object Model (DOM), and the Browser
Object Model (BOM). A discussion of the relevant standards from the European Computer
Manufacturer’s Association (ECMA) and the World Wide Web Consortium (W3C) is also
included.

Considered the JavaScript expert by many people in the development community, author Douglas Crockford identifies the abundance of good ideas that make JavaScript an outstanding object-oriented programming language-ideas such as functions, loose typing, dynamic objects, and an expressive object literal notation. Unfortunately, these good ideas are mixed in with bad and downright awful ideas, like a programming model based on global variables.

Robots Meta Tag

Not everyone has access to their web server, but they still want to have control over how crawlers
behave on their web site. If you’re one of those, you can still control the crawlers that come to your
site. Instead of using the robots.txt file, you use a robots meta tag to make your preferences known
to the crawlers.

The robots meta tag is a small piece of HTML code that is inserted into the <HEAD> tag of your
web site and it works generally in the same manner that the robots.txt file does. You include your
instructions for crawlers inside the tags. The following example shows you how your robots meta

tag might look:
<html>
<head>
<meta name=”robots” content=”noindex, nofollow”>
<meta name=”description” content=”page description.”>
<title>
Web Site Title
</title>
</head>
<body>

This bit of HTML tells crawlers not to index the content on the site and not to follow the links on
the site. Of course, that might not be exactly what you had in mind. You can also use several other
robots meta tags for combinations of following, not following, indexing, and not indexing:
<meta name=”robots” content=”index,follow”>
<meta name=”robots” content=”noindex,follow”>

Key Factors Effecting Link Quality

According to SEO convention and the information gleaned from the Google patents, there
are a number of factors affecting the quality of your inbound links.

Link Density
Links from pages with fewer outbound links have more influence than from pages where
there are huge numbers of links – see FFAs. Additional outbound links dilute the value of
existing links on a page. My suggestion is to accept links from pages with no more than
10 to 12 links. Avoid pages with 20+ external links.

Site and Page Relevance
A link from a site and page carrying similar content would carry more influence than
from a site without the similar content.

Google Page Rank
For Google ranking purposes a link from a high Page Rank site has even greater
influence. A link from a PR 6+ site is extremely valuable. At the other extreme, I suggest
you are prudent when exchanging links with sites of a PR of zero. The PR0 category
contains a number of banned sites.

Anchor Text
Anchor text is the text that contains or sits alongside a link. This text provides additional
relevance to the quality of a link. Anchor text is written in HTML. On-screen part of the
text shows up as highlighted (usually coloured) or underlined type and part in normal
type. The anchor text for your site could be written in HTML code as follows:
<a href="http://www.yoursite.com"> Your Site Title </a> - A short description of what
you do. <BR>


Link Age
A long established link is deemed by Google to have more value than a recent link. A
rapid build up in links may also be deemed spam. However Google apparently makes an
allowance for a rapid build-up of links generated by news stories.

How to Optimise Your Site

Search engine optimisation is a marketing discipline. It is not a stand alone function.
Before any specific optimisation activity is undertaken it is essential that two areas are
non-search areas are appraised:
Understanding your Organisation’s Online Business Strategy
Good SEO requires a through understanding of your organisation’s overall business
strategy. How does search fit in with activities such as advertising, e-mail and direct
marketing? Is there a marketing plan? What does it say about objectives, strategy and
budgets? What is the overall direction of the business and what can search contribute?

Researching your Market Category, Customers and Competitors
Good SEO also requires a thorough understanding of the market category within which
the search project and web site will compete. What is the category size and how is it
developing. What other channels to market are there? What information is available
regarding their behaviour and attitude of customers? What role in the buying process is
played by the search marketing? Who are current and likely competitors? Once the above
is fully grasped you can proceed to the first real activity of SEO; Keyword selection.

Keyword Selection - Factors
Keyword selection is the first search specific discipline. Having explained that spiders
read and index text, we find that some text is more important than others. That text is
keywords. Valuable keywords are the words or phrases that prospective customers use
when searching in your market category. Keyword selection is therefore crucial and has
implications for so much else within search. I have drawn up a list of factors that should
be taken into account when selecting keywords.
Category Priorities
The first thing to remember is that the number of keywords you can use on any one site
or page has a finite limit. A general recommendation is that there is an overall limit of 20

Table basics in HTML

It’s reasonably straightforward to create a simple table when hand-coding markup. The
bare essentials of a single table is an opening <table> tag, followed by at least one table
row (a <tr>), followed by at least one table cell (a <td>, meaning “table data”). Here’s an
example:
<table>
<tr>
<td>Some data</td>
</tr>
</table>

That’s about as minimalist as you can get when it comes to creating tables, but you’re
unlikely to create a table with only one item of data, so let’s make things a touch more
interesting. The following markup is for a two-column table with four rows of data (the
presentational border attribute is just here as a visual aid to better distinguish the layout
of the table, and its effect should be replicated with CSS in a production environment):
<table border="1">
<tr>
<td>Name</td>
<td>Place of residence</td>
</tr>
<tr>
<td>Paul Haine</td>
<td>Oxford</td>
</tr>
<tr>
<td>Vikki Roberts</td>
<td>Bracknell</td>
</tr>
<tr>
<td>Leon Boardman</td>
<td>London</td>
</tr>
<tr>
<td>Emma Sax</td>
<td>Peaslake</td>
</tr>
</table>

Anatomy of an XHTML document

Finally, let’s look at how a strict XHTML 1.0 document is laid out:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Our title</title>
</head>
<body>
<p>Our content</p>
</body>
</html>
Let’s now go through this markup one line at a time.
Doctype declaration
First we see a doctype declaration. A doctype declaration provides an indication as to
what Document Type Definition (DTD) you’re going to be writing your markup to. A
DTD is basically a page detailing the rules and grammar of the markup. It can look a little
daunting, but let’s break it down and examine each piece. The doctype starts off like this:
<!DOCTYPE
Nothing much more to say here; this is just how a doctype begins. The root element of the
document that will appear after the doctype declaration—the <html> element—needs to
be specified immediately after the opening of the declaration:
<!DOCTYPE html
Note that we can use html or HTML, depending on the version of (X)HTML we’re writing to
and how we’re writing it. For all XHTML doctypes, the root element should be in lowercase,
but for HTML doctypes the root element may be uppercase if the rest of your tags
are written so.
Following that, we have the word PUBLIC:
<!DOCTYPE html PUBLIC
This indicates that the DTD we’re about to reference is publicly available. If the DTD was
private, then we would use SYSTEM instead (as in “a system resource,” probably a locally
held resource somewhere on your network).
Next we have the Formal Public Identifier (FPI), which describes details about both the DTD
and the organization behind the DTD.

XHTML vs. HTML

The question of whether to use XHTML or HTML will often not even come up in an average
web project; most web designers these days will naturally gravitate toward XHTML, as
it is perceived as being new, advanced, and the “X” makes it sound cool. The truth is,
XHTML isn’t as different from HTML as people think, and the purpose of this section of the
chapter is to discuss exactly how XHTML differs from earlier versions of HTML, debunk
some myths and misconceptions about XHTML and HTML, examine the issues behind
MIME types, and cover when it is (and isn’t) appropriate to use XHTML over HTML.

Differences between XHTML and HTML

There are several rules that apply to XHTML that do not apply to HTML. These are fairly
straightforward and you may know some (or all) of them already, but to reiterate:

The <html>, <head>, and <body> tags are all required in XHTML.

The <html> tag must have an xmlns attribute with a value of http://www.w3.org/
1999/xhtml.

All elements must be closed. I touched upon this earlier, but just remember that an
opening tag must have either an equal closing tag (if it’s a container tag) or a selfclosing
space-plus-slash.

All tags must be written in lowercase.

All attribute values must be quoted with either single quotes or double quotes.
Thus, class=page is invalid but class="page" and class='page' are both fine.

All attributes must have values. Some attributes, such as the selected attribute
used with the <option> tag, could be written in a shortened form in HTML—that is,
<option selected>data</option> would be valid. In XHTML, however, you must
write <option selected="selected">data</option>.

Ampersands should be encoded. That is, you should write &amp; instead of just &.
This is true wherever the ampersand is: in your content or in a URL.