views:

115

answers:

5

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

A: 

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

<link rel="stylesheet" type="text/css" href="my_external_css_file.css" />
faileN
I don't this this is what the OP asks. He wants to conditionally load css when js is disabled.
cherouvim
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.
Spudley
I want to change the look of page if javascript is disabled.
metal-gear-solid
AH okay, now I got the question - Sorry so far...
faileN
+7  A: 

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.

cherouvim
+1 pls can you provide any link on how to do this
metal-gear-solid
If you use jQuery then simply do `$(function(){$('body').addClass('jsEnabled');});`
cherouvim
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.
metal-gear-solid
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.
cherouvim
+1  A: 

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

yc
It's not working on IE7
metal-gear-solid
Strange. It's both @import and inline CSS are registering properly for me. Try opening http://htmlto.com/page.html with JavaScript disabled. Is the background purple and the text styled in small-caps for you? It is for me.
yc
See my edit with screenshot.
yc
If i keep javascript off in IE7 it shows background showing purple. otherwise white.
metal-gear-solid
...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.
yc
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.
metal-gear-solid
Yeah, it looks like that's the case.
yc
+1  A: 

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.

gertas
+1 for this extreme hack, I wouldn't use it though.
cherouvim
+1  A: 

Hi,

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

Dr.Molle