Reply to Re: How to change stylesheet using Javascript?

Your name:

Reply:


Posted by Gιrard Talbot on 12/23/78 11:28

Stuart a Γ©crit :
> typingcat@gmail.com wrote:
>
>> Hello.
>> I know it is possible to change separate elements' style by changing
>>
>> [element].style=[another style];
>>
>> But is it possible to set the entire page's stylesheet?
>> For example,
>> <head>
>> <link rel="stylesheet" type="text/css" href="style1.css" />
>> </head>
>>
>> Can I change "style1.css" to "style2.css" using Javascript and make the
>> page be changed dynamically?
>>
>
> It is possible - something like the following works :
>
> ....
>
> <link rel="stylesheet" href="style1.css" id="stylesheet">

link does not take an id attribute value.

>
> <script type="text/javascript">
>
> function changeStyle() {
> document.getElementById('stylesheet').href = 'style2.css';
> }
>

<link rel="stylesheet" type="text/css" href="default.css" title="Default">

<link rel="alternate stylesheet" type="text/css" href="Foo.css"
title="Foo">

(...)

function changeActiveStyleSheet(previous, new)
{
if(document.styleSheets)
{
for (var StyleSheetIterator = 0; StyleSheetIterator <
document.styleSheets.length; StyleSheetIterator++)
{
if(document.styleSheets[StyleSheetIterator].title == previous)
{
document.styleSheets[StyleSheetIterator].disabled = true;
};
if(document.styleSheets[StyleSheetIterator].title == new)
{
document.styleSheets[StyleSheetIterator].disabled = false;
};
};
};
}

/*
http://www.w3.org/TR/2000/REC-DOM-Level-2-Style-20001113/stylesheets.html#StyleSheets-StyleSheet
*/
(...)

Default stylesheet: <input type="radio" ...
onclick="changeActiveStyleSheet('Foo', 'Default');">

Foo stylesheet: <input type="radio" ...
onclick="changeActiveStyleSheet('Default', 'Foo');">

Not tested. I'm sure the code could be improved but the general design
is correct. Opera 7+ will not support document.styleSheets collection
but there is an UI for choosing stylesheets.

GΓ©rard
--
remove blah to email me

[Back to original message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация