home

articles

broadband

browsers

fraud001

javascript001

javascript002

javascript003

links

mysql01

mysql02

mysql03

mysql04

php001

php002

php003

problem001

spyware

virus01

virus02

web_programming

Sponsor001

 

Page created 19 April 2005, updated 13 June 2008.

JavaScript

With so many programming languages available, it's difficult to choose which one to learn. Personally, I believe that PHP is the best language to use on the server side, but it is better to use Javascript on the client side. I am of course, referring to web pages.

WHY USE JAVASCRIPT?

It is easier to program with PHP, but PHP works on the web server. So every time that you use PHP, you have to download a web page. So if something can be done on the client side, it makes more sense to use Javascript. For example, if you were to fill in a form, and send it to the server, any error would be handled by the server script. This would involve submitting the form, returning an error page, and sending the form again. This takes up the clients time, and uses up unnecessary bandwidth. Alternatively, the error checking could be done on the client side, using Javascript.

PROBLEMS

JavaScript is interpreted by the browser! Which means that the script will only work if it is supported by the clients web browser. For example, I have created scripts which work perfectly well using Internet Explorer 6, but don't work with either the Firefox or the Opera browsers. To illustrate this point further, there are some games at the Fun and Games web site, which are written in Javascript, and only work with Internet Explorer. So the questions to ask are, "Will the script work on different browsers"? and "Will the script prevent the web page from working"?

EXAMPLE CODE

This article isn't meant to teach you every aspect of Javascript, as there are so many books available on the subject. What you will see, are scripts that I have created and used.

FILE UPLOAD

In the articles about PHP, there are some scripts which I used to upload a photo from my computer to the server. This involves using a form called uploader.htm, and calling another web page uploader.php. One problem that I had to solve, was to stop people uploading files which were not photos, or photos with the 'bmp' extension, as they are too large. I decided to allow photos with a 'jpg' of 'gif' extension only. This involves screening out the extension mime type, and alerting any errors.

THE SCRIPT

The method which I used, was to break the file name into single letters, find the period, and then rebuild the string from the last period. The code contains html tags, and the upload form. The variables that I used have a '$' symbol, simply to conform with my PHP programming.

<HTML>

 <HEAD>

  <TITLE>File Upload</TITLE>

  </HEAD>

 <BODY BGCOLOR="WHITE" TEXT="#0101BA" VLINK="WHITE" ALINK="RED">

<SCRIPT TYPE = "text/javascript">

var $file = "";

var $str = "";

function checkfile(){

$file = document.f.file.value;

for( var i = 0; i < $file.length; i++){

var $char = $file.charAt(i);

if( $char == "." ){

$str = "";

}

$str += $char;

}

if( $str != ".jpg" && $str != ".gif"){

alert("Unable to send!\nPlease select a file, which has either a jpg or gif extension.");

return false;

}

else{

return true;

}

}

</SCRIPT>

<H1>File Upload</H1>

<p>The file must be a picture with either a jpg or gif extension!<br>Anything else will be rejected by the error handler.</p>

<H3>Select a file to upload:</H3>

<FORM NAME="f" ACTION="uploader.php" ONSUBMIT = "return checkfile()" METHOD="post" ENCTYPE="multipart/form-data">

<P>

<INPUT TYPE="file" NAME="file" SIZE=45></P>

<P>

<INPUT TYPE=SUBMIT VALUE="Upload File"></FORM>

</BODY>

</HTML>