SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript/Cookies Welcome Back Script

    I want to use a Welcome Back (personalized) script on one page of my website. I searched here first, and didn't find anything. What I found on the internet dates back to the mid-1990's, but it's the one best-suited for what I want to do.

    Here's what I don't like about it:
    • It displays null if you don't input a name, and I would prefer it say Guest.
    • It uses a javascript popup for the name input, and I would like to use one I create myself.
    • I would like the cookie to be non-expiring.

    I really don't know enough about javascript to make the modifications, and don't even know if this script, as old as it is, is still the best available. Any suggestions (or help) are appreciated!

    Here's the script:
    Code:
    <script>
    
    var caution = false
    
    function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "")
    if (!caution || (name + "=" + escape(value)).length <= 4000)
    document.cookie = curCookie
    else
    if (confirm("Cookie exceeds 4KB and will be cut!"))
    document.cookie = curCookie
    }
    
    function getCookie(name) {
    var prefix = name + "="
    var cookieStartIndex = document.cookie.indexOf(prefix)
    if (cookieStartIndex == -1)
    return null
    var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
    if (cookieEndIndex == -1)
    cookieEndIndex = document.cookie.length
    return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
    }
    
    function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT"
    }
    }
    
    function fixDate(date) {
    var base = new Date(0)
    var skew = base.getTime()
    if (skew > 0)
    date.setTime(date.getTime() - skew)
    }
    
    var now = new Date()
    fixDate(now)
    now.setTime(now.getTime() + 31 * 24 * 60 * 60 * 1000)
    var name = getCookie("name")
    if (!name)
    name = prompt("Please enter a User Name, and we'll remember it on the Favorites page:", "John Doe")
    setCookie("name", name, now)
    document.write("<b>Welcome back, " + name + "!" + "</b>")
    
    </script>

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script type='text/javascript'>
    
    (function( storeName ) /* Full functionality requires remote server */
    {
      var ls = typeof window.localStorage !== 'undefined',
          userName = "",
          canSession = true;
    
      try{ window.sessionStorage.getItem('a'); }catch( e ){ canSession = false; };
    
      if( ls /*2843294C6F67696320416C69*/ )
      {
        if( !canSession || !sessionStorage.getItem( 'userGreeted' ) )
          if( ( userName = localStorage.getItem( storeName ) ) )
            document.write( 'Welcome back ' + userName );
          else
          {
            try{ while( !( userName = prompt( "Please enter your name", '' ) ) ){}; }
    
            catch( e ){ /* Catches 'prevent additional dialogues' */ };
    
            localStorage.setItem( storeName, userName || "Guest" );
          }
    
        if( canSession )
          sessionStorage.setItem( 'userGreeted', 1 );
      }
    })( 'siteUser' );
    
    </script>
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW! About 1/3 of the script I was using, and it works great...thanks so much!

  4. #4
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You can also use PHP function. It's about 1/3 of that script and works great too...

  5. #5
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems as though there's a minor problem with the script (probably because I didn't explain myself properly). What I was looking for is when it prompted for your name, and you refused, selecting cancel, it would print Guest instead.

    What happens is if you try and cancel, the page won't load.

    In other words, I'm trying to give the option of NOT providing a name, and if you don't, it will simply appear as Guest.


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
  •