MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

General Selection

Because of the simple general form of the if ... else statement, you can use it as a very general selection mechanism by combining if statements in sequence, as in the code below, which determines the day of the week (in English) from the numeral for that day held by a Date object. Days of the week were numbered from 0 for Sunday, through to 6 for Saturday.

<SCRIPT>
var today = new Date()
var dayNo = today.getDay()
if (dayNo == 0)
dayName = 'Sunday'
else if (dayNo == 1)
dayName = 'Monday'
else if (dayNo == 2)
dayName = 'Tuesday'
else if (dayNo == 3)
dayName = 'Wednesday'
else if (dayNo == 4)
dayName = 'Thursday'
else if (dayNo == 5)
dayName = 'Friday'
else if (dayNo == 6)
dayName = 'Saturday'
window.alert('Local day is ' + dayName)
</SCRIPT>
    

Note that there are other control statements in JavaScript. An alternative to using multiple if...else statements would be to use a switch statement. Use your knowledge of Java to experiment with these statements.

Exercise 9

Navigator objects provide properties and methods for manipulating the Web browser. A pre-declared variable called navigator can be used for this purpose. The following code writes out details of the browser:

    document.write('appCodeName = ', navigator.appCodeName, '<BR>')
    document.write('appName = ', navigator.appName, '<BR>')
    document.write('appVersion = ', navigator.appVersion, '<BR>')
    document.write('language = ', navigator.language, '<BR>')
    document.write('platform = ', navigator.platform, '<BR>')
      

For Internet Explorer 5 on Windows 98, it produces the following:

    appCodeName = Mozilla
    appName = Microsoft Internet Explorer
    appVersion = 4.0 (compatible; MSIE 5.0; Windows 98)
    language = undefined
    platform = Win32
      

For Netscape Navigator 4.51 on Windows 98, it produces the following:

    appCodeName = Mozilla
    appName = Netscape
    appVersion = 4.51 [en] (Win98; I)
    language = en
    platform = Win32
      

For Firefox 3 running on Ubuntu, it produces the following:

    appCodeName = Mozilla
    appName = Netscape
    appVersion = 5.0 (X11; en-GB)
    language = en-GB
    platform = Linux i686
      

You will notice that for Firefox some of this data is very out of date (such as the application name and version number). In this case, a more useful to use the userAgent property, which produces the following output:

	Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.0.5) Gecko/2008121622 Ubuntu/8.10 (intrepid) Firefox/3.0.5 Ubiquity/0.1.5
      

Internet Explorer 7 has the following userAgent string:

	Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
      

While this data is not as clear as the appName property, at least it does contain the browser's name.

When looking only at the appName property, we can see that the first eight characters roughly distinguishes the browser manufacturers. Strings have a method substr() for extracting a string from within another (called a substring). The method takes two arguments: the position of the first character to be extracted and the number of characters to be extracted. As in Java, String positions begin from 0. Write an if statement to test the browser make and report it using a dialogue box.

You can find a discussion of this exercise at the end of the unit.

Activity 6: Remembering an Event

Modify the code of Exercise 2 (reproduced below), in which a button changed labels after being clicked, so that clicking the new button has no effect.

  <FORM> 
  <INPUT type=button value="Click to order" 
    onClick="window.alert('Purchase confirmed. Thank you'); 
    this.value = '[purchase confirmed]'">
  </FORM>
      

You can find a discussion of this activity at the end of the unit.

Now do Review Questions 7, and 8.