Archive for April, 2009

Change Web Browser Title with Javascript

Every web browser has a title at the top of the page and is specified in HTML. By default, the the title tag is located in the header of the HTML. Despite the default in HTML, it is possible to change the default title name with javascript. Below is a the basic snippet of javascript code that changes the current Web Browser title to “The Web Page title has been changed“.

document.title = "The Web Page title has been changed"

In the live demo below, the above snippet is inserted into the onclick event of an html input button. Before clicking the button below, note the current browser window title.

After clicking the button above, you will notice that the web browser title has been changed to The Web Page title has now been changed. Below is the actual code on how the button was created.

<input type="button" onclick="document.title='The Web Page title has been changed'" />

Directory Disk Space in Linux

Running Linux distribution Centos 4.6, I need to check the disk space available in a directory. For this, I use the command df, which reports the file system disk space usage.

df
This command returns the file system, the mount directory, total disk space, used disk space, and available disk space (in 1-K blocks) based on currently mounted directories.

df -h
This command returns the file system, the mount directory, total disk space, used disk space, and available disk space (in MB and GB blocks) based on currently mounted directories.

df /to/directory
This command returns the file system, the mount directory, total disk space, used disk space, and available disk space (in 1-K blocks) based the stated /to/directory.

df /to/directory -h
This command returns the file system, the mount directory, total disk space, used disk space, and available disk space (in MB and GB blocks) based the stated /to/directory.

Calculate Average in PHP Example

To calculate the average of numbers in PHP, we first need to add up all the item, then divide by the total number of items. In our example, we will add up the eight numbers 2, 4, 5, 3, 2, 9, 11, and 9.

First, the code example:

<?php
  $totalRatings = 2+4+5+3+2+9+11+9;
  $totalVotes = 8;
  $average = $totalRatings / $totalVotes;
  echo "Average: $average";
?>

Next, the output of the above PHP example will print the following to a web browser.

Average: 5.625

Since PHP is not strongly typed, the line of PHP code that performs the dividing of $totalRatings and $totalVotes (both integers) will correctly result in a double value.