Design  | SFC  |  NOS  |  Code |  Diary  | WWW |  Essay |  Cisco | [Home]
 

[Bill's Home]
[Old Page]

Quick links

Design Tips
WinSock in Visual Basic
WinSock multiserver
DHTML
CSS
Flash/XML
ASP
PHP
JavaScript
Java
Delphi
RSA
WAP
Java Socket Programming
Subnetting
RS-232
Subnetting

Source Code

Pascal
Java
C Programming
Applied PC Interfacing
WinSock client
WinSock server
FTP client
HTTP client
Java client/server
HTTP client
Subnetting in VB
RS-232 in VB
WinSock Multiserver

 

 

 

 

JavaScript - Examples

JavaScript integrates into the HTML page within the <SCRIPT LANGUAGE= "JAVASCRIPT"> and the </SCRIPT> tags. It has a similar syntax to Java, but is not as strict on the syntax of the code. For example to print the current time (HH:MM) with JavaScript:

<p>Current time is
<script language="javascript">
var dat=new Date();
document.write(dat.getHours() + ":" + dat.getMinutes());
</script>

Current time is

Outputting to browser. The document.write() applies the write() method to the document object, and outputs a processed string to the browser screen. JavaScript has much of the power of a programming language and supports arrays, such as:

<script language="javascript">
var dat=new Date(),
mon=["Jan","Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"];
document.write("Month is ");
document.write(dat.getMonth());
document.write(" and the name of the month is ");
document.write(mon[dat.getMonth()]);
</script>

Decisions. As with most programming languages, JavaScript supports decisions with the if() and switch() statements, such as the following which determines if it is morning or afternoon, and greets the user either with a "Good Morning!", or a "Good Afternoon!":

<FONT COLOR="GREEN">
<script language="javascript">
var dat=new Date();;
hr=dat.getHours();
if (hr<12)
{
document.write("Good Morning!");
}
else
{
document.write("Good Afternoon!");
}
</script>
</FONT>

Repetitive loops. JavaScript supports for() and while() loops. For example the following loops for values of fsize from 1 to 5, which are used to define the font size.

<script>
var int;
for (int=1;int<11;int++)
document.write("Value is " + int + "<BR>");
</script>
<script language="javascript">
var fsize;
for (fsize=1;fsize<=5;fsize++)
{
document.write("<br> <FONT SIZE = ");
document.write(fsize + ">");
document.write("Hello</FONT>");
}
</SCRIPT>

User functions. Even user functions can be incorporated into the page. For example, a func-tion call to a square function is as follows:

<script language="javascript">
function sqr(val)
{
return val * val
}
var value=sqr(3);
document.write("Three squared is "
+ value);
</SCRIPT>

Methods. In object-oriented design, methods are applied to object. Typical objects in JavaScript are Math (which contains mathematical properties and methods), history (which contains a lists of the visited URLs), button (which contains the object for buttons) and document (which is the container for the information on the current page). An example JavaScript object is Math. For example the following determine the square of a value:

The square root of 15 is
<script language="javascript">
document.write(Math.sqrt(15.0));
</SCRIPT>
The square root of 15 is

Another example is to determine the cosine of a value, but applying the cos() method to the Math object:

The cosine of 1.5 radians is
<script>
document.write(Math.cos(1.5))
</script>
The cosine of 1.5 radians is

The string object has many useful methods, such as toLowerCase(), which converts a string to lowercase, and toUpperCase(), which converts a string to upper, as used in the following example:

TYPEwrITer in lowercase is
<SCRIPT> document.write(("TYPEwrITer")   .toLowerCase())
document.write("and in uppercase it is");
document.write(("TYPEwrITer")
   .toUpperCase()) </SCRIPT>
TYPEwrITer in lowercase is

 

Using events. JavaScript has many useful functions, such as one to open windows of a certain size and with certain conditions, and also to respond to events, such as the onMouseDown() event when the user clicks on a graphic. The following opens a window (with the window.open() function) of width of 310 pixels and height of 200 pixels, when the user clicks on the referred graphic (message.gif). The onMouseDown() is an event which occurs when the user clicks on the graphic.

<script language="javascript">
function openWindow(theURL,winName,features) {
window.open(theURL,winName,features);
}
</script>
<img border="0" src="design_new/message.gif" width="159" height="25"
onMouseDown="openWindow('message.html','', 'toolbar=yes,
menubar=yes, scrollbars=yes, resizable=yes, width=310, height=200')"
align="left">