Skip to content

Instantly share code, notes, and snippets.

@theophani
Created March 16, 2011 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theophani/872373 to your computer and use it in GitHub Desktop.
Save theophani/872373 to your computer and use it in GitHub Desktop.
detect if generated content is supported by the browser
/*
Adapted from
https://github.com/KrofDrakula/Modernizr/commit/cdb322bdbdc7b62b4f0fcece08372e6194342f7d
*/
var gencontentSupported = function() {
var doc = document,
d = doc.createElement('div'),
p = doc.createElement('p'),
b = doc.body,
w,
rand = ('' + Math.random()).split('.')[1];
supported = false,
getWidth = function(el) {
if(el.offsetWidth) {
return el.offsetWidth;
}
return parseInt(doc.defaultView.getComputedStyle(el, '').getPropertyValue('width'), 10);
};
p.id = 'gc'+rand;
d.innerHTML = '<style type="text/css">#gc'+rand+'.h'+rand+':before{content:"XXX"}#gc'+rand+'{position:absolute}</style>';
b.appendChild(d);
b.appendChild(p);
w = getWidth(p);
// is size different?
p.className = 'h'+rand;
if(w !== getWidth(p)) {
supported = true;
}
b.removeChild(d);
b.removeChild(p);
return supported;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment