JavaScript Basics: A Comprehensive Guide
JavaScript Basics: A Comprehensive Guide
JavaScript: Introduction
Introduction
03/02/2021 [Link] 2
1
03/02/2021
Introduction
03/02/2021 [Link] 3
2
03/02/2021
The two Javascript tags must be placed between the <body> and </body> tags
in the case of a direct execution or between the <head> and </head> tags of the page
HTML for deferred execution.
Stored in a file on the server at its call address in the form of TEXT
IMPORTANT the extension .txt or .js
Simplifies the maintenance of sites using common JavaScript modules
to several HTML pages.
03/02/2021 [Link] 6
3
03/02/2021
alert() method
•is used to display simple text-type information to the user. Once the latter has read it,
He must click OK to make the box disappear.
confirm() method
allows the user to choose between the OK and Cancel buttons.
•prompt() method
The prompt() method allows the user to type their own message in response to the question.
posed
The [Link] allows you to write HTML code on the WEB page
03/02/2021 [Link] 7
<html>
<head>
<title> une page simple </title>
</head>
<body>
Hello
<script language='javascript'>
alert('hello');
[Link] (
prompt('What is your name?', 'Please enter your name here')
);
confirm('which button will you choose?');
</script>
</body>
</html>
03/02/2021 [Link] 8
4
03/02/2021
The syntax of the Javascript language is based on the model of Java and C.
General rules
• The insertion of spaces can be done anywhere in the script.
• Each command must end with a semicolon (;).
• A decimal number is separated by a point (.) and not by a comma.
• The JavaScript language is case-sensitive.
• There are two methods to integrate comments into your
scripts.
• Place a double slash (//) in front of the text
• Frame the text with a slash followed by a star (/*) and the same sequence reversed.
(*/)
03/02/2021 [Link] 9
The variables
03/02/2021 [Link] 10
5
03/02/2021
The variables
The scope
Variables can be global or local.
A global variable is declared at the beginning of the script and is accessible from anywhere.
place of the program.
A local variable is declared inside a function and can only be used within the function.
function itself.
03/02/2021 [Link] 11
The variables
The type of a variable depends on the value stored in that variable.
type declaration.
•var maVariable= ‘Philippe’;
maVariable =10;
three main types of values
String
Number10^-308 < number < 10^308
Integers
floating point decimal numbers
3 special values:
Positive Infinity or +Infinity (positive infinite value)
•Negative Infinity ou–Infinity (valeur infinie négative)
•Nan (Not a Number) usually generated as a result of an operation
incoherent mathematics
Boolean
two literal values: true (true) and false (false).
03/02/2021 [Link] 12
6
03/02/2021
Special values
03/02/2021 [Link] 13
Operations on strings
Concatenation
var chain = " hello " + " FI3/FCD1 ";
7
03/02/2021
•eval
This function executes JavaScript code from a string.
...
<SCRIPT LANGUAGE="JavaScript">
function evaluation() {
[Link]=eval([Link]); }
</SCRIPT>
...
<FORM NAME="form">
Enter a mathematical expression: <INPUT TYPE="text" NAME=saisie MAXLENGTH=40 SIZE=40>
<INPUT TYPE="button" VALUE="evaluation." onClick="evaluation()">
<INPUT TYPE="text" NAME=calcul MAXLENGTH=40 SIZE=40>
</FORM>...
03/02/2021 [Link] 15
String
converts the specified object to a string
day = new Date(430054663215); // Converts the number into date Month day, Year etc.
alert(String(day));
Escape
returns the hexadecimal value from a string encoded in ISO-Latin-1.
!&//return!&%
03/02/2021 [Link] 16
8
03/02/2021
The Objects
JavaScript objects are either predefined entities of the language or
created by the programmer.
For example, the browser is an object called "navigator
The browser window is called 'window
The HTML page is another object, which we call "document
A form inside a "document is also an object.
A hyperlink in an HTML page is yet another object. It is called "link.
etc...
JavaScript objects can respond to 'Events'.
Not all browsers support the same objects
Access to the properties of an object
•[Link]
•[Link] = verte
03/02/2021 [Link] 17
Les Objets
A string of characters
03/02/2021 [Link] 18
9
03/02/2021
Les Objets
var i = 1;
typeofi;//returnsnumber
var titre="Les raisins de la colère";
typeof title; //returnsstring
var jour = new Date();
typeofjour;//returnsobject
var choice = true; typeofchoice;//returnsboolean
var cas = null; typeofcas;//returnsobject
typeof parseFloat; //returnsfunction
typeofMath; // returns object (IE 5.*, NS 6.*, NS 4.78, Opera 6.*, Opera 5.*
typeofMath; // returns function NS 3.*, Opera 3.*
03/02/2021 [Link] 19
Property:
•length: retourne la longueur de la chaîne de caractères;
Methods:
•anchor( ): formats the string with the named <A> tag;
•b( ): format the string with the <B> tag; to be reviewed !!
•big( ): formats the string with the <BIG> tag;
charAt( ): returns the character located at a certain position;
charCodeAt(): returns the code of the character at a certain position;
concat(): allows you to concatenate 2 strings;
fromCharCode( ): returns the character associated with the code;
indexOf( ): allows you to find the index of occurrence of a character in a string;
03/02/2021 [Link] 20
10
03/02/2021
03/02/2021 [Link] 21
Property:
•length: retourne le nombre d'éléments du tableau;
Methods:
•concat( ): allows you to concatenate 2 arrays;
join( ): converts an array into a string;
reverse( ): inverses the ranking of the elements in the array;
slice(): returns a section of the array;
•sort( ): allows for the sorting of array elements;
03/02/2021 [Link] 22
11
03/02/2021
The highest object in the hierarchy iswindow that corresponds to the window
even from the browser.
Browser objects
12
03/02/2021
03/02/2021 [Link] 25
03/02/2021 [Link] 26
13
03/02/2021
Methods:
•blur( ): removes the focus from the window;
close( ): closes the window;
•focus(): sets the focus on the window;
moveBy( ): moves by a distance;
moveTo( ): moves the window to a specified point;
open( ): opens a new window;
print( ): prints the content of the window;
•resizeBy( ): resizes by a certain ratio;
resizeTo( ): resizes the window;
setTimeout(): evaluates a string after a certain period of time.
03/02/2021 [Link] 27
Properties:
•applets: returns the collection of Java applets present in the document;
•cookie: allows to store a cookie;
•domain: indicates the domain name of the server that provided the document;
forms: returns the collection of forms present in the document;
•images: retourne la collection d'images présentes dans le document;
•links: returns the collection of links present in the document;
•referrer: indique l'adresse de la page précédente;
•title: indique le titre du document.
Methods:
close( ): closes the document for writing;
open( ): opens the document for writing;
write( ): writes in the document;
•writeln( ): writes to the document and performs a line break
03/02/2021 [Link] 28
14
03/02/2021
The events
JavaScript is event-driven
occur during various actions on the objects of an HTML document.
onLoad;
onClick
onMouseover
onMouseout
onChange
•…
03/02/2021 [Link] 29
The events
•Evénement onLoad
It occurs when a web page is loaded in the browser window.
The entire page (including the images it contains if their loading is planned) must
having been charged for it to take place
This event can only be associated with an image; in which case, it occurs a
once its loading is complete
alert('page loaded');
Example of the onLoad event
</BODY></HTML>
03/02/2021 [Link] 30
15
03/02/2021
The events
•Click Event
It occurs when the user clicks on a specific element on a page, such as
a hyperlink, an image, a button, text, etc.
It can also be triggered when the user clicks anywhere on the page if
has been associated not with a specific element, but with the entire body element
<HTML><BODY>
<INPUT TYPE="Button" Value="click here" onClick="alert('Click')">
</BODY></HTML>
03/02/2021 [Link] 31
The events
Mouseover event
Similar to onClicks, it is enough for the user to place their mouse pointer over one
the aforementioned elements (hyperlink, image, button, text, etc.) for it to take place
Mouseout event
•A l’inverse deonMouseover, cet événement se produit lorsque le pointeur de la souris
leaves the selection area of an element.
<HTML><BODY>
<IMG SRC="[Link]" onMouseOver="src='[Link]';"
onMouseOut="src='[Link]';">
</BODY></HTML>
03/02/2021 [Link] 32
16
03/02/2021
Naming of objects
To be able to manipulate an object in JavaScript, it must have a name.
In the case where the object is unique, there is no need for a name to designate this object.
Example: the case of BODY (only one BODY per document), DOCUMENT (only one DOCUMENT)
by window
03/02/2021 [Link] 33
Manipulation of objects
To address an object, it is not enough to give its name: one must also
specify its "access path" in the hierarchy of the structure
<HTML><BODY
onLoad="[Link]='Hello';">
<FORM name="form"><INPUT NAME="field" TYPE="text">
</FORM></BODY></HTML>
If the window name is omitted, the browser will default to the window
current
In the case of frames, it is relevant to give the name of the window.
It is also possible to omit [Link]: the addressing succeeds since it
There is only one object 'document' in the window.
03/02/2021 [Link] 34
17
03/02/2021
Properties Methods
namefield name focusgive the focus (cursor)
blurremove the focus
typetype of field (text, button, radio,
•clicksimulate a click (on a button)
checkbox, submit, reset
Events
valuetext value
onFocusdetects the focus grab
sizefield size onBlurdetects loss of focus
maxlengthmaximum size of a text field onClickdetects a click
checkedcheckbox / radio button checked or not onChangedetects changes
disabledgrayed out (modification not possible)
readOnlyread-only
classstyle class name
stylename of the style
03/02/2021 [Link] 35
Accepted properties
•name, value, defaultvalue, size, maxlength, disabled, readOnly, class, style
Accepted methods
focus, blur
Accepted events
•onFocus, onBlur, onChange
03/02/2021 [Link] 36
18