Change CSS with JavaScript (JavaScript)


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2. * Stylesheet toggle variation on styleswitch stylesheet switcher.
  3. * Built on jQuery.
  4. * Under an CC Attribution, Share Alike License.
  5. * By Kelvin Luck ( http://www.kelvinluck.com/ )
  6. **/
  7.  
  8. (function($)
  9. {
  10. // Local vars for toggle
  11. var availableStylesheets = [];
  12. var activeStylesheetIndex = 0;
  13.  
  14. // To loop through available stylesheets
  15. $.stylesheetToggle = function()
  16. {
  17. activeStylesheetIndex ++;
  18. activeStylesheetIndex %= availableStylesheets.length;
  19. $.estableceEstilo(availableStylesheets[activeStylesheetIndex]);
  20. };
  21.  
  22. // To switch to a specific named stylesheet
  23. $.estableceEstilo = function(styleName)
  24. {
  25. $('link[@rel*=style][title]').each(
  26. function(i)
  27. {
  28. this.disabled = true;
  29. if (this.getAttribute('title') == styleName) {
  30. this.disabled = false;
  31. activeStylesheetIndex = i;
  32. }
  33. }
  34. );
  35. createCookie('style', styleName, 365);
  36. };
  37.  
  38. // To initialise the stylesheet with it's
  39. $.estiloInicial = function(defaultStyle)
  40. {
  41. $('link[rel*=style][title]').each(
  42. function(i)
  43. {
  44. availableStylesheets.push(this.getAttribute('title'));
  45. }
  46. );
  47. var c = readCookie('style');
  48. if (c) {
  49. $.estableceEstilo(c);
  50. }
  51. else {
  52. $.estableceEstilo(defaultStyle);
  53. }
  54. };
  55. }
  56. )(jQuery);
  57.  
  58. // cookie functions http://www.quirksmode.org/js/cookies.html
  59. function createCookie(name,value,days)
  60. {
  61. if (days)
  62. {
  63. var date = new Date();
  64. date.setTime(date.getTime()+(days*24*60*60*1000));
  65. var expires = "; expires="+date.toGMTString();
  66. }
  67. else var expires = "";
  68. document.cookie = name+"="+value+expires+"; path=/";
  69. }
  70. function readCookie(name)
  71. {
  72. var nameEQ = name + "=";
  73. var ca = document.cookie.split(';');
  74. for(var i=0;i < ca.length;i++)
  75. {
  76. var c = ca[i];
  77. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  78. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  79. }
  80. return null;
  81. }
  82. function eraseCookie(name)
  83. {
  84. createCookie(name,"",-1);
  85. }
  86. // /cookie functions

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.