TweetFacebookShareThis

JavaScript Basics Part 2

Previous Page    Next Page

JavaScript code used to create the welcome dialogue

function welcome ()
{
  var alerttext="Hello and welcome to JavaScript Part Two"
  var confirmtext="Do you wish to continue?"
  var username=prompt("Please enter your name","")
  alert(alerttext)
  confirm(confirmtext)
  alert("hello, "+username+" " + "lets begin Part Two")
}

The code used in the body section

<form class="center">
<input type="button" value="Click here to see how the script works" onclick="welcome ()">
</form>

Statements

Statements can be run when the document is loaded, Immediately after the document loads, when responding to a users action or when called by another script statement, JavaScript statements invoke object methods such as write as in the document.write, initialise or define variables when naming the variable, manipulate variables, change the value of a variable, invoke a function routine that may need to be done over and over again, make programming decisions using conditionals.

Comments

// this is a single line comment

/* this is a comment that can use multiple lines */

<!- - this is for the browser it comments out the javascript if the browser is not set up to read it, but a browser with javascript enabled will be able to read it //- - >

Programming concepts

Server side applications verses client side applications, server-side scripts rely on databases to generate the content, typically for frequently changing sites, PHP, ASP, .NET, JSP and ColdFusion are server-side applications, languages for server-side programs include Java, Python, Perl, C#, C++, Visual basic sometimes using JavaScript for server-side applications, a web author needs to have control or the server and backend databases, the server scripting is often done by accomplished programmers. With client side applications browsers can run separate plugins or applets for audio, video, animation (Adobe, flash, active X)

Rollover Image

Computer Studies rollover image

JavaScript code used to create the rollover image

<script type="text/javascript">
if (document.images)
{
  imageup = new Image();
  imageup.src = "images/javascript_logo.gif";
  imagedown = new Image();
  imagedown.src = "images/logo.gif";
}
  function down(name)
{
  if (document.images)
{
  document[name].src = eval (name + "down.src" );
}
}
  function up (name)
{
  if (document.images) {
  document[name].src = eval (name + "up.src" );
}
}
</script>

The code used in the body section

<a href="basics1.html" onmouseover="down('image')" onmouseout="up('image')">
<img class="rollover" src="images/javascript_logo.gif" name="image" alt="rollover image"/></a>

JavaScript variables

Almost all statements manipulate data (values), JavaScript has several formal data value types, String a series of alphanumeric characters in quotes, Number any number that is not in quotes, Boolean a logical true or false, Object an entity that is defined by properties and methods, Function a function definition and Null a value that is totally devoid of content, the best way to work with data in script is to assign the data to a variable using the var keyword to declare and initialise and you should choose variable names carefully.

When naming variables you can't use spaces or dashes in between the names, although you could use the underscore to separate the words, camel case can be used such as firstName or lastName, also numbers can't be used at the start of the variable name although numbers can be used after the first letter, when declaring numbers in the variable you don't use them inside quotation marks.

Some JavaScript Tutorials
www.w3schools.com
www.javascriptkit.com

Previous Page    Next Page

Recommended Books