SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast Michael Randall's Avatar
    Join Date
    Nov 2003
    Location
    Raleigh, NC
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Code proofing needed

    Hi,

    I am using the following javascript to read the username that is contained in a
    cooke that I set. The "if" portion of the code works everytime. However,
    the "else" portion of the script is not being run.

    Can anyone see any obvious errors or typos with the code that may cause this problem?

    Code:
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    var username = getCookie("mike");
     if (username) {
    document.write("<span style='font-size:90%;font-weight:bold;
    text-transform:capitalize;'>" + "Welcome, " + username + "." + "</span>"
    + "<br>" + "<a href=javascript:deleteCookie('mike')>log out</a>");
    } 
     else
    {
    document.write("RWD Clients," + "<a href='login.asp'>please sign in</a>.");
    }
    -Michael

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Michael,

    It's possible that getCookie() always returns a string, so if that is true, try this...

    if (username.length) {
    }
    else {
    }

  3. #3
    SitePoint Enthusiast Michael Randall's Avatar
    Join Date
    Nov 2003
    Location
    Raleigh, NC
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mike!

    Changing (username) to (username.length) corrected the problem in IE!

    However, this did nothing for NS or Opera.

    Any more thoughts?

    -Michael

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post the getCookie() function so we can look at what it returns.

  5. #5
    SitePoint Enthusiast Michael Randall's Avatar
    Join Date
    Nov 2003
    Location
    Raleigh, NC
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    Code:
    function getCookie(name) {
      var dc = document.cookie;
      var prefix = name + "=";
      var begin = dc.indexOf("; " + prefix);
      if (begin == -1) {
        begin = dc.indexOf(prefix);
      } else
        begin += 2;
      var end = document.cookie.indexOf(";", begin);
      if (end == -1)
        end = dc.length;
      return unescape(dc.substring(begin + prefix.length, end));
    Last edited by Michael Randall; Dec 14, 2003 at 22:11.

  6. #6
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps try...
    Code:
    if (typeof(username)=='string' && username.length) {
    }
    else {
    }
    And just in case you'd like to try it, here's some different cookie functions...
    Code:
    // cookie implementations based on code from Netscape Javascript Guide
    function xSetCookie(name, value, expire, path) {
      document.cookie = name + "=" + escape(value) + ((!expire) ? "" : ("; expires=" + expire.toGMTString())) + "; path=/";
    }
    function xGetCookie(name) {
      var value=null, search=name+"=";
      if (document.cookie.length > 0) {
        var offset = document.cookie.indexOf(search);
        if (offset != -1) {
          offset += search.length;
          var end = document.cookie.indexOf(";", offset);
          if (end == -1) end = document.cookie.length;
          value = unescape(document.cookie.substring(offset, end));
        }
      }
      return value;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •