Created
October 14, 2013 11:04
-
-
Save Comiccobe/6974026 to your computer and use it in GitHub Desktop.
Check if class exists in stylesheet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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