How would you use CSS within a jQuery script, so that if javascript were disbaled within the browser both the jQuery script and CSS would not show up?
3 Answers
Take a look at this post: http://forum.jquery.com/topic/loading-stylesheets-on-the-fly
8 Comments
<link/>, add a <style/>.Another approach I've seen is to dynamically add a special css class to the body tag via javascript, and then preprend javascript-only CSS selectors with that css class.
eg:
<script>
document.body.className += ' scriptson';
</script>
<style>
.scriptson a {
/* only applied if javascript is enabled */
}
</style>
1 Comment
Put the <script> and <link> tag in a <noscript> tag.
To do the opposite, use $("head").append("<link href="CSSFILE" />"); or of the sort so that if javascript is disabled, the script will not run and the css will not be included. Note though, because it takes Javascript time to load, it will be jumpy. It is not what you want, which is why the <noscript> is the better solution.
$('a').css('color', 'red')will only be executed if javascript is turned on. Is that what you're looking for?