8

how to load an external css file if javascript is disabled.

Should work in IE 6 to 9, Firefox Google chrome, Safari

I tried <noscript> And keeping <noscript> inside but it's not working in IE 7

5 Answers 5

15

I'd do it the other way around. Always load the css which will contain rules prefixed with body.jsEnabled. Then simply add a class "jsEnabled" on body via javascript.

This way of "discovering capabilities" is approximately how http://www.modernizr.com/ works.

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

6 Comments

If you use jQuery then simply do $(function(){$('body').addClass('jsEnabled');});
I want to load the css <link rel="stylesheet" type="text/css" href="non-js.css"> only if javascript is disabled and want to keep this css after all other css files.
What you are trying to achieve may not be possible, but only emulated by the method I'm describing. Of course, a better solution may exist.
I tried this method and I get the page rendered first with the default css (JavaScript off) and then the jsEnabled is applied. The result is a big flicker on load for JavaScript enabled clients which looks bad.
You need to apply the class as soon as possible. Also, do you use @import css?
|
10

I've tested this method in IE7, and it works. Putting <style> tags (instead of a <link> within the <noscript>

<noscript>
<style type="text/css">
 @import url (yourexternalfile.css);

body{
background-color: purple;
}

</style>
</noscript>

EDIT:

Here's a screenshot of this working in IE7. alt text

6 Comments

Strange. It's both @import and inline CSS are registering properly for me. Try opening htmlto.com/page.html with JavaScript disabled. Is the background purple and the text styled in small-caps for you? It is for me.
If i keep javascript off in IE7 it shows background showing purple. otherwise white.
...That's the point. The purple style is applied within the noscript tag; same with the small-caps on the h2 tags. On that page, all of the styles are loading within noscript tags, to prove that it works. (The page is unstyled when JS is enabled.) In your case, you can use @import url('your-no-js-css') to load CSS only when JS is disabled.
ok you mean @import works but <link> not for IE7, ok, I will check in other browsers. and will give + 1 for for this trick.
Yeah, it looks like that's the case.
|
4

Try this:

<html>
  <head>
    <script type="text/javascript"> document.write('<'+'!--'); </script>
       <link rel="stylesheet" type="text/css" href="non-js.css">
    <!-- -->  
  </head>
  <body>
      <script type="text/javascript"> document.write('<'+'!--'); </script>
       Visible when no-JS 
      <!--  --> Always visible
  </body>
</html>

Hack, but it is correct with HTML. If JS is enabled then comment start control tag is printed into page - then second start tag is ignored and ending tag closes commented content. So if JS is enabled link tag will be commented out and not downloaded at all.

1 Comment

I'm tempted to use this, it's the only method that has worked in my case. I want to load some custom fonts via external stylesheets if js is not enabled. If js is enabled instead i want to load the fonts via typekits webfont loader so i can fire off js events after the fonts have loaded.
1

while <noscript> is not allowed in <head>, and <link> + <style> are only allowed in <head> , you also could use this:

<link id="noscriptStyle" rel="stylesheet" type="text/css" href="my_external_css_file.css" />
<script type="text/javascript">
document.getElementById('noscriptStyle').parentNode.removeChild(document.getElementById('noscriptStyle'));
</script>

But by myself I would prefer the method posted by cherouvim

Comments

-1

CSS-Files have nothing to do with enabled/disabled javascript...

<link rel="stylesheet" type="text/css" href="my_external_css_file.css" />

3 Comments

I don't this this is what the OP asks. He wants to conditionally load css when js is disabled.
The question is ambiguious, but I the way I read it, what he wants is to include a specific CSS file for users without Javascript.
I want to change the look of page if javascript is disabled.

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.