Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created April 9, 2010 05:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save subtleGradient/360913 to your computer and use it in GitHub Desktop.
Save subtleGradient/360913 to your computer and use it in GitHub Desktop.
HTML5 Template with className states and browser detection
// Swap classes onload and domready
$(function(){ $('html').removeClass('not-ready').addClass('ready'); }); // jQuery
$(window).load(function(){ $('html').removeClass('loading').addClass('loaded'); });
// target a specific version
if (navigator.isIE == 6){
// do stuff for IE6
}
// Deliever different behaviour for a group of IE versions
if (navigator.isIE < 9){
// ie's notorious png-tranparent-pixels-go-black-on-fade issue
$('png-image').hide();
} else {
$('png-image').hide(400);
}
// Check for IE generally
if (navigator.isIE){
// do IE stuff
} else {
// do other stuff since NaN is falsey
}
// Swap classes onload and domready
document.addEvent('domready',function(){ $$('html').removeClass('not-ready').addClass('ready'); }); // MooTools
document.addEvent('load',function(){ $$('html').removeClass('loading').addClass('loaded'); }); // MooTools
// target a specific version
if (navigator.isIE == 6){
// do stuff for IE6
}
// Deliever different behaviour for a group of IE versions
if (navigator.isIE < 9){
// ie's notorious png-tranparent-pixels-go-black-on-fade issue
$('png-image').setStyle('display','none');
} else {
$('png-image').fade('out');
}
// Check for IE generally
if (navigator.isIE){
// do IE stuff
} else {
// do other stuff since NaN is falsey
}
<!DOCTYPE html>
<html lang=en class=no-js>
<head>
<meta http-equiv=Content-Type content="text/html; charset=UTF-8">
<!--[if IE]><![endif]-->
<!--[if IE]><meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1"><![endif]-->
<title>Page Title - Site Title</title>
<meta name=description content="">
<meta name=keywords content="">
<meta name=author content="">
<script>try{
if(typeof navigator=='undefined')navigator={};
HTML=document.getElementsByTagName('html')[0];
HTML.className="js loading not-ready";
HTML.className+=(this.top===this.window?' not-':' ')+'framed';
}catch(e){}</script>
<meta name=viewport content="width=device-width; initial-scale=1.0;">
<!--[if IE 9]><script>try{HTML.className+=' ie9'}catch(e){};navigator.isIE=9</script><![endif]-->
<!--[if IE 8]><script>try{HTML.className+=' ie8'}catch(e){};navigator.isIE=8</script><![endif]-->
<!--[if IE 7]><script>try{HTML.className+=' ie7'}catch(e){};navigator.isIE=7</script><![endif]-->
<!--[if IE 6]><script>try{HTML.className+=' ie6'}catch(e){};navigator.isIE=6</script><![endif]-->
<!--[if lt IE 6]><script>try{HTML.className+=' ie5'}catch(e){};navigator.isIE=5</script><![endif]-->
<!--[if gt IE 9]><script>try{HTML.className+=' ie10'}catch(e){};navigator.isIE=10</script><![endif]-->
<!--[if !IE]>--><script>try{HTML.className+=' not-ie'}catch(e){};navigator.isIE=NaN</script><!-- <![endif]-->
<link rel=stylesheet href="css/style.css?v=1">
<link rel=stylesheet href="css/handheld.css?v=1" media=handheld>
<script src="js/modernizr.js?v=1"></script>
</head>
<body>
<div class=page>
<div class=head>
</div>
<div class=body>
<div class=leftCol>
</div>
<div class=rightCol>
</div>
<div class=main>
</div>
</div>
<div class=foot>
</div>
</div>
<script src="js/framework.js?v=1"></script>
<script src="js/app.js?v=1"></script>
<!--[if lt IE 7]><script src="js/dd_belatedpng.js?v=1"></script><![endif]-->
<script>
if(document.location.hash.indexOf('DEBUG')+1){
document.write('<script src="js/profiling/yahoo-profiling.min.js?v=1"><\/script>');
document.write('<script src="js/profiling/config.js?v=1"><\/script>');
}
_gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
;(function(d,t,src){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=true;g.src=src;s.parentNode.insertBefore(g,s)})
(document,'script','//www.google-analytics.com/ga.js')
</script>
</body>
</html>
@subtleGradient
Copy link
Author

the PS3 version needs testing to double-check that it all works

@subtleGradient
Copy link
Author

if you need 100% IE5 support, then I pity you

@subtleGradient
Copy link
Author

Beware of setting opacity to 0 for .loading .whatever some browsers don't properly repaint (or whatever) when removing the loading className later. There's a workaround, but I'm not going into it now.

@subtleGradient
Copy link
Author

I'm really not sure why document.documentElement.className doesn't work in PS3. It must be resolving as some other object. I'm not sure what's going on there. Probly deserves more testing.

@subtleGradient
Copy link
Author

navigator.isIE must be NaN in non-ie browsers to get stuff like navigator.isIE <= 7 to work. If you set it to almost any other falsy value it'll type coerce to 0 when using operators like <= on it (thereby making navigator.isIE < 7 pass for non-ie browsers). Using NaN fixes that. NaN is the most negative thing ever, it's not even equal to itself!

@subtleGradient
Copy link
Author

With this version I chose to go with a global HTML variable to keep from having to do the lookup multiple times and shorten the number of characters.

@subtleGradient
Copy link
Author

I also chose to use getElementsByTagName instead of documentElement.
This should be supported by more browsers.

@subtleGradient
Copy link
Author

Version 3fa146 includes lots of good stuff from http://github.com/paulirish/html5-boilerplate

@subtleGradient
Copy link
Author

Yes, I know that using isIE in your code is generally bad practice.
I would never use it in library code.
But as a pragmatic developer, sometimes I just need to Get Stuff Done™

@subtleGradient
Copy link
Author

Lately I've been avoiding using unnecessary semicolons in my JS. But since this is an HTML file I'm still going to keep using semicolons. Why? Because 'crazy stuff' can happen and I don't feel like thinking of every single crazy thing that could possible happen to be sure that it wouldn't break my stuff.

I'm using this file as the base for PHP, Rails, ASP classic, .NET, Django, etc…
So there's no telling if some random post-processor might be installed that I don't know about or something 'wacky'.

@subtleGradient
Copy link
Author

Since this edition adds the IE classnames using JS, you're not going to get those classnames when JS is disabled. If that's an issue for you then you should use conditional comments to create the body tag instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment