Skip to content

Instantly share code, notes, and snippets.

@Comiccobe
Created October 14, 2013 11:04
Show Gist options
  • Save Comiccobe/6974026 to your computer and use it in GitHub Desktop.
Save Comiccobe/6974026 to your computer and use it in GitHub Desktop.
Check if class exists in stylesheet.
<!DOCTYPE html>
<html>
<head>
<style>
.class-checker {
position: absolute;
left: -99999px;
top: -99999px;
width: 9999px;
height: 9999px;
display: none;
}
.existing-class {
width: 16px;
height: 16px;
}
</style>
</head>
<body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function classExists(className) {
var div = document.createElement("div");
div.className = "class-checker " + className;
document.body.appendChild(div);
var width = $(div).width();
document.body.removeChild(div);
return width !== 9999;
}
alert(classExists("existing-class"));
alert(classExists("fictional-class"));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment