javascript: first steps of chatbox

Keeping with tradition of these javascript experiments, this is set up just like all the others:   My html, and js file are in my desktop folder, and I double click my html file to see it run in the default browser. 




I want to make my whispering wall's text display in a chatbox, so the user can see what they typed, and prior conversations.
As of now, the whispering wall will take input, process it through the NLTK and python code, and return a response to screen.
But none of the input is kept (or previous Wiwa responses) once the html is refreshed with the retrieved response from my scripts.

So the first thing to do is see how to create this scrolling text box.
This post only covers the experiment of creating/exploring how to display a chatbox.
( I am hoping to create the second part to this experiment today which will cover placing a script line from a file, into the chat box.)


Just a few items I had to look up for the HTML javascript code:
"clearing input field":  https://www.w3schools.com
"keep scroll at bottom": https://stackoverflow.com
"add a breakline to childNode:" https://stackoverflow.com

break it, modify it, explore, create, hydrate.
The code:

                                             <!-- HTML -->

<!DOCTYPE html>

<html>
<head>
<title>Exercise 4 chatbox</title>
 <script type="text/javascript" src="/home/nellie/Desktop/browser test/ex4.js"></script>
  </head>
  <!-- #91cac5 == greyish teal-->
  <!-- #413647 == dark grey violet -->
  <!-- #edc488 == Peach -->
  <body id="Main_Body" style="background: #413647; font-color: ">
    <br>
    <br>


  <h1 style="color: #edc488;"> This is the browser opened page </h1>
  <br>


<h3> CHAT </h3>
  <div style="border:solid #d5500d; background: #EDC488; height: 100px; width: 50%; overflow: auto;" id="chatbox">
  </div>
  <h1> INPUT: </h1>
  <textarea id="user_input" type="text" onfocus="this.value=''" maxlength="120" style="height: 20px; width: 400px; border: solid #d5500d;">
  </textarea>
  <div style="display: inline-block; margin-left: 25%; margin-right: 25%;">
    <button id="mybutton" onclick="MyFunction()">Test the code</button>




  </body>
  </html>






                                          // JavaScript //

//javascript for ex4 chatbox
function scroll_to_bottom(){
  var objDiv = document.getElementById("chatbox");
  objDiv.scrollTop = objDiv.scrollHeight;
}

function MyFunction(){
  element = document.getElementById("chatbox");
  linebreak = document.createElement("br");
  user_input = document.getElementById("user_input").value;
  element.append(user_input);
  element.appendChild(linebreak);
  scroll_to_bottom();
}





 



Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame