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:

<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>
<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>

WOW! About 1/3 of the script I was using, and it works great…thanks so much!

You can also use PHP function. It’s about 1/3 of that script and works great too…

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.