Blogs about web-design:
Title Vs. Alt.
Here are my tips to understanding the intentions and key differences between the alt and title attributes for <img /> tags.
Alt:
The alt attribute is intended to be used instead of the image, rather than in addition to the image. It should describe all of the objects in the image. (Though technically incorrect, alt attributes are sometimes known as alt tags.)
The alt attribute is the text that should appear if the image doesn't load, or if visitor can't see image. The alt text may also appear in the image's place while the image is loading.
Include alt attributes for all images. For decorative images such as bullets or spacers, use an empty alt attribute (alt=""). (Of course, decorative images really ought to be part of styling, not content.) Don't use alt attributes for keyword stuffing.
IE6 will show the alt attribute when a visitor hovers over an image. Google uses an image's alt attribute when indexing images.
Title:
An image's title attribute should be used for additional, non-essential information.
In most browsers (IE7 and above, Firefox, Safari, et cetera), the title attribute will appear when the visitor hovers over the image.
Note that the title attribute can be used on many tags, not just images. For example, one good use of the title attribute is to add descriptive text about the destination on anchors / links.
getElementsByClass.
You know about getElementById(), and getElementsByTagName(), but what about getting elements by class?
Yes, there is a native JavaScript function getElementsByClassName(). It is supported by FF3+, Safari 4+, Chrome 4+, Opera 10+, but not IE6, IE7, or IE8.
Here's a simple way to take action on all elements with a given class:
var theScript = document.createElement('script');
<h2 class="myAwesomeClass">My Awesome Header</h2>
<p>This is a paragraph.</p>
<script type="text/javascript">
elems = document.getElementsByTagName("h2");
for ( i=0; i<elems.length; i++ )
{
if ( elems[i].className == "myAwesomeClass" )
{ elems[i].style.color = "red"; }
}
</script>
For the HTML shown, the JavaScript creates an array of all elements with a given tag name. A for loop checks the class of each element in the array, and performs some action on that element if its class matches. (If your class occurs on multiple types of elements, just specify "*" for the class name. )
This is a pretty good solution for just a few lines of code. For much more flexibility a just a few lines of code more, Dustin Diaz has written his own getElementsByClass function.
Tales of Shame.
I have heard a few tales of the shame of Internet Explorer 6, the web browser famously despised by web developers (even its own creator). Submitted for your approval, here are a couple of those tales:
- Six years passed between the release of Internet Explorer 6 (2001) and the next version, IE7 (2007). Why so long? I was told that after IE6 was released, Microsoft decided that browser wars were won. The development team disbanded. I recently mentioned this story to an actual longtime Microsoft employee, who said "that sounds about right".
- An large technical organization has some critical business applications which only work in IE6. They will be upgrading all their workstations to Windows 7, which includes Internet Explorer 8, but not IE6. The solution? Instead of upgrading the legacy web application, this organization will provide virtual WinXp machines with IE6.
On URIs.
To call an asset (such as an image, stylesheet or JavaScript) from same directory as the current page, then start the URI with the filename. Example:
<img src="happy-kitty.jpg" />
To call something from the same domain as the current page, then start the URI with / and include the directory structure. Example:
<img src="/images/happy-kitty.jpg" />
To use the same protocol as the existing page, start the URI with //, and include the entire directory structure.
<script src="//library/the-script.js"></script>
This is useful for when you need to call assets via HTTPS, but only when your page itself has been delivered to the browser via HTTPS.
To pull in an asset from anywhere in the world wide web, just use the fully qualified URI.
<img src="http://somthing.com/best-kitty.jpg" />
Selecting Children in the DOM.
Sometimes, it's necessary to use JavaScript to walk through the HTML elements on a page. The function childNodes() function (as well as firstChild, lastChild, and previousSibling) seems like a great great way to to this. Unfortunately, it'll produce vastly different results in different browsers.
Firefox evaluates any white space in the element as a text node, so if your HTML is like this:
<div id="theDiv">
<a href="http://www.wired.com" id="theA">Wired</a>
</div>
then firstChild will be the line break. Other browsers will return the anchor element as the firstChild. Blech.
Instead, use getElementsByTagName("*"), which will get only tags. For the above HTML, this JavaScript will get the link value:
theAnchor = document.getElementById("theDiv").getElementsByTagName("*")[0];
theHref = theAnchor.getAttribute("href");
Since the first element inside theDiv is the anchor.
Remaining blogs about web-design:
- Cheap, Quick and Good? — 4.3.2011
- Quick And Easy CSS Targeting. — 1.11.2011
- Fantastic Tabs. — 7.31.2010
- Dynamically Adding a JavaScript Element. — 7.21.2010
- Domain Jokes. — 4.20.2010
- Browser Support. — 4.7.2010
- Opera Mini on the iPhone. — 3.26.2010
- IE8's Compatibility Mode. — 3.20.2010
- iPhones and Flash. — 3.9.2010
- GoDaddy's Superbowl Ads. — 2.13.2010
- The Save Icon. — 2.7.2010
- The Paranoid Web Developer. — 12.31.2009
- Appending an External JavaScript File. — 12.27.2009
- Photoshop - Seamless Background Photos. — 12.14.2009
- Pixels versus Ems. — 11.29.2009
- Print Style Sheets. — 11.7.2009
- Programming Web Services with SOAP. — 10.22.2009
- Adventures in Apache - mod_speling. — 9.20.2009
- Noscript. — 8.18.2009
- Drupal Tricks for Dummies. — 6.7.2009
- Firefox - Zoom Text Only. — 6.3.2009
- The Developer's Prayer. — 5.20.2009
- Smooth Animation Using jQuery's slideToggle() - Details. — 5.6.2009
- Testing CMSs. — 4.16.2009
- Comment Away. — 3.16.2009
- What I'd Test. — 2.9.2009
- 300 Million Reasons to Optimize. — 2.2.2009
- Re-Woot. — 1.28.2009
- Twitters are no Substitute for Blogs. — 1.20.2009
- Information When it's Needed. — 12.11.2008
- Services and Uptime. — 11.19.2008
- The Market Chooses You. — 9.23.2008
- jQuery - Vertical Show / Hide. — 9.9.2008
- jQuery - Tablesorter. — 9.7.2008
- jQuery - Core Table Functionality. — 8.31.2008
- jQuery - A Library. — 8.27.2008
- My Evening at Refresh. — 7.24.2008
- Experiments with IE6 and Alpha .PNG Solutions. — 7.13.2008
- Custom Dreamweaver Snippets Folder — 6.12.2008
- Webmonkey is Back. — 5.21.2008
- Articles Too. — 5.4.2008
- Naked, Not Broken. — 4.9.2008
- Click Here, I've Been A Jerk to You. — 3.31.2008
- Alpha Transparent .PNGs in IE6 Using Fireworks. — 2.21.2008
- Hacked the Gibson. — 1.29.2008
- Time Flies When You're Coding. — 12.27.2007
- Random Bits. — 11.9.2007
- Hot .PNG News. — 10.17.2007
- Screen Reading. — 9.10.2007
- Clearfix (with an IE7 solution). — 8.21.2007
- A CSS Styled Form. — 7.18.2007
- Good and Bad Podcasts. — 6.11.2007
- Equal Height Columns. — 5.27.2007
- (Not Quite) PNG-Tastic! — 4.29.2007
- Free At Last. — 4.26.2007
- Nudity & Distractions. — 4.5.2007
- Welcome to the Talkies. — 2.18.2007
- It's called "Pro Bono". — 1.24.2007
- Wii The Eff? — 1.23.2007
- Goomba Code. — 1.9.2007
- Internet News. — 9.13.2006
- More Bitching. — 8.28.2006
- Web Devloper Extension. — 6.22.2006
- How'd He Do That? — 6.18.2006
- Google Analytics. — 6.6.2006