View Single Post
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: save and load font and background setting using cookies

 
0
  #2
Jan 15th, 2009
Hi there! Heres the conclusion to your 1st query regarding changing the looks of your page.
This script wil load specific themes' using different style sheets depending on the selection being provided by the user and of course with the help of cookies. It's a compact script, hope youll enjoy this...

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <?xml-stylesheet type="text/css" href="#inline" media="all"?>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  9. <meta http-equiv="Content-Style-Type" content="text/css" />
  10. <title>Essential's Demo</title>
  11. <style id="inline" type="text/css" media="all">
  12. body { background-color: #00F; color: #FFF; }
  13. </style>
  14.  
  15. <script type="text/javascript">
  16. /* (c) Copyright 2008-2009 Essential Works. All rights reserved. ~
  17.  
  18. Permission granted for the use of this script, as long as all credits ramain as is.
  19.  
  20. This code has been tested and before released and working 100% with OP8 and IE */
  21.  
  22. function setCookie(name, value, expires, path, domain, secure) {
  23. expires = expires ? '; expires=' + expires.toGMTString() : '';
  24. path = path ? '; path=' + path : '';
  25. domain = domain ? '; domain=' + domain : '';
  26. secure = secure ? '; secure' : '';
  27. var cookies = name + '=' + escape(value) + expires + path + domain + secure;
  28. document.cookie = cookies;
  29. }
  30.  
  31. function getCookie(name) {
  32. var thisName = name + '=';
  33. var firstIndex = document.cookie.indexOf(thisName);
  34. if (firstIndex == -1) { return null; }
  35. var lastIndex = document.cookie.indexOf(';',firstIndex + thisName.length);
  36. if (lastIndex == -1) { lastIndex = document.cookie.length;
  37. return unescape(document.cookie.substring(firstIndex + thisName.length, lastIndex)); }
  38. }
  39.  
  40. function deleteCookie(name, path, domain) {
  41. if (getCookie(name)) {
  42. document.cookie = name +"=" + path + domain +
  43. "; expires=Thu, 01-Jan-10 00:00:01 GMT"
  44. } }
  45. function cookieDate(date) {
  46. var thisDate = new Date(0);
  47. var refDate = thisDate.getTime();
  48.  
  49. if ( refDate > 0 ) { date.setTime(date.getTime() - refDate); }
  50. }
  51.  
  52. function saved() {
  53. var now = new Date();
  54. cookieDate(now)
  55.  
  56. var lists = document.all ? document.all.frm : document.forms['frm'];
  57.  
  58. now.setTime(now.getTime() + (31 * 24 * 60 * 60 * 1000));
  59. var name = getCookie('name');
  60. name = lists.lst.options[lists.lst.selectedIndex].value;
  61. setCookie('name', name, now);
  62. }
  63. function themes() {
  64.  
  65. var loadStyle = document.all ? document.all.inline : document.getElementById('inline');
  66.  
  67. /* There are alot of ways to this, just keep those mind workin. Here's the basic of it and i hope you appreciate this. */
  68.  
  69. var form = document.all ? document.all.frm : document.forms['frm'];
  70.  
  71. /* Ok, let's say (for example you have this two style sheets, named as "black.css" and the other one is "white.css")
  72.   Now all you need to do is to - apply specific rules on this css documents, which is the "black.css" and "white.css"
  73. */
  74.  
  75. switch(getCookie(name)) {
  76. case 'black.css' : form.lst.selectedIndex = 1; break;
  77. case 'white.css' : form.lst.selectedIndex = 2; break;
  78. default : alert('Please select a theme on the list!'); }
  79. loadStyle.innerHTML = '@import "' + getCookie(name) + '";'; }
  80.  
  81. if (window.addEventListener)
  82. window.addEventListener('load',themes,false);
  83. else if (window.attachEvent)
  84. window.attachEvent('onload',themes);
  85. else if (document.getElementById)
  86. window.onload = theme;
  87.  
  88. </script>
  89. </head>
  90. <body>
  91. <h1>Essential Works</h1>
  92. <form id="frm" action="javascript:void(0);">
  93. <div>
  94. <select name="lst" size="1" onchange="saved();">
  95. <option value="none" selected="selected">Select Your Theme</option>
  96.  
  97. <!-- Path or filename of your CSS must be declared from the value of this OPTION ~
  98. black.css is the default -->
  99.  
  100. <option value="black.css">Black Theme</option>
  101. <!-- Same process must be applied on this OPTION ~
  102. default is white.css -->
  103. <option value="white.css">White Theme</option>
  104. </select>
  105. </div>
  106. </form>
  107. </body>
  108. </html>
Reply With Quote