Jump to content
Larry Ullman's Book Forums

benjamin.morgan

Members
  • Posts

    38
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by benjamin.morgan

  1. Solved. Thanks HartleySan. It was the window.onload causing the problem as well as it being initially set in javascript. I had to choose no wrap instead of onload on the side. I've never had that problem before!

    http://jsfiddle.net/98d6X/3/

     

    Another problem that came up is if I use

    var mov = document.getElementById('move');
    
    

     

    If i use mov.style.left it doesn't work anymore. I am putting the var mov as a global variable outside of the functions. It will only work if I create the same variable in both functions.

  2. The console error in mine and Larry's code is

     

     

    TypeError: target is undefined

    COOKIE.setCookie('theme', target.id, expire);

     

    Here is my cookie code

     

    var COOKIE = {
    setCookie: function(name, value, expire) {
     'use strict';
     var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);
     str += ';expires=' + expire.toGMTString();
     document.cookie = str;
    },
    getCookie: function(name) {
     'use strict';
     var len = name.length;
     var cookies = document.cookie.split(';');
     for (var i = 0, count = cookies.length; i < count; i++) {
      var value = (cookies[i].slice(0,1) == ' ') ? cookies[i].slice(1) : cookies[i];
      value = decodeURIComponent(value);
      if (value.slice(0,len) == name) {
       return cookies[i].split('=')[1];
      }
     }
     return false;
    }
    };
    

  3. Okay, I have the code working but I don't under stand it at all.

     

    Here is what I am having a problem with. I understand the setTheme function. What starts confusing me is on the window.onload. It calls the setThemeCookie function but what goes in the e variable in the function?

     

    Also it says "Close your browser window, or even quit the browser, and then reopen the

    page to see your theme selection retained." When I close the browser or reopen the page or refresh it, it always goes back to the blank theme.

     

    function setTheme(theme) {
    'use strict';
    var css = null;
    if (document.getElementById('cssTheme')) {
    css = document.getElementById('cssTheme');
    css.href = 'css/' + theme + '.css'
    } else {
    css = document.createElement('link');
    css.rel = 'stylesheet';
    css.href = 'css/' + theme + '.css';
    css.id = 'cssTheme';
    document.head.appendChild(css);
    }
    }
    
    function setThemeCookie(e) {
    'use strict';
    e = e || window.event;
    if (e.preventDefault) {
    e.preventDefault();
    } else {
    e.returnValue = false;
    }
    var target = e.target || e.srcElement;
    
    var expire = new Date();
    expire.setDate(expire.getDate() + 7);
    COOKIE.setCookie('theme', target.id, expire);
    setTheme(target.id);
    return false;
    }
    window.onload = function() {
    'use strict';
    document.getElementById('aTheme').onclick = setThemeCookie;
    document.getElementById('bTheme').onclick = setThemeCookie;
    var theme = COOKIE.getCookie('theme');
    if (theme) {
    setThemeCookie(theme);
    }
    };
    
    

     

    HTML

     

    <p>Choose a theme: <a href="somepage.php?theme=a" id="aTheme">A Theme</a> || <a href="somepage.php?theme=b" id="bTheme">B Theme</a></p>
    

×
×
  • Create New...