Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional CSS include based on <script> <noscript> 2

Status
Not open for further replies.

entidi

Programmer
Sep 18, 2007
6
IT
Hi all,

I implemented a hierarchy browser using javascript when available. Whithout javascript, I wanted a full-expanded menu.
I solved with the following code:

<link rel="stylesheet" type="text/css" ref="style.css"></link>
<noscript>
<link rel="stylesheet" type="text/css" href="style_nojs.css"></link>
</noscript>

It conditionally includes a specific stylesheet when javascript is not found. It works well, but there it is no XHTML1.0 compliant (<link> cannot be buried inside <noscript>).

Any idea or suggestion?
Thankyou in advance...

Implementation:
 
Easy, have javascript write out the link rel of the style sheet you want included when javascript is present. and have the no JS hard coded into the page above.

If there is no JS available. the stylesheet can't get written out. but if there is, it will override the no js stylesheet as long as it comes after it.


Code:
 <link rel="stylesheet" type="text/css" href="style_nojs.css"></link>
<script>
document.write('<link rel="stylesheet" type="text/css" ref="style.css"></link>');
</script>


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
you could include your non-js style as default and write over your it to include your js style when js is available.
 
Because <noscript> cannot be inside <head>, I was thinking also <script> cannot be used.
Checking the w3c documentation carefully, I discovered I was wrong: what a stupid question...

Thank you for your help!
 
Instead of using JS to link another stylesheet, why not use it to append a class to the body element?

That way, you can specify an alternate set of rules that use this class in the same stylesheet.



--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
It sounds good: this way I can encapsulate all the js specific customizations in support.js. The problem is that, apart js is a scripting language in the client side, I don't know ANYTHING about ecma scripting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top