Documente Academic
Documente Profesional
Documente Cultură
This tutorial is about JavaScript, and how JavaScript works with HTML and CSS.
JavaScript Introduction
« Previous
Next Chapter »
It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
The following example changes the content (innerHTML) of an HTML element identified with id="demo":
Example
document.getElementById("demo").innerHTML = "Hello JavaScript";
Try it Yourself »
There are several chapters, about the HTML DOM, later in this tutorial.
JavaScript Can Change HTML Attributes
This example changes the value of the source attribute (src) of an HTML <img> element:
The Light bulb
Try it Yourself »
Try it Yourself »
JavaScript was invented by Brendan Eich, to be used in Netscape (a no longer existing browser) in 1995,
and was adopted by the ECMA standard association in 1997.
ECMA-262 is the official name. ECMAScript 5 (version 1.8.5 - July 2010) is the latest standard.
JavaScript Where To
« Previous
Next Chapter »
JavaScripts can be put in the <body> and in the <head> section of an HTML page.
The <script> Tag
The <script> and </script> tells where the JavaScript starts and ends.
The lines between <script> and </script> contain the JavaScript code:
Example
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "My First JavaScript Function";
}
</script>
Often, JavaScript code is written to be executed when an event occurs, like when the user clicks a button.
Invoke a function = call upon a function (ask for the code in the function to be executed).
You will learn much more about functions and events in later chapters.
JavaScript in <head> or <body>
Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.
Often you will see scripts at the bottom of the <body> section. This can reduce display time.
Sometimes you will see all JavaScript functions in the <head> section.
Anyway, separating HTML and JavaScript, and putting all code in one place, is always a good habit.
JavaScript in <head>
In this example, a JavaScript function is placed in the <head> section of an HTML page.
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
</body>
</html>
Try it Yourself »
JavaScript in <body>
In this example, a JavaScript function is placed in the <body> section of an HTML page.
The function is invoked (called) when a button is clicked:
Example
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>
</html>
Try it Yourself »
Note It is a good idea to place scripts at the bottom of the <body> element.
This improves page load, because HTML loading is not blocked by scripts loading.
External JavaScripts
External scripts are practical when the same code is used in many different web pages.
To use an external script, put the name of the script file in the source (src) attribute of the <script> tag:
Example
<!DOCTYPE html>
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>
Try it Yourself »
You can place an external script reference in <head> or <body> as you like.
The script will behave as if it was located exactly where you put the reference in the HTML document.
Note External scripts cannot contain <script> tags.
<p>This example calls a function which performs a calculation, and returns the result:</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
return a * b;
}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>
</body>
</html>
Try it Yourself »
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)
Function arguments are the real values received by the function when it is invoked.
Function Invocation
The code inside the function will execute when "something" invokes (calls) the function:
When JavaScript reaches a return statement, the function will stop executing.
If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.
Functions often compute a return value. The return value is "returned" back to the "caller":
Example
function myFunction(a, b) {
return a * b; // Function returns the product of a and b
}
Try it Yourself »
Why Functions?
You can reuse code: Define the code once, and use it many times.
You can use the same code many times with different arguments, to produce different results.
Example
<script>
function toCelsius(f) {
return (5/9) * (f-32);
}
document.getElementById("demo").innerHTML = toCelsius;
//document.getElementById("demo").innerHTML = toCelsius(77);
</script>
</body>
</html>
JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.
Alert Box
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
window.alert("sometext");
<script>
function myFunction() {
alert("I am an alert box!");
}
</script>
</body>
</html>
Try it Yourself »
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");
<p id="demo"></p>
<script>
function myFunction() {
var x;
if (confirm("Press a button!") == true) {
x = "You pressed OK!";
} else {
x = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Try it Yourself »
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");
<p id="demo"></p>
<script>
function myFunction() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML =
"Hello " + person + "! How are you today?";
}
}
</script>
</body>
</html>
Try it Yourself »
Line Breaks
To display line breaks inside a popup box, use a back-slash followed by the character n.
Example
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
alert("Hello\nHow are you?");
}
</script>
</body>
</html>
Try it Yourself »