[SOLVED] Loading a different CSS file using javascript

This is a discussion on "[SOLVED] Loading a different CSS file using javascript" within the JavaScript Forum section. This forum, and the thread "[SOLVED] Loading a different CSS file using javascript are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old May 17th, 2008, 17:59
New Member
Join Date: May 2008
Location: London, UK
Age: 28
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Loading a different CSS file using javascript

Hello

Hopefully this is the right section for this problem. How can i load a different .css file by clicking a button? I have 3 css files. I can load the first one just fine through the <head> tags but how can i load the other 2 when i click two buttons on my html page?

I have been searching for example but i couldnt find any. I will appreciate any help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old May 17th, 2008, 19:07
welshstew's Avatar
Site Admin

SuperMember
Join Date: May 2007
Location: inside the outside
Posts: 1,691
Blog Entries: 14
Thanks: 3
Thanked 32 Times in 30 Posts
Re: Loading a different CSS file using javascript

you just need a style switcher...

add the following to a javascript file and call it something logical (styleswitch for instance)
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);
then add it to your page in the head section
Code: Select all
<head>
<script type="text/javascript" src="styleswitch.js"></script>
</head>
you then need to add you stylesheets:
Code: Select all
<head>
<link rel="stylesheet" type="text/css" href="default.css" title="default" />
<link rel="alternate stylesheet" type="text/css" href="one.css" title="one" />
<link rel="alternate stylesheet" type="text/css" href="two.css" title="two" />
<script type="text/javascript" src="styleswitcher.js"></script>
</head>
then add the options to your content to switch the styles
Code: Select all
<body>
<a name="top"></a>
<a href="#top" onclick="setActiveStyleSheet('default'); return true;">
default style</a>

<a href="#top" onclick="setActiveStyleSheet('one'); return true;">
style one</a>

<a href="#top" onclick="setActiveStyleSheet('two'); return true;">
style two</a>
</body>
give that a go and let us know how you get on.
__________________
WelshStew Site Admin
If you think I've helped, click the "Thanks"
tierney rides tboard - uk site | xtreme wales - extreme clothing
WebForumz - facebook | LinkedIn
Last Blog Entry: Phorm approved for UK rollout (Sep 17th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old May 18th, 2008, 05:36
New Member
Join Date: May 2008
Location: London, UK
Age: 28
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Loading a different CSS file using javascript

Thank you welshstew. That worked like a charm.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old May 18th, 2008, 08:39
welshstew's Avatar
Site Admin

SuperMember
Join Date: May 2007
Location: inside the outside
Posts: 1,691
Blog Entries: 14
Thanks: 3
Thanked 32 Times in 30 Posts
Re: Loading a different CSS file using javascript

glad to have helped
__________________
WelshStew Site Admin
If you think I've helped, click the "Thanks"
tierney rides tboard - uk site | xtreme wales - extreme clothing
WebForumz - facebook | LinkedIn
Last Blog Entry: Phorm approved for UK rollout (Sep 17th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old May 18th, 2008, 12:36
New Member
Join Date: May 2008
Location: London, UK
Age: 28
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Loading a different CSS file using javascript

Hello welshstew

How can i write that javascript so that it works even if javascript is disabled?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
css, javascript

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
flash file background loading strebes Flash & Multimedia Forum 2 Aug 30th, 2007 20:44
Why is my Flash File not loading? BlackReef Flash & Multimedia Forum 2 Jul 17th, 2007 19:04
load javascript file VanHype JavaScript Forum 7 Apr 27th, 2007 15:00
swf not loading on html file Christom Flash & Multimedia Forum 2 Mar 31st, 2005 09:51
Loading a mp3 and wav file... courtjester Flash & Multimedia Forum 4 Jun 14th, 2004 04:18


All times are GMT. The time now is 05:47.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization 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