Body onLoad done the separatist way

By Luke Smith on July 17, 2005 5:31 PM

I'm a big fan of separating content from functionality. Clean semantic markup doesn't have a place for javascript. That's icing. I like to keep to this sequence:
  1. Start with content
  2. Markup the content using the appropriate semantic tags
  3. Begin building your stylesheet to define the character of your presentation
  4. Add the necessary container elements to allow for the design/layout your going for*Ideally, you can create an effective space without adding extra tags, but this usually isn't realistic.
  5. Add the layout style to the stylesheet/s
  6. Build the javascript functionality to augment the site (in a separate file)
  7. Add any noscript section/s to replace "necessary" functionality
  8. Lather, rinse, repeat
Rather than clutter the markup with onBlah events and other stuff that doesn't celebrate the content, add the event handlers in a page_init() function. Then add this handy little code snippet to the end of your javascript file to purge that last remnant of markup+code, <body onload="page_init();">: your_javascript.js
if (window.addEventListener) {
     window.addEventListener('load',page_init,false);
} else if (window.attachEvent) {
     window.attachEvent('onload',page_init);
}
I won't take credit for this code. I copied it from some site a while back. I can't recall which, though. If I happen across it, I'll link it here. Now your <body> is clean and fresh.

No TrackBacks (http://lucassmith.name/mt/mt-tb.cgi/22)

ls.n

LucasSmith.name

Luke and Heidi

I'm Luke. I am a front end engineer at Yahoo! on the YUI team.

Mostly I write about code stuff, but occassionally I'll mix in some real life. You've been warned.

Archives

Tags

Feeds

Subscribe to feed Recent entries

Content licensed under Creative Commons

Code licensed under BSD license

©2005 - 2010 Lucas Smith

Powered by Movable Type