WEB STANDARDS CREATIVITY
WEB STANDARDS CREATIVITY
WEB STANDARDS CREATIVITY
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
228<br />
if (typeof event.stopPropagation != "undefined")<br />
{<br />
event.stopPropagation();<br />
}<br />
else<br />
{<br />
event.cancelBubble = true;<br />
}<br />
return true;<br />
};<br />
This short function is mainly about getting around browser differences in event handling. The first conditional checks whether an<br />
event object was passed to the function itself. This is normally done automatically by the event listener, but that doesn’t happen<br />
with Internet Explorer 5.x. If the event object doesn’t exist, we default to window.event, which is what Internet Explorer 5.x uses.<br />
Next, we check whether the event object method stopPropagation() exists. This is the W3C standard method for stopping an<br />
event from bubbling upwards (that is, stopping the mousedown event on the anchor from also registering on the h2). If stop-<br />
Propagation() exists, we call it. However, Internet Explorer uses a proprietary property of the event object called cancelBubble<br />
to stop event bubbling. If stopPropagation() doesn’t exist, we set event.cancelBubble to true, and this has the same effect.<br />
Now we need to deal with the click event. You might have noticed that to add the mousedown event listener to the anchor,<br />
we used the abstracting function from the resolution-dependent layout, attachEventListener(), but for the click event, we<br />
actually use one of the old event handlers, the onclick property. Because Safari doesn’t allow us to stop the default action<br />
of click events added using the W3C event listeners, we need to revert to the dot property. In practice, this shouldn’t cause<br />
much of a problem. We’re adding the event handler to an element created by our own script, so it shouldn’t have any other<br />
event information attached to it that might conflict.<br />
The function that the click event handler points to is clickExpandCollapse():<br />
function clickExpandCollapse()<br />
{<br />
if (!hasClass(this.parentNode.parentNode, "collapsed"))<br />
{<br />
addClass(this.parentNode.parentNode, "collapsed");<br />
}<br />
else<br />
{<br />
removeClass(this.parentNode.parentNode, "collapsed");<br />
}<br />
return false;<br />
};<br />
This does a simple toggle of the class on the div that surrounds the h2. If that div doesn’t have a class of collapsed, we add<br />
the class. If it does have a class of collapsed, we remove the class. This makes it easy to style the different views of a collapsed<br />
and expanded module purely through the CSS.<br />
When dealing with element classes in clickExpandCollapse(), we always use these custom functions: hasClass(),<br />
addClass(), and removeClass(). These make it easier to work with multiple classnames on the same element. I’ve included<br />
the functions in their own library file, class_names.js, so they can easily be included in other projects:<br />
function hasClass(target, classValue)<br />
{<br />
var pattern = new RegExp("(^| )" + classValue + "( |$)");