Category Archives: tutorial

Events in HTML

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

Lightscribe setup on Mac

I purchased a Lightscribe DVD writer to experiment with creating some interesting DVD labels. I work on the Mac platform and normally most hardware/software combinations are simply plug and play. Not so in this case. I sent an email to Lacie seeking help. Their step by step solution fixed the problem. I’m providing it here in the event you are having the same issues.

Symptoms: I was able to write my data to my drive, but when I tried to print using the software that Lacie supplied with the drive, the software would not recognize my drive. I learned that there are four separate files that must be loaded in a particular sequence that the system needs to work. Following is the sequence of steps offered to me by Lacie support that fixed my problem.

Disclaimer: This article is written 7/13/2009. By the time you read this article, the software requirements, their versions, and the links I am providing may have changed or no longer exist. This article is provided as a courtesy to get you in the right direction for further inquiry.

“First, please ensure you are using actual Lightscribe-compatible media and be sure to put the media inside the drive gold-side (label side) down and close the tray before trying to print. If that doesn’t work please follow the instructions below for further assistance.

The following steps fix almost all Lightscribe software-related issues.

Step 1

Download the latest version of the Lightscribe Host Software and LaCie Lightscribe Labeler 1.2 from LaCie.com.   Also, go to Lightscribe’s website to download the latest system software.

Step 2

Find the existing LaCie Lightscribe Labeler in your Applications directory. Drag it to the trash.

Step 3

In the Finder go to /Library/Receipts  (this can be done by double-clicking the Macintosh HD icon, double-clicking Library, then double-clicking Receipts)

Step 4

Delete the two entries that are lightscribe related. There are usually two, but if there are more, delete them anyway. A normal Mac OS install doesn’t have Lightscribe software pre-loaded.

Step 5

Empty the trash.

Step 6

Install the newer Light Scribe Host Software.

Step 7

Install LaCie Lightscribe Labeler 1.2 .

Step 8

Install Lightscribe System Software

Unless your drive is physically broken; this should fix the issue. A test burn should go through without any problems. If you continue having problems try the drive on a second Mac to confirm there are no software issues with your computer. Make sure you are actually using Lightscribe media with the label gold-side down in the drive. You might also check to see if the issue replicates on a second computer and swap out the data cable (USB or FireWire) as well.”

Lightscribe Host Software 1.8.15.1 for Mac
http://www.lacie.com/us/support/drivers/driver.htm?id=10082

Lightscribe Library 1.14.25.1
http://www.lacie.com/us/support/support_manifest.htm?id=10340

LaCie Lightscribe Labeler 1.2 for Mac
http://www.lacie.com/us/support/drivers/driver.htm?id=10061

Lightscribe System Software for Mac
http://www.lightscribe.com/downloadsection/mac/index.aspx?id=813

document.write() function

One of my online students was baffled moving from HTML coding to javascript. This is not surprising since they are very different languages. For my students, and others, that are struggling, the following tutorial may be helpful, http://w3schools.com/js/js_intro.asp.

I don’t consider myself a coder. Others are far more qualified. Still I’ll take a stab at answering specific question you may have. For students, post the question in the discussion forum. For visitors post the question in my contact form.

For this article, let me see if I can address the things this student has questioned.

Q:”I typed the code in as shown, but I didn’t comprehend how it worked.”

A: Did the code work? Some of the authors examples are Explorer and PC centric. For example the status bar code does not work in Firefox. So if your code doesn’t work, I can give you an alternate approach to illustrate the concept of an event handler.

Q: What are the plus signs for in the script on J-15?

A: The “+” sign can perform different functions depending on the context. In an arithmetic operation, it performs addition. When it is between “strings”, i.e text, it concatenates the strings together. For example, document.write(“I love” + ” javascript”), prints the two strings “I love” and “javascript” together so it looks like “I love javascript” on the screen. It is often used to mix strings and variables together. You can find out more at the following URL http://w3schools.com/js/js_operators.asp

Q: Why are there parentheses, then quotes, then quotes within quotes?

A: On page J-15 in your text, there is code in Figures J-14, 15, and 16. In figure J-16 is the following code:

document.write(“<h3>” + name + “, to subscribe to our newsletter, fill out the form below. </h3>”)

document.write() is a function. It prints what ever is in the parenthesis to the screen. The code wants the following to print to the screen. The name of the person  followed by the text “to subscribe…”. The name information is retrieved by the variable assignment in Figure J-14, ie.

var name=prompt(“Type your first name”)

The person is prompted to enter their first name. That information is saved in the variable “name”. Then the document.write() function prints the contents of the variable, the following text, and surrounds it all with an <h3> tag. So it prints the <h3> tag, then adds the variable value, then the text followed by the ending </h3> tag.

Let me know if you found this little tutorial helpful. Drop me a line.

img width and height attributes

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

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