Alternative CSS Selection (IE)

This is a discussion on "Alternative CSS Selection (IE)" within the Web Page Design section. This forum, and the thread "Alternative CSS Selection (IE) are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 18th, 2005, 15:55
Reputable Member
Join Date: May 2005
Location: Sheffield
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
Alternative CSS Selection (IE)

Hello.

I am wanting to allow users to change css stylesheets on my webpage, using links.
I have been following information from: http://www.alistapart.com/articles/alternate/ but it doesn't explain how to set up the links.

I have two style sheets set up and I have added the Javascript to the page (see below)

How do I set up the <a href .. link to change the style sheet? and which variables do I update in the Javascript please?

Code: Select all
<link href="GA_styles.css" rel="stylesheet" type="text/css" title="blue standard"/>
<link href="GA_styles2.css" rel="alternate stylesheet" type="text/css" title="different"/>

<script language="JavaScript" type="text/javascript">

HTMLLinkElement.getAttribute("rel").indexOf("style") != -1
HTMLListElement.getAttribute("title")
HTMLLinkElement.getAttribute("rel").indexOf("alt") != -1

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;
     }
   }
}


</script>

</head>
Reply With Quote

  #2 (permalink)  
Old Jul 21st, 2005, 22:06
herkalees's Avatar
Highly Reputable Member
Join Date: Jul 2005
Location: Massachusetts, USA
Age: 87
Posts: 576
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to herkalees Send a message via MSN to herkalees Send a message via Yahoo to herkalees
Here's what I did on my site, and it works perfectly:

This is in a file called styleswitcher.js:
Code: Select all
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);
This is in my head tag and points to the javascript:
Code: Select all
<script type="text/javascript" src="scripts/styleswitcher.js"></script>
This is how my CSS/link tags look in my head tag:
Code: Select all
<link href="styles/webstyle.css" rel="stylesheet" type="text/css" title="webstyle" />
<link href="styles/printstyle.css" rel="alternate stylesheet" type="text/css" title="printstyle" />
Thos rel tags are necessary

And here are the actual links which allow a visitor to choose:
Code: Select all
Page Style: Web / Print
[/b]
Reply With Quote
  #3 (permalink)  
Old Jul 26th, 2005, 13:45
Reputable Member
Join Date: May 2005
Location: Sheffield
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks very much for this
Reply With Quote
  #4 (permalink)  
Old Jul 30th, 2005, 04:31
herkalees's Avatar
Highly Reputable Member
Join Date: Jul 2005
Location: Massachusetts, USA
Age: 87
Posts: 576
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to herkalees Send a message via MSN to herkalees Send a message via Yahoo to herkalees
Quote:
Originally Posted by Andy K
Thanks very much for this
You're welcome twice as much.
Reply With Quote
Reply

Tags
alternative, css, selection

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
File Selection in ASP wbeetge Starting Out 0 Oct 3rd, 2007 16:10
Forced Selection In a popup window Jamie18 JavaScript Forum 0 Jun 18th, 2007 18:58
Content Appears Based on Drop-Down Menu Selection WiseWizards JavaScript Forum 3 Aug 9th, 2005 17:48
Alertnative CSS selection using Javascript (IE) Andy K JavaScript Forum 1 Jul 20th, 2005 11:10
IE6 Text selection bug Smokie Web Page Design 9 Jun 1st, 2005 20:12


All times are GMT. The time now is 00:32.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43