Posts Tagged ‘html’

Events in HTML

Monday, November 9th, 2009

When beginners are learning web design, events can often be confusing. Are events part of HTML, Javascripts, or the DOM? What are they and how do we use them?

What is an Event?

HTML stands for Hypertext Markup Language. It is a language we use to give instructions to a browser how to structure and present information on a web page. A markup language cannot make decisions, store values, or respond to user choices beyond simple form elements. In order to make a HTML document dynamically responsive to user interaction, we must have a mechanism for the browser to be aware of itself and the different actions that take place. We do that through events.

An event is everything that happens within the scope of a session in a browser window. For example, opening and closing a browser window are events and are tracked by the browser software. Loading and unloading an HTML file are events. HTML elements have events. For example, with hypertext, i.e. links, moving the mouse over a link, off a link, or clicking a link are all events on that element.  We can take advantage of the fact that the browser is aware of every event that happens. We can give a browser instructions to follow when an event occurs and thereby introduce change in the browser in response to user choice. That is how we introduce interactivity.

The Document Object Model (DOM)

The ability of a browser to be aware of the elements it contains and “sense” events that happen is a result of requiring a browser to structure how elements on a page are referenced and organized. That identification and organizing structure is called the DOM or Document Object Model. It organizes elements in a window into a tree-like structure. Through that hierarchy, it knows how to find and address every element on the page.

Is an event part of HTML, Javascript, or the DOM?

Reading the entry on the DOM in Wikipedia can shed some light on that question. Originally, different browsers gave more or less access to the elements on a web page via the scripting agents that were created to control the pages. IE used Visual Basic and JScripting to control pages rendered in IE. Netscape used Javascript to control pages rendered in Netscape.  This proprietary method of controlling page elements was very inefficient and costly in the web design process and often resulted in pages that would not render correctly in multiple browsers. It became apparent after some time that multiple browsers would be used and that if the World Wide Wide was going to work efficiently, a standardized approach was needed.

The W3C brought the major browser vendors together to create a standardized method for allowing all browsers to control the elements on their pages so that code that was written could be predictably rendered on all browsers. In this light, browser agents (Firefox, IE, Safari, Opera, Chrome, etc) give access to page elements to both HTML and Javascript statements by way of the DOM.

How to access an event

It is up to discussion as to whether it is HTML or Javascript that actually “calls” an event. For example, the one of the first tags used in an HTML document is the <html> tag. Once the browser encounters that tag, it knows to render the page according to its understanding of the HTML language. Once it encounters the <script> tag, it knows to interpret the page statements to whatever language is indicated in the <script> tag. Unless you specifically tell the browser that you are using a scripting language, you can assume you are using HTML.

For example, the code…

<a href=”file1.htm” mouseover=”function1()”>link1</a>

… is an example of HTML coding. In this example the anchor tag calls function1(), defined elsewhere on the page in a <script> tag, when the mouse moves over the anchor link. The anchor tag is an HTML tag. The mouseover DOM event is being accessed, through HTML, in the same way a tag attribute is; i.e. the syntax of attribute name = attribute value is an HTML convention. The value of the event call is scripting code. So is the call an HTML attribute or is it a temporary call to a script using the underlying DOM. My guess is that different authors will argue both explanations.

References:

  1. HTML event attributes, W3Schools. http://www.w3schools.com/html/html_eventattributes.asp
  2. Javascript events, W3Schools. http://www.w3schools.com/js/js_events.asp
  3. Document Object Model Events, W3C. http://www.w3.org/TR/DOM-Level-2-Events/events.html
  4. Document Object Model. Wikipedia. http://en.wikipedia.org/wiki/Document_Object_Model

img width and height attributes

Tuesday, February 17th, 2009

A student asks…

“Just to clarify, the primary purpose of the width and height attributes is to reserve space for the image, not to actually resize the image, correct? “

The width and height attributes on the img tag actually do both; they reserve the screen real-estate and also resize the image.

Reserving real-estate

When you think about, what is HTML markup? It is a collection of content surrounding by tags giving instructions to a browser. All of it is ASCII text. There are  no embedded images or media like you might have in a pdf, or Word document. All resources other than text are referenced as external files. For example, look at the following example:

<img src="images/logo.gif" width="100" height="50" />

The browser is being instructed to reserve a place on the screen 100 pixels wide and 50 pixels tall for the logo.gif image, in the images directory, that will be referenced as an external file. Since all media resides outside the HTML file, then what we are doing is preparing a place on the screen where the media will be shown.

Providing Dimensions

The attribute values of width=”100″ height=”50″ state that a definite amount of screen space is being reserved. The image will be forced into those dimensions. If we do not provide the dimensions, the browser will calculate them based on the actual image dimensions; which may or may not throw off our design. Many designers recommend providing the dimensions to take that additional computational burden off the browser.

Here are some things to think about

  • If the image dimensions we provide are not the same as the original dimension, some distortion will take place
  • If the original image dimensions are much larger than the ones we specify in the attribute values, then we are wasting bandwidth. The entire image has to be downloaded and then resized before rendered. Why download a 1 MB image and resize it, if a 20 kB image will do?
  • If the original image dimensions are much smaller than the ones we specify in the attribute values, then the image will appear pixelated because it is being scaled up.

Best practices

Remember, you always have to think about the output. Keep different versions of the image to be used for different output resolutions. For example, if you have a 1 MB version of a logo for printed material, resize it for web delivery. Determined how many colors, and what size you need for the screen and then save an appropriate version for the web that has the needed dimensions, color depth, and resolution (72ppi). Put those dimensions into the width and height attribute values of your <img> tag. In that way, the image is web optimized. What you see in your image editing program is what you see on your web page. You are taking the resizing calculation burden off the browser, maximizing bandwidth, and preventing your image from distortion problems.

Attribute syntax in HTML and CSS

Thursday, February 5th, 2009

A student asked the following question…

“I understand what we’re doing, but I’m worried about memorizing all of the coding. It might help if I understood the reasons behind all of it. For example, why do font changes need semicolons on them and not just a closing quotation mark like other things?”

Great question and I believe what you are referring to is the difference between an attribute in a HTML tag and a property declaration in CSS. In HTML, an attribute modifies or embellishes a tag. Attributes follow the following syntax:

<tagName attribute=”value”> Some content </tagName>

The value is always surrounded by quotation marks. For some attributes like href for example, it is easy to see.

<a href=”some URL”> link text </a>

Specifying the type of font I want to display is a bit different since we no longer use the <font> tag in xHTML. Cascading Style Sheet, CSS, declarations are used to specify font presentation as outlined in the W3C recommendations1. Properties in CSS are defined using the following syntax

property: value;

If I want to change the font family for a specific paragraph using an inline Cascading Style Sheet declaration, I need to use a CSS declaration within the context of a HTML tag. An attribute was created to accomplish that, the style attribute. The basic syntax of that code would look like…

<tagName style=”CSS declaration”> some content</tagName>

So, for example, lets say I have a <h1> heading element. I would like the heading to be in a Verdana font. If a Verdana font is not available on the viewers computers, I’d settle for an Arial font. If neither is available, I at least want a sans-serif font used. In CSS syntax that would look like the following”

font-family: Verdana, Arial, sans-serif;

Now, let’s place that CSS declaration into a HTML attribute

<h1 style=”font-family: Verdana, Arial, sans-serif;”> Some content </h1>

That is why the punctuation of colons, commas, and semi-colons all have to be included between the quotation marks. It is because the CSS syntax has to fit within the HTML syntax.

References

Lie, Håkon Wium and Bos, Bert (April 2008). Basic concepts. Cascading Style Sheets, level 1 W3C Recommendation. http://www.w3.org/TR/REC-CSS1/#basic-concepts

Why is hypertext blue?

Tuesday, February 3rd, 2009

A student asked the design question, “Why is hypertext blue?”

I was able to speculate why as a designer I might have chosen the color blue over another; like red. Red indicates alarm where blue is less intimidating yet still provides a strong contrast with surrounding black text and a white background. However, I could not answer the question in terms of any supporting specification on the W3C (World Wide Consortium) web site.

After some searching, I discovered the following answer from the inventor of the World Wide Web, Lee Berners. The following quote is an excerpt from his FAQ web page…

“There is no reason why one should use color, or blue, to signify links: it is just a default. I think the first WWW client (WorldWideWeb I wrote for the NeXT) used just underline to represent link, as it was a spare emphasis form which isn’t used much in real documents. Blue came in as browsers went color – I don’t remember which was the first to use blue. You can change the defaults in most browsers, and certainly in HTML documents, and of course with CSS style sheets. There are many examples of style sheets which use different colors.

My guess is that blue is the darkest color and so threatens the legibility least. I used green whenever I could in the early WWW design, for nature and because it is supposed to be relaxing. Robert Cailliau made the WWW icon in many colors but chose green as he had always seen W in his head as green.”

Well there you have it. Blue is a default. There was no design decision on the part of the W3C and the reasons for its usage might be more attributed to  decisions of particular browser developers. My guess is that someone used it, it worked, and others reasoned that “if it ain’t broke, don’t fix it.” Sometimes standards come into practice more out of habit than anything else. This may be one of those instances.

Note: If any reader knows for sure, I welcome a comment indicating as such.

References

Berners, Lee (n.d.) Frequently Asked Questions. http://www.w3.org/People/Berners-Lee/FAQ.html

About WordPad vs NotePad

Thursday, January 22nd, 2009

A student writes the following question…

“When I write my code in Wordpad and save as an htm text document, it looks like one. But after I open it in the browser to see how it looks, the icon in the folder no longer says .htm and it appears to be a Firefox document.

Is that right? Or am I doing something I shouldn’t. I can reopen it by telling it to open in Wordpad, but the icon never looks like a text document again. “

That’s a great question. First, the Windows OS comes with two native programs for creating text files: Notepad, and Wordpad. NotePad allows us to create documents using simple ASCII characters, often referred to as “plain text.” You can see that by the default extension of .txt. WordPad in contrast saves its document using the .rtf extension (Rich Text Format). Rich text format includes codes that describe characters. For example, like with Word (.doc) or OpenOffice.org (.odt), the rtf format provides the ability to control the presentation of visible characters with invisible characters that control boldness, italics, underlines, margins, font-sizes, font-families, etc. Plain text does not include that type of information.

When we create HTML code by hand on a PC, we use NotePad. That’s because we do not want any characters entering our code that shouldn’t be there. HTML documents are created with ASCII or plain text and are OS and platform independent. A HTML document created on a PC can be edited and viewed on a Mac, Sun, Linux, or Unix installation.

In brief… edit in Notepad. Save with a .htm or .html extension instead of the default .txt extension. When you want to edit the file, open it in NotePad. When you want to view or “render” the file, open it in Firefox, Safari, Explorer, Chrome, Opera or any of the other web browsers.