Simple Java Scripts

bulletIntroduction

Scripting is based on short lines of code that can do a lot of work for us. This page recalls several simple JavaScript applications. Their understanding relies on the fact that on opening, the Internet browser creates a window object with values determined by the default browser settings. Certain defaults, such as the URL of the web page opened in the browser, can be modified using the Preferences, Internet Options menus etc. Others are determined by the computer's settings. The most frequently used properties of the window object are the location, alert, open and document.

bulletBrowser Redirection

The location property is well suited for redirection of the browser to another location. An example of the redirection code follows. It utilizes the fact that the JavaScript code is executed before the browser is visible to the client. Notice that the dot in the script used to call the location as the window property. The noscript element is reached only if the client's JavaScript is disabled, in which case the HTML comment embracing the write line kicks in and prevents the browser from displaying the JavaScript code to the client. The user is then forced to click the link to get redirected.


<html>
   <head>
      <script type="text/javascript">
         <!--
         window.location="http://www.foo.com";
         //-->
      </script>
      <noscript>
         The page www.oldfoo.com moved to
         <a href="http://www.foo.com">foo.com </a>. 
         Follow the link to get there.
      </noscript>
   </head> 
   <body> </body>
</html>  
		

bulletAlert Dialog Window

The alert dialog window proves to be useful in debugging of the java code. It can be placed anywhere in the code to pop up and show the value of a variable, its type etc. It must be called as a function with a string argument. For example, the following code defines a string variable and shows content of the variable in the dialog box:


var str="Hello, how are you?";
window.alert(str);
		

bulletOpening a New Window

The general form of the open function is open(URL, name, "width=nbr1, height=nbr2"). It opens a new window of width and height determined by nbr1 and nbr2. Not all arguments are required though. For example, the command below opens a new browser window with the EmInf page.


<a href="#" onclick="window.open('http://eminf.com');"> 
   EmInf.com 
</a>
		

bulletDocument Object

The document object keeps, besides the already mentioned write function, track of all objects displayed in the browser. Objects such as the document title, hyperlinks, images, form components etc. Notice, how the following code snippet uses the title property to retrieve the browser window title.


<html>
   <head>
   <title>Window Title</title>
   <script type="text/javascript">
      window.document.write("The \
         title you see in your browser's title bar\
         is "+window.document.title);
   </script>
   </head> 
   <body></body>
</html>  
		

It is to emphasize, that the browser processes the HTML code sequentially, which means, if the title element was placed after the script element, which is perfectly legal, its inner content would not be displayed, because no title has been set yet and the default is an empty string.

bulletReferences

David Barron: Scripting Languages. Wiley 2000

The ECMAScript scripting language. World Wide Web Consortium 1999