Sign in to follow this  

JavaScript Chatroom help

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm attempting on making a chatroom model for a future web-based RPG. It works, but I'm running into the same problem that happened when I was using the win32 api language. Now on javascript, when I type in my input, hit submit, it erases the whole output window and puts in the NPC's response. Like most chatrooms, you'd see past conversations if you scroll up. I need the same thing happening here somehow. The only way I can think of this being done is making a second buffer that keeps incrementing itself, yet I believe the max is 256 characters? So that wouldn't work well, if I'm thinking this straight. Please give some insight if you can. Thanks.
<html>
<head><title>JavaScript Practice</title></head>

<script language="javascript" type="text/javascript">

// String DataBase Function
function StringDataBase()
{
  // Store User's Input into the Buffer
	var Buffer = document.ChatRoom.Input.value;

	/* String Database */
	
	// Sword
	if (Buffer.match("sword")) {
		document.ChatRoom.Output.value = "Why, yes I do!"; }
	// Name
	if (Buffer.match("name")) {
		 document.ChatRoom.Output.value = "I am IKickYourAss."; }


	/* Clean Up */
	document.ChatRoom.Input.value = ""; 	 // Erase pre-written text
	document.ChatRoom.Input.focus();	  	 // Focus on Input
}

</script>

<body bgcolor="#e7e7e7">

<form name="ChatRoom" method="POST">

<!-- Output for Chatroom -->
<textarea name="Output" cols="58" rows="20" readonly="true">
</textarea>

<!-- Input for Chatroom -->
<br>
<input type="text" name="Input" size="77">

<!-- Submit Button -->
<br>
<input type="button" name="btn" value="Submit" onclick="StringDataBase()">


</form>
</body>
</html>

Share this post


Link to post
Share on other sites
Hitting a submit button will submit the form unless you tell it not to.

Either make the onclick event return false (and return the value, like)


<input type="submit" onclick="return StringDataBase()">
Also return false from StringDataBase



Or change the button into a button element with type="button" instead of submit, which won't automatically submit your form.

You might want to change the CSS so that button elements are formatted more similarly to input type=button elements, as on IE by default they are rather different.

Oh yes, and please stop using an obsolete DOM - either use the DOM0 methods OR the DOM1 methods, not obsolete IE dom "assume that a property of the same name as any named element is automatically placed on document" crap.

Cheers

Mark


Share this post


Link to post
Share on other sites
I did what you suggested, and thanks for taking the time. However, I'm still running into the same problem. When I type in something, like "do you have a sword?" the NPC responds. Then I ask him what his name is, and the previous text disapears and the NPC responds. I need the previous text still there. If it helps, please paste this into notepad to get a better idea of what's going on after typing the second question. By the way, this is a single-player automated chatroom, if you're curious. I'm still a beginner so I'm keeping it that way :)

EDIT: I assume its like appending to the end of a file then displaying its contents in the chatroom output?


<html>
<head><title>JavaScript Practice</title></head>

<script language="javascript" type="text/javascript">

// String DataBase Function
function StringDataBase()
{
// Store User's Input into the Buffer
var Buffer = document.ChatRoom.Input.value;

/* String Database */

// Sword
if (Buffer.match("sword")) {
document.ChatRoom.Output.value = "Why, yes I do!"; }
// Name
if (Buffer.match("name")) {
document.ChatRoom.Output.value = "I am IKickYourAss."; }


/* Clean Up */
document.ChatRoom.Input.value = ""; // Erase pre-written text
document.ChatRoom.Input.focus(); // Focus on Input

return false;
}


</script>

<body bgcolor="#e7e7e7">

<form name="ChatRoom" method="POST">

<!-- Output for Chatroom -->
<textarea name="Output" cols="58" rows="20" readonly="true">
</textarea>

<!-- Input for Chatroom -->
<br>
<input type="text" name="Input" size="77">

<!-- Submit Button -->
<br>
<input type="button" name="btn" value="Submit" onclick="return StringDataBase()">


</form>
</body>
</html>





[Edited by - philvaira on October 10, 2004 3:51:50 PM]

Share this post


Link to post
Share on other sites
I did it! I just appended a new string variable every time the NPC has somethng to say. This is a simple source since I'm celebrating at the moment. I'm sure this is heading towards the right way. Here's a better version right now (edit)


<script language="javascript" type="text/javascript">
var File = ""; // The main file that will append.

// String DataBase Function
function StringDataBase()
{
// Store User's Input into the Buffer
var Buffer = document.ChatRoom.Input.value;

/* String Database */

// Sword
if (Buffer.match("sword")) {
File += "Why, yes I do!"; }
// Name
if (Buffer.match("name")) {
File += "I am IKickYourAss."; }

document.ChatRoom.Output.value = File;

/* Clean Up */
document.ChatRoom.Input.value = ""; // Erase pre-written text
document.ChatRoom.Input.focus(); // Focus on Input

return false;
}

Share this post


Link to post
Share on other sites
<html>
<head><title>JavaScript Practice</title></head>

<script language="javascript" type="text/javascript">
var File = ""; // The main file that will append.

// String DataBase Function
function StringDataBase()
{
// Store User's Input into the Buffer
var Buffer = document.ChatRoom.Input.value.toLowerCase();

/* String Database */

// Sword
if (Buffer.match("sword")) {
File += "Why, yes I do!\n"; }
// Name
if (Buffer.match("name")) {
File += "I am IKickYourAss.\n"; }

document.ChatRoom.Output.value = File;

/* Clean Up */
document.ChatRoom.Input.value = ""; // Erase pre-written text
document.ChatRoom.Input.focus(); // Focus on Input

return false;
}



</script>

<body bgcolor="#e7e7e7">

<form name="ChatRoom" method="POST">

<!-- Output for Chatroom -->
<textarea name="Output" cols="58" rows="20" readonly="true">
</textarea>

<!-- Input for Chatroom -->
<br>
<input type="text" name="Input" size="77">

<!-- Submit Button -->
<br>
<input type="button" name="btn" value="Submit" onclick="return StringDataBase()">


</form>
</body>
</html>


Note the use of .toLowerCase() (so I can enter Sword) and \n for newlines.

Share this post


Link to post
Share on other sites

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this