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>