I have a prompt box appear onLoad that asks for a persons name. I am trying to get the text in the page to say "Good Morning, 'username' ".
for the onLoad I want to use onLoad="checkCookie()" but it won't write the "message" with the name and will load a seperate page.
Basically what I am asking is how can I get the message + the name pulled from the cookie to display on the page?
var message = (depends on the time of day, Good Morning or Good Afternoon)
var username = (whatever the user puts in the prompt box)
<script type="text/javascript">
var time=currentTime.getHours();
var message="Good Morning!";
if (time >= 12) {
message = "Good Afternoon!";
time = time - 12;
}
if (time == 0) {
time = 12;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
{
document.write("<h3 style='margin-top:45px;text-align:center'>" + message + " " + username + "</h3>");
}
else
{
username=prompt("Please enter your name:","");
if (username!=null && username!="")
{
setCookie("username",username,365);
}
}
}
</script>