1

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?

5
  • 2
    I might be a little confused. If you apply any css styles dynamically from within javascript, then those same styles will necessarily not be applied if javascript is turned off. eg, $('a').css('color', 'red') will only be executed if javascript is turned on. Is that what you're looking for? Commented Jun 29, 2011 at 18:26
  • @digitalbath I'd upvote that if it were an answer. That sounds like what tehman is looking for. Commented Jun 29, 2011 at 18:32
  • Yes this seems like the simplest solution to my question. Thank you Commented Jun 29, 2011 at 18:35
  • It's the simplest solution, but depending on what you're actually trying to achieve it might not be the best solution. If you find yourself moving most of your CSS rules out of a .css file and into a .js file, you're probably doing something wrong. See my answer further down for an alternative approach. Commented Jun 29, 2011 at 18:39
  • I have all of my CSS rules in a .css file for whether the browser has javascript enabled or not. I just want added functionality for when it is enabled. And if it is not enabled then I do not want the CSS rules within the javascrupt to alter the other rules within the .css file. Commented Jun 29, 2011 at 18:44

3 Answers 3

4

Take a look at this post: http://forum.jquery.com/topic/loading-stylesheets-on-the-fly

Sign up to request clarification or add additional context in comments.

8 Comments

i dont want to link to a css file, how would this work if i wanted the css within the script?
@tehman How do you expect to load the css within the script if javascript is disabled?
@ghoppe, he wants to not load the CSS if JavaScript is disabled.
@tehman, if you want the whole CSS inside your JavaScript file, then create a string with it and instead of adding a <link/>, add a <style/>.
@Radu well, now I'm really confused. By definition, any CSS in the jQuery script will not be applied if the script is disabled. This question needs work.
|
2

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

Thanks for the alternative approach, did not think of this but it does look like it could come in handy. Saved for future reference.
0

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.

4 Comments

This would cause the opposite.
But this is how it ought to be done. If Javascript doesn't work, have a back up CSS file to fix whatever things were wrong.
Your answer's first line is a bit backwards.
I already know this, and I have said this is the best way to do it. If he wants to mess up his website, he can use the second part. I answered it thoroughly and yet I have 0, woooooh yay.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.