You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

styleswitcher.js 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. function getSelectionChange(e){
  2. var selectValue = e.options[e.selectedIndex].value;
  3. if (selectValue != null) setActiveStyleSheet(selectValue);
  4. }
  5. function setActiveStyleSheet(title) {
  6. var i, a, main;
  7. for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  8. if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
  9. a.disabled = true;
  10. if(a.getAttribute("title") == title) a.disabled = false;
  11. }
  12. }
  13. }
  14. function getActiveStyleSheet() {
  15. var i, a;
  16. for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  17. if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  18. }
  19. return null;
  20. }
  21. function getPreferredStyleSheet() {
  22. var i, a;
  23. for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  24. if(a.getAttribute("rel").indexOf("style") != -1
  25. && a.getAttribute("rel").indexOf("alt") == -1
  26. && a.getAttribute("title")
  27. ) return a.getAttribute("title");
  28. }
  29. return null;
  30. }
  31. function createCookie(name,value,days) {
  32. if (days) {
  33. var date = new Date();
  34. date.setTime(date.getTime()+(days*24*60*60*1000));
  35. var expires = "; expires="+date.toGMTString();
  36. }
  37. else expires = "";
  38. document.cookie = name+"="+value+expires+"; path=/";
  39. }
  40. function readCookie(name) {
  41. var nameEQ = name + "=";
  42. var ca = document.cookie.split(';');
  43. for(var i=0;i < ca.length;i++) {
  44. var c = ca[i];
  45. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  46. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  47. }
  48. return null;
  49. }
  50. window.onload = function(e) {
  51. var cookie = readCookie("style");
  52. var title = cookie ? cookie : getPreferredStyleSheet();
  53. setActiveStyleSheet(title);
  54. document.getElementById("theme-switcher").value = title;
  55. }
  56. window.onunload = function(e) {
  57. var title = getActiveStyleSheet();
  58. createCookie("style", title, 365);
  59. }