Js学習コード

2673 ワード

Jsを使って入力した名前を並べ替えます.
sort.js
var numnames = 0;
var names =  new Array();
function SortNames(){
	// get the name from the text field
	thename = document.theform.newname.value;
	//add the name to the array
	names[numnames] = thename;
	//Increment the counter
	numnames++;
	// sort the array
	names.sort();
	document.theform.sorted.value=names.join("
"); }
 sort.httm
<HTML>
 <HEAD>
  <TITLE> Array Sorting </TITLE>
  <script type="text/javascript" language= "javascript" src="sort.js">
  </script>
  
 </HEAD>

 <BODY>
 <p> Enter two or more name in the  textarea below.</p>
 <form name="theform">
 Name:
 <input type="text" name = "newname" size="20">
 <input type ="button" name="addname" value="Add" onclick="SortNames();"> 
 <br>
 <h2>Sorted names</h2>
 <textarea cols="60" rows="10" name ="sorted">
 the sorted names will appear here!
 </textarea>
 <form>
 </BODY>
</HTML>
 
Jsで時間を表示する
timegreet.js
 
//get the current date
now = new Date();
//split into hours minutes seconds
hours = now.getHours();
mins = now .getMinutes();
secs = now.getSeconds();

//Display the time
document.write("<h2>");
document.write("hours: "+hours+"  mins: "+mins+"  secs: "+secs);
document.write("</h2>");
// display a greeting 
document.write("<p>");
if(hours<10) document.write("Good morning!");
else if(hours>=14&&hours<=17) document.write("Good afternoon!");
else if(hours>17) document.write("Good evening!");
else document.write("Good day!");
document.write("</p>");
 
date.httm
<HTML>
 <HEAD>
  <TITLE> Control Test </TITLE>
 
 </HEAD>

 <BODY>
  <h1>Current Date and Time</h1>
  <p>
  <script language="javascript" type="text/javascript" src="timegreet.js">
  </script>

  </p>
 </BODY>
</HTML>