0

I have this pixelpost photoblog and it has a theme with a java script css switcher.

The problem is if I switch to the alternative css (black theme) and hit refresh the page is stripped of any css information. If i don't refresh everything seems to be ok though.

I can't figure it out since I'm fairly new to programming, could you give me a hand please. This is the site, and the switcher is top right corner (html and js code below):

<a href="#" onclick="setActiveStyleSheet('light'); return false;"><b>Light</b></a>/<a href="#" onclick="setActiveStyleSheet('dark'); return false;"><b>Dark</b></a>

js:

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);
Silviu
  • 25
  • 7
  • Works fine for me on Firefox 7 and Chrome 13. – Anonymous Oct 17 '11 at 22:07
  • Same here. Anyway, if there's a problem after the refresh it means there's a problem with the cookies having the wrong value. You can have a look at what's set with `alert(document.cookie)` – deviousdodo Oct 17 '11 at 22:55
  • I found the cookie named style, "content: dark" and "accessible to script: yes" so it seems the values are ok. – Silviu Oct 18 '11 at 10:51

1 Answers1

0

Looks like the style might not have loaded when the cookie style is being applied.

Try putting the

<script type="text/javascript" src="templates/simplyElegant/scripts/styleswitcher.js"></script>

at the bottom of your document, just before the tag.

Nik
  • 2,718
  • 23
  • 34
  • Worked just fine, thanks Nik. There is still a fraction of a second when you refresh the page, where there's no CSS applied, but for the time being it's ok. – Silviu Oct 18 '11 at 10:05
  • Oops, talked too soon. There's a new problem now. There are multiple scripts on one particular page and unless they are placed in a certain order the photo won't show up. styleswitcher.js jquery-1.3.2.min.js jScripts.js If I place all of them at the bottom, before the html tag I run into the same problem as before. If I place only the style switcher, the photo won't load. The trick worked on the other pages where there is only one script called. – Silviu Oct 18 '11 at 10:32