Help - Search - Members - Calendar
Full Version: Welcome message
HTMLHelp Forums > Programming > Client-side Scripting
Goo
Hi

I have created a html page and want to ask the user to type in his name (prompt box) when index.html page loads and then greet him. I am using JavaScript cookies, but when the user has entered his name and presses OK the greetings message is displayed on a blank page, and not on the homepage.

My code:
<html>
<head>
<script type = "text/javascript">
function message()
{
name = window.prompt("What is your Name?");
if ((name == null)||(name == ""))
{
name = "Unknown";
}
document.cookie = name;
document.write("<p>Greetings " + name + "</p>");
}
</script>
</head>
<body onload = "message()">
<img src = "logo1.jpg">
</body>
</html>

when i remove the document.write("<p>Greetings " + name + "</p>"); in the head and paste it in the body, it doesn't remember the name that was entered and only the word "Greetings" can be seen.

Code:
<body onload = "message()">
<script type = "text/javascript">
document.write("<p>Greetings " + name + "</p>");
</script>
<img src = "logo1.jpg">
</body>
</html>


Can someone please help me.
Brian Chandler
You can't use document.write after the document has loaded, because it starts writing a _new_ page. That's why the existing stuff all disappears.
Goo
What can I substitute document.write with to display my message?
Brian Chandler
You have to fiddle around with the DOM, so-called, to set the value of the appropriate bit of the document where this message is going to appear.

But really, what's the point? Why not just do this:

<p>Type your name in the following box...
<p> Greetings <input type=text size=30> !


Goo
I need to store the name in a cookie, because when the user needs to fill out a reply form, the name field must by default show the username.

I fixed the problem with the welcome message, and my name is now stored in a cookie. But how do I retrieve the cookie and use as default value in name field?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.