Java script

Java script is a different language than java and is defined in the http://www.ecma-international.org/publications/standards/Ecma-262.htm ECMA standard 262 or ISO/IEC 16262. It is usually directly embedded into a html page and requires a browser to interpret and run it. Copy it into the console and you have the html file:

cat > HelloWorldScript.html << "EOF"

<html>

<body>

<script type="text/javascript">

document.write("<b>Hello World!</b>");

</script>

</body>

</html>

EOF

Java script can also be outside of an html page as a separated file. Then inside the html a reference is then required:

    <script type="text/javascript" src="<script name>.js"></script>

Note

It is important to set the </script> end tag and do not use /> in the beginning tag.

It is advised to put the script outside the page, since many xml tools simply do not get a clue that the xml file parsed is suddenly javascript so a javascript line as

for(i=0;i<81;i++){

gets interpreted as a half tag <81 and producing an error.

Javascripts have a similar syntax as C or Java.

A difference is that variables are declared without a type. The reasons for that is that variables are actually objects in Javascript and objects have methods: s.toUpperCase() converts the string variable's (object) data to uppercase.

var s="Urs"
    s.length  // no brackets it is not a function it is data

an other difference to C are that the functions are defined with the keyword function and no return data type:

function clean_out_lines()
{
var i;
var n=0;

Javascripts in the body are executed when the client load the page.

<body onload="Runme()">

is an alternative to load them but not have the code in the body.

They can also be executed by events as button clicks. The following example replaces text (identified by the id attribute) when the button is pressed:

<html>
<head>
<title>Hello Applet</title>
<script type="text/javascript">
function ShowDate(){
  document.getElementById("time").innerHTML=Date();
}
</script>
</head>
<body>
<p>Press the button and</p>
<p id="time">Here comes the time</p>
<button type="button" onclick="ShowDate()">Button</button>
</body>
</html>

An other nice event is

t=setTimeout('<function name>()',1000);

This starts a function after a delay. If this is the same function than the one calling it, the function will be called periodically. This can be used to show stuff being alive at the web page or update the web page periodically using:

window.location.reload() 

Reloading web pages periodically produce flickering of reload icons and loading text on the browser border. An other approach is using Ajax for that, that allows to reload just certain elements of the page.

For interactions pop up boxes can be used:

    var value=prompt("Prompt text","default text");
var r=confirm("Are you sure?"); 
alert("alert text!");

Browsers come with Javascript development support containing a javascript console where the ShowDate function can be called without pressing the button: ShowDate()

Usually javascript variables live just as the page is in the browser, however javascripts can use cookies to store them as non-volatile variables.

To debug javascripts on new firefox versions click on Tools > Web Developer => Debug.

Javascript makes use of DOM to access data inside a file. There is HTML DOM and XML DOM. That allows to access tags, text, attributes using the DOM way.

The library jQuery can be used to with javascript to make life easier. It is an external file containing javascript that can be made available by putting a link in the web pages javascript code:

    <script type="text/javascript" src="jquery.js"></script>

The file can be downloaded or left on Internet

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>

or

<script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
</script>

Ajax is used to communicate to the server without need to reload whole pages.

Javascript can be developed within firefox. It contains a debugger. The javascript needs to be edited outside of firefox containing a syntax capable editor. If there is a syntax error then firefox refuses to load the file. This makes it difficult to find the error. Therefore a environment as eclipse JavaScript Development Tools (JSDT) can be used that parse the Javascript code and show an error before the browser refuses it to load. JavaScript Development Tools (JSDT) might be already installed on eclipse so just a JavaScript project needs to be created. JavaScript Development Tools (JSDT) can do more as debugging javascript using rihno https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino

Javascript in Firefox

Linurs Servernest startpage