Forum Moderators: not2easy

Message Too Old, No Replies

CSS Text Only

CSS Text Only

         

vortex2k4

9:30 am on Jan 4, 2006 (gmt 0)

10+ Year Member



Hi

I have created a seperate style sheet to my normal one which displays the site text only. I have used this code as a link so it changes to this style sheet
<a class="footer" href="index.html" onclick="changeStyle('Textonly');" accesskey="c" title="Textonly, Accesskey c">Text Only</a>

This works fine, but what i want is that every other link that is clicked when in text only opens with the text only style sheet, instaed of reverting back to the graphical one. Maybe a cookie or javascript im not sure cause im a n00b at html and css, but any help would be greatful :)

Ta

limbo

5:52 pm on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cookies is the way :)

-----------------

HTML
<a href="#" onClick="setActiveStyleSheet('textonly'); return false;">text version</a>

JScript
function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style")!= -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}

function getActiveStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style")!= -1 && a.getAttribute("title") &&!a.disabled) return a.getAttribute("title");
}
return null;
}

function getPreferredStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style")!= -1
&& a.getAttribute("rel").indexOf("alt") == -1
&& a.getAttribute("title")
) return a.getAttribute("title");
}
return null;
}

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

window.onload = function(e) {
var cookie = readCookie("style");
var title = cookie? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
}

window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

----------------

Credit to ALA and another few helpful people.

vortex2k4

3:59 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



OK thanks, do i jyst put the jscript in <script></script> tags? Im new to all this :D

Also i think what you gave me was just for two style sheets, what about a third for high contrast for example what bits would i need to replicate?

Thanks for your help

limbo

5:08 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can add as many or as few as you like

Just change the ('textonly') in above post to another stylesheet eg.

<a href="#" onClick="setActiveStyleSheet('blueocean'); return false;">blue ocean</a>

You also need to name you style sheets using the title attribute

So if you had multiple style sheets your code might look a little like (note the alternate stylesheet property)

head >>
<link href="css.css" rel="stylesheet" type="text/css" title="original">
<link rel="alternate stylesheet" type="text/css" href="blueocean.css" title="blueocean">
<link rel="alternate stylesheet" type="text/css" href="textonly.css" title="textonly">

body >>
<a href="#" onClick="setActiveStyleSheet('original'); return false;">original</a>
<a href="#" onClick="setActiveStyleSheet('blueocean'); return false;">blue ocean</a>
<a href="#" onClick="setActiveStyleSheet('textonly'); return false;">text version</a>

With regard to the Javascript - you can place it all in the head - but to keep it 'lean and mean' I'd put in an external file in the head like:

<script language="JavaScript" type="text/javascript" src="yoursite/js/styleswitcher.js"></script>

philsmears

2:50 pm on Jan 18, 2006 (gmt 0)



'I have created a seperate style sheet to my normal one which displays the site text only.'

You using display:none / visibility: hidden?