<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://conwiki.graveyardofairships.com/index.php?action=history&amp;feed=atom&amp;title=Use_JS_to_Change_Stylesheets</id>
	<title>Use JS to Change Stylesheets - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://conwiki.graveyardofairships.com/index.php?action=history&amp;feed=atom&amp;title=Use_JS_to_Change_Stylesheets"/>
	<link rel="alternate" type="text/html" href="https://conwiki.graveyardofairships.com/index.php?title=Use_JS_to_Change_Stylesheets&amp;action=history"/>
	<updated>2026-04-10T21:50:11Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>https://conwiki.graveyardofairships.com/index.php?title=Use_JS_to_Change_Stylesheets&amp;diff=46&amp;oldid=prev</id>
		<title>R51: Created page with &quot;Tiddles used this script for a JS style switcher in demo versions of Skyline/Skyglade.  &lt;pre&gt;   /*********************************************************************************...&quot;</title>
		<link rel="alternate" type="text/html" href="https://conwiki.graveyardofairships.com/index.php?title=Use_JS_to_Change_Stylesheets&amp;diff=46&amp;oldid=prev"/>
		<updated>2012-03-04T20:48:08Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Tiddles used this script for a JS style switcher in demo versions of Skyline/Skyglade.  &amp;lt;pre&amp;gt;   /*********************************************************************************...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Tiddles used this script for a JS style switcher in demo versions of Skyline/Skyglade.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/***********************************************************************************************&lt;br /&gt;
                             Script to swap between stylesheets&lt;br /&gt;
  Written by Mark Wilton-Jones, 05/12/2002. v2.2.1 updated 14/03/2006 for dynamic stylesheets&lt;br /&gt;
************************************************************************************************&lt;br /&gt;
&lt;br /&gt;
Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script&lt;br /&gt;
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use&lt;br /&gt;
&lt;br /&gt;
To set up the page in the first place:&lt;br /&gt;
&lt;br /&gt;
	Inbetween the &amp;lt;head&amp;gt; tags, put:&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;script src=&amp;quot;PATH TO SCRIPT/swapstyle.js&amp;quot; type=&amp;quot;text/javascript&amp;quot; language=&amp;quot;javascript1.2&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	Also between the head tags, put your stylesheets, best done as external links, but you can use&lt;br /&gt;
	&amp;lt;style ...&amp;gt; tags as well.&lt;br /&gt;
&lt;br /&gt;
		Stylesheets cannot be switched if they have no title attribute and will be used at all times:&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; href=&amp;quot;all.css&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		Stylesheets will be on by default if they have a title attribute and their rel attribute is set to &amp;#039;stylesheet&amp;#039;.&lt;br /&gt;
		Most browsers will only allow one of these to be defined (or several sharing the same title):&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; href=&amp;quot;default.css&amp;quot; title=&amp;quot;Default&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		Stylesheets will be off by default if they have a title attribute and their rel attribute is set to &amp;#039;alternate stylesheet&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;link rel=&amp;quot;alternate stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; href=&amp;quot;contrast.css&amp;quot; title=&amp;quot;High Contrast&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;link rel=&amp;quot;alternate stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; href=&amp;quot;bigFont.css&amp;quot; title=&amp;quot;Big Font&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To swap between stylesheets:&lt;br /&gt;
&lt;br /&gt;
	changeStyle();                           //switches off all stylesheets that have title attributes&lt;br /&gt;
	changeStyle(&amp;#039;Default&amp;#039;);                  //switches off all stylesheets that have title attributes that do not match &amp;#039;Default&amp;#039;&lt;br /&gt;
	changeStyle(&amp;#039;High Contrast&amp;#039;);            //switches off all stylesheets that have title attributes that do not match &amp;#039;High Contrast&amp;#039;&lt;br /&gt;
	changeStyle(&amp;#039;Big Font&amp;#039;);                 //switches off all stylesheets that have title attributes that do not match &amp;#039;Big Font&amp;#039;&lt;br /&gt;
	changeStyle(&amp;#039;High Contrast&amp;#039;,&amp;#039;Big Font&amp;#039;); //switches off all stylesheets that have title attributes that do not match &amp;#039;High Contrast&amp;#039; or &amp;#039;Big Font&amp;#039;&lt;br /&gt;
&lt;br /&gt;
	Opera 7+ and Mozilla also allow users to switch between stylesheets using the view menu (only one at a time though ...)&lt;br /&gt;
&lt;br /&gt;
To make the script remember the user&amp;#039;s choice of stylesheets, for example to use on more than one page or if they reload&lt;br /&gt;
- includes stylesheets chosen using the view menu in Gecko - it will only attempt to store a cookie if they actually&lt;br /&gt;
changed something:&lt;br /&gt;
&lt;br /&gt;
	In these examples, I call the cookie used to store the choice &amp;#039;styleTestStore&amp;#039;. You could use any name you like.&lt;br /&gt;
&lt;br /&gt;
	To remember only until the browser window is closed:&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;body onload=&amp;quot;useStyleAgain(&amp;#039;styleTestStore&amp;#039;);&amp;quot; onunload=&amp;quot;rememberStyle(&amp;#039;styleTestStore&amp;#039;);&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	To remember for 10 days (for example):&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;body onload=&amp;quot;useStyleAgain(&amp;#039;styleTestStore&amp;#039;);&amp;quot; onunload=&amp;quot;rememberStyle(&amp;#039;styleTestStore&amp;#039;,10);&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that some browsers (most notably Opera) do not fire the onunload event when the page is&lt;br /&gt;
reloaded, and will only fire it when the user clicks a link or submits a form. If you need the&lt;br /&gt;
style preference to be stored even when reloading, you should call rememberStyle immediately&lt;br /&gt;
after you call changeStyle.&lt;br /&gt;
&lt;br /&gt;
If you are going to provide users with a mechanism to change stylesheets, you may want to check&lt;br /&gt;
if the browser will allow you to change stylsheets first. Use:&lt;br /&gt;
&lt;br /&gt;
	if( document.styleSheets || ( window.opera &amp;amp;&amp;amp; document.childNodes ) || ( window.ScriptEngine &amp;amp;&amp;amp; ScriptEngine().indexOf(&amp;#039;InScript&amp;#039;) + 1 &amp;amp;&amp;amp; document.createElement ) ) {&lt;br /&gt;
		document.write(&amp;#039;Something that allows them to choose stylesheets&amp;#039;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
It&amp;#039;s not perfect, because it will also appear in ICEbrowser, which makes a mess when it tries to&lt;br /&gt;
change to an alternate stylesheet. If you want, you can use&lt;br /&gt;
	if( ( document.styleSheets || ( window.opera &amp;amp;&amp;amp; document.childNodes ) || ( window.ScriptEngine &amp;amp;&amp;amp; ScriptEngine().indexOf(&amp;#039;InScript&amp;#039;) + 1 &amp;amp;&amp;amp; document.createElement ) ) &amp;amp;&amp;amp; !navigator.__ice_version ) {&lt;br /&gt;
but you should then update that if ICE is updated to make it work properly.&lt;br /&gt;
________________________________________________________________________________________________*/&lt;br /&gt;
&lt;br /&gt;
function getAllSheets() {&lt;br /&gt;
	if( !window.ScriptEngine &amp;amp;&amp;amp; navigator.__ice_version ) { return document.styleSheets; }&lt;br /&gt;
	if( document.getElementsByTagName ) { var Lt = document.getElementsByTagName(&amp;#039;link&amp;#039;), St = document.getElementsByTagName(&amp;#039;style&amp;#039;);&lt;br /&gt;
	} else if( document.styleSheets &amp;amp;&amp;amp; document.all ) { var Lt = document.all.tags(&amp;#039;LINK&amp;#039;), St = document.all.tags(&amp;#039;STYLE&amp;#039;);&lt;br /&gt;
	} else { return []; } for( var x = 0, os = []; Lt[x]; x++ ) {&lt;br /&gt;
		var rel = Lt[x].rel ? Lt[x].rel : Lt[x].getAttribute ? Lt[x].getAttribute(&amp;#039;rel&amp;#039;) : &amp;#039;&amp;#039;;&lt;br /&gt;
		if( typeof( rel ) == &amp;#039;string&amp;#039; &amp;amp;&amp;amp; rel.toLowerCase().indexOf(&amp;#039;style&amp;#039;) + 1 ) { os[os.length] = Lt[x]; }&lt;br /&gt;
	} for( var x = 0; St[x]; x++ ) { os[os.length] = St[x]; } return os;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function changeStyle() {&lt;br /&gt;
	window.userHasChosen = window.MWJss;&lt;br /&gt;
	for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) {&lt;br /&gt;
		if( ss[x].title ) { ss[x].disabled = true; }&lt;br /&gt;
		for( var y = 0; y &amp;lt; arguments.length; y++ ) { if( ss[x].title == arguments[y] ) { ss[x].disabled = false; } }&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function rememberStyle( cookieName, cookieLife ) {&lt;br /&gt;
	for( var viewUsed = false, ss = getAllSheets(), x = 0; window.MWJss &amp;amp;&amp;amp; MWJss[x] &amp;amp;&amp;amp; ss[x]; x++ ) { if( ss[x].disabled != MWJss[x] ) { viewUsed = true; break; } }&lt;br /&gt;
	if( !window.userHasChosen &amp;amp;&amp;amp; !viewUsed ) { return; }&lt;br /&gt;
	for( var x = 0, outLine = &amp;#039;&amp;#039;, doneYet = []; ss[x]; x++ ) {&lt;br /&gt;
		if( ss[x].title &amp;amp;&amp;amp; ss[x].disabled == false &amp;amp;&amp;amp; !doneYet[ss[x].title] ) { doneYet[ss[x].title] = true; outLine += ( outLine ? &amp;#039; MWJ &amp;#039; : &amp;#039;&amp;#039; ) + escape( ss[x].title ); }&lt;br /&gt;
	}&lt;br /&gt;
	if( ss.length ) { document.cookie = escape( cookieName ) + &amp;#039;=&amp;#039; + escape( outLine ) + ( cookieLife ? &amp;#039;;expires=&amp;#039; + new Date( ( new Date() ).getTime() + ( cookieLife * 86400000 ) ).toGMTString() : &amp;#039;&amp;#039; ) + &amp;#039;;path=/&amp;#039;; }&lt;br /&gt;
}&lt;br /&gt;
function useStyleAgain( cookieName ) {&lt;br /&gt;
	for( var x = 0; x &amp;lt; document.cookie.split( &amp;quot;; &amp;quot; ).length; x++ ) {&lt;br /&gt;
		var oneCookie = document.cookie.split( &amp;quot;; &amp;quot; )[x].split( &amp;quot;=&amp;quot; );&lt;br /&gt;
		if( oneCookie[0] == escape( cookieName ) ) {&lt;br /&gt;
			var styleStrings = unescape( oneCookie[1] ).split( &amp;quot; MWJ &amp;quot; );&lt;br /&gt;
			for( var y = 0, funcStr = &amp;#039;&amp;#039;; styleStrings[y]; y++ ) { funcStr += ( y ? &amp;#039;,&amp;#039; : &amp;#039;&amp;#039; ) + &amp;#039;unescape( styleStrings[&amp;#039; + y + &amp;#039;] )&amp;#039;; }&lt;br /&gt;
			eval( &amp;#039;changeStyle(&amp;#039; + funcStr + &amp;#039;);&amp;#039; ); break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	window.MWJss = []; for( var ss = getAllSheets(), x = 0; ss[x]; x++ ) { MWJss[x] = ss[x].disabled; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>R51</name></author>
	</entry>
</feed>