lundi 4 octobre 2010

Javascript Review

JavaScript is what the language is usually called. However, the name JavaScript is owned by Netscape. Microsoft calls its version of the language JScript. The generic name of the language is EcmaScript.

Difference between Javascript and JScript :

http://javascript.about.com/od/reference/a/jscript.htm

Hello World :

<html>
<
body>
<
script type="text/javascript">
document.write("Hello World!");
</script>
</
body>
</
html>

Javascript in the Head section :

<html>
<
head>
<
script type="text/javascript">
function
message()
{
alert("This alert box was called with the onload event");
}
</script>
</
head>

<
body onload="message()">
<
p>We usually use the head section for functions (to be sure that the functions are loaded before they are called).</p>
</
body>
</
html>

For browsers that do not support JavaScript :

Browsers that do not support JavaScript, will display JavaScript as page content. To prevent them from doing this, and as a part of the JavaScript standard, the HTML comment tag should be used to "hide" the JavaScript. Just add an HTML comment tag <!-- before the first JavaScript statement, and a --> (end of comment) after the last JavaScript statement, like this:



<html>
<
body>
<
script type="text/javascript">
<!--
document.write("Hello World!");
//-->
</script>
</
body>
</
html>



Alert Box :

alert("You pressed Cancel!");

Confirm Box :

confirm("Press a button");

Prompt Box :

function Prompt() {
prompt("sometext","defaultvalue");
}

The for Loop :

for (var=startvalue;var<=endvalue;var=var+increment)
{
//code to be executed
}

getElementById() Accesses the first element with the specified id.

<head>
<
script type="text/javascript">
function
getValue() {
var x = document.getElementById("myHeader");
alert(x.innerHTML);
}
</script>
</
head>
<
body>

<
h1 id="myHeader" onclick="getValue()">Click me!</h1>

</
body>
</
html>

getElementByName Accesses all elements with the specified name.

<html>
<
head>
<
script type="text/javascript">
function
getElements() {
var x = document.getElementsByName("x");
alert(x.length);
}
</script>
</
head>
<
body>

<
input name="x" type="text" size="20" /><br />
<
input name="x" type="text" size="20" /><br />
<
input name="x" type="text" size="20" /><br /><br />
<
input type="button" onclick="getElements()" value="How many elements named 'x'?" />

</
body>
</
html>