I’m trying to write a piece of text over an existing web page based on whether or not the cookie is set. The cookie part works fine, but the document.write causes the text to appear on a new blank page.
<script type="text/javascript">
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!="")
{
alert("Welcome again " + username);
}
else
{
username=prompt("Please enter your name:","");
document.write ("hello");
if (username!=null && username!="")
{
setCookie("username",username,7);
}
}
}
</script>
<body onload="checkCookie()">
</body>
I guess I need to use a z-index somehow, but can’t seem to get it to work. If someone could explain how I get my ‘document.write’ line to appea on top of the stuff that already exists on the page, I would be really grateful. All of this is happening on a page on a joomla site, and the code is in the php file for one of the modules. Thanks all.
Rob.
Assign the body an ID, such as ‘body’, then use the following:
W3 Schools has a good example of it.