Asp + ajax as chat room 1

Source: Internet
Author: User

This is a simple example, which consists of two files: index.html and post. asp.

First, let's take a look at the code of the index.html file,

<Html> <Style> * {font: 9pt; line-height: 1.7} </style>
<Script>
Var XmlDom = document. createElement ("xml ");
XmlDom. onreadystatechange = function ()
{
If (XmlDom. readyState = "interactive ")
 {
Var ChatInfo = XmlDom. selectNodes ("// chat ");
For (var C = 0; C <ChatInfo. length; C ++)
  {
Var From = ChatInfo [C]. selectSingleNode ("sender ");
Var To = ChatInfo [C]. selectSingleNode ("recipient ");
Var Say = ChatInfo [C]. selectSingleNode ("speak ");
Var Div = document. createElement ("div ");
Div. appendChild (Text (From. text, From. getAttribute ("color"), true ));
Div. appendChild (Text (""));
Div. appendChild (Text (To. text, To. getAttribute ("color"), true ));
Div. appendChild (Text ("say :"));
Div. appendChild (Text (Say. text, Say. getAttribute ("color ")));
Var ShiJian = Text ("[" + (new Date (parseInt (ChatInfo [C]. getAttribute ("Time"), 10 ))). toLocaleString () + "]", "# abcdef ");
Secret. style. fontSize = "3mm ";
Div. appendChild (ShiJian );
Document. body. firstChild. appendChild (Div );
Div. scrollIntoView ();
  }
SetTimeout ("Show ()", 2000 );
 }
}
Function Text (text, color, clicked)
{
Var txtNode = document. createTextNode (text );
If (color)
 {
Var fontNode = document. createElement ("font ");
FontNode. color = color;
If (clicked)
  {
FontNode. style. textDecoration = "underline ";
FontNode. style. cursor = "pointer ";
FontNode. onclick = function ()
   {
Document. getElementById ("ToName"). value = text;
Document. getElementById ("ChatInput"). focus ();
   }
  }
FontNode. appendChild (txtNode );
 }
Return color? FontNode: txtNode
}
Function Show ()
{
Var SendText;
Var MyInput = document. getElementById ("ChatInput ");
If (MyInput. disabled)
 {
Var From = document. getElementById ("MyName"). value;
If (! From) From = "anonymous ";
Var To = document. getElementById ("ToName"). value;
If (! To) To = "everyone ";
Var FromColor = document. getElementById ("MyColor"). value;
Var ToColor = document. getElementById ("SendColor"). value;
Var ChatColor = document. getElementById ("SayColor"). value;
SendText = "? "+ Escape ([From, FromColor, To, ToColor, document. getElementById (" ChatInput "). value, ChatColor]. join (" \ B "));
MyInput. disabled = 0;
MyInput. value = "";
MyInput. focus ();
 }
XmlDom. load ("Post. asp" + (SendText? SendText :""));
}
Function SetColor (colorId)
{
Var clrSel = document. getElementById (colorId );
ClrSel. options. selectedIndex = parseInt (Math. random () * clrSel. options. length );
}
Onload = function ()
{
SetColor ("MyColor ");
SetColor ("SayColor ");
SetColor ("SendColor ");
If (! Name) name = prompt ("Enter your name:", "Anonymous ");
Document. getElementById ("MyName"). value = name? Name: "Anonymous ";
Document. getElementById ("ChatInput"). focus ();
Show ();
}
</Script>
</Head> <body> <div style = "overflow: auto"> </div>
<Input id = "MyName" size = "10" value = "me"> <select id = "MyColor">
<Option value = "#000000" style = "background-color: black; color: white" >#000000 </option>
<Option value = "# ff0000" style = "background-color: # ff0000"> # ff0000 </option>
<Option value = "#008000" style = "background-color: #008000" >#008000 </option>
<Option value = "# 0000ff" style = "background-color: # 0000ff"> # 0000ff </option>
<Option value = "#800080" style = "background-color: #800080" >#800080 </option>
<Option value = "#008080" style = "background-color: #008080" >#008080 </option>
<Option value = "#808000" style = "background-color: #808000" >#808000 </option>
</Select> for <input id = "ToName" size = "10" value = ""> <select id = "SendColor">
<Option value = "#000000" style = "background-color: black; color: white" >#000000 </option>
<Option value = "# ff0000" style = "background-color: # ff0000"> # ff0000 </option>
<Option value = "#008000" style = "background-color: #008000" >#008000 </option>
<Option value = "# 0000ff" style = "background-color: # 0000ff"> # 0000ff </option>
<Option value = "#800080" style = "background-color: #800080" >#800080 </option>
<Option value = "#008080" style = "background-color: #008080" >#008080 </option>
<Option value = "#808000" style = "background-color: #808000" >#808000 </option>
</Select> description: <select id = "SayColor">
<Option value = "#000000" style = "background-color: black; color: white" >#000000 </option>
<Option value = "# ff0000" style = "background-color: # ff0000"> # ff0000 </option>
<Option value = "#008000" style = "background-color: #008000" >#008000 </option>
<Option value = "# 0000ff" style = "background-color: # 0000ff"> # 0000ff </option>
<Option value = "#800080" style = "background-color: #800080" >#800080 </option>
<Option value = "#008080" style = "background-color: #008080" >#008080 </option>
<Option value = "#808000" style = "background-color: #808000" >#808000 </option>
</Select>
<Input style = "width: 100%" onkeypress = "if (value & event. keyCode = 13) disabled = 1 "id =" ChatInput "> </body>

Next page

For more information, see www.111cn.net)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.