Académique Documents
Professionnel Documents
Culture Documents
Syntax:
vararray-name= [item1,item2, ...];
Type: Object
Using the JavaScript Keyword new
varcars
=newArray("Saab","Volvo","BMW");
Access the Elements of an Array
varcars = ["Saab","Volvo","BMW"];
document.getElementById("demo").inn
erHTML = cars[0];
Associative Arrays
Associative arrays have an index that is not
necessarily an integer, and can be sparsely
populated. The index for an associative
array is called thekey, and its type is called
theKeyType.
Associative arrays are declared by placing
theKeyTypewithin the[ ]of an array
declaration:
int[string] aa; // Associative array of ints
that are // indexed by string keys. // The
KeyType is string. aa["hello"] = 3; // set
value associated with key "hello" to 3
JavaScriptObjects
Object Methods
Methods are actions that can be
performed on objects. A method is
actually a function definition stored
as a property value.
You access an object method with
the following syntax:
objectName.methodName()
If you access the fullName method,
without (), it will return the function
definition:
name = person.fullName;
var person = {
firstName: "John",
lastName : "Doe",
id
: 5566,
fullName : function() {
JavaScript Functions
<script>
function myFunction(p1, p2) {
return p1 * p2;
}
document.getElementById("demo").innerHTML =
myFunction(4, 3);
</script>
var x = myFunction(4, 3); // Function is called,
return value will end up in x
function myFunction(a, b) {
return a * b; // Function returns the
product of a and b
}
Dialog Box
<button onclick="myFunction()">Try
it</button>
<script>
function myFunction() {
Confirm Box
A confirm box is often used if you
want the user to verify or accept
something.
When a confirm box pops up, the
user will have to click either "OK" or
"Cancel" to proceed.
If the user clicks "OK", the box
returns true. If the user clicks
"Cancel", the box returns false.
Syntax
window.confirm("sometext");
var x;
} else {
document.getElementById("demo").innerHTML =
x;
}
</script>
Prompt Box
A prompt box is often used if you want the
user to input a value before entering a page.
When a prompt box pops up, the user will
have to click either "OK" or "Cancel" to
proceed after entering an input value.
If the user clicks "OK" the box returns the
input value. If the user clicks "Cancel" the box
returns null.
Syntax
window.prompt("sometext","defaultText");
The window.prompt() method can be
written without the window prefix.
Line Breaks
To display line breaks inside a popup
box, use a back-slash followed by the
character n.
<button onclick="myFunction()">Try
it</button>
<script>
function myFunction() {
innerHTML
By manipulating an element's innerHtml you'll be
able to change your text and HTML as much as
you like.
Each HTML element has an innerHTML property
that defines both the HTML code and the text that
occurs between that element's opening and
closing tag. By changing an element's innerHTML
after some user interaction, you can make much
more interactive pages.
However, using innerHTML requires some
preparation if you want to be able to use it easily
and reliably. First, you must give the element you
wish to change an id. With that id in place you will
be able to use the getElementById function,