Vous êtes sur la page 1sur 28

JavaScript

JavaScript ????

JavaScript is THE scripting language of the Web. JavaScript is easy to learn. You will enjoy it.

What is JavaScript?

JavaScript is used in billions of Web pages to add interactivity, functionality, validate forms, communicate with the server, and much more. JavaScript is a scripting language A scripting language is a lightweight programming language A JavaScript is usually embedded directly into HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary compilation) Everyone can use JavaScript without purchasing a license

What can a JavaScript Do?

JavaScript gives HTML designers a programming tool JavaScript can put dynamic text into an HTML page JavaScript can react to events JavaScript can read and write HTML elements JavaScript can be used to validate data JavaScript can be used to detect the visitor's browser JavaScript can be used to create cookies

JavaScript How To ...

The HTML <script> tag is used to insert a JavaScript into an HTML page.

Example
<html> <body> <h1>My First Web Page</h1>

<script type="text/javascript"> document.write("<p>" + Date() + "</p>"); </script>


</body> </html>

How to Handle Older Browsers

use the HTML comment tag

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

JavaScript Where To ...

JavaScripts can be put in the <body> and in the <head> sections of an HTML page. You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section at the same time. JavaScripts in an HTML page will be executed when the page loads. This is not always what we want. Sometimes we want to execute a JavaScript when an event occurs, such as when a user clicks a button. When this is the case we can put the script inside a function.

JavaScript Where To ...


Events are normally used in combination with functions (like calling a function when an event occurs). It is a common practice to put all functions in the head section, or at the bottom of the page. This way they are all in one place and do not interfere with page content.

Using an External JavaScript


Save the external JavaScript file with a .js file extension. <html> <head> <script type="text/javascript" src="xxx.js"></script> </head> <body> </body> </html>

JavaScript Comments

<script type="text/javascript"> /* The code below will write one heading and two paragraphs */ document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); //this is single line comment </script>

JavaScript Variables

Variable names are case sensitive (y and Y are two different variables) Variable names must begin with a letter or the underscore character Declaring (Creating) JavaScript Variables
var

x; var carname; var x=5; var carname="Volvo";

Local & Global JavaScript Variables

A variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function. (the variable has local scope). Variables declared outside a function become GLOBAL, and all scripts and functions on the web page can access it.

JavaScript Popup Boxes

Alert Box : An alert box is often used if you want to make sure information comes through to the user.

alert("sometext");

Confirm Box : A confirm box is often used if you want the user to verify or accept something.

var r=confirm("Press a button"); if (r==true) { alert("You pressed OK!"); } else { alert("You pressed Cancel!"); }

JavaScript Popup Boxes

Prompt Box : A prompt box is often used if you want the user to input a value before entering a page.
var

name=prompt("Please enter your name","Harry Potter"); if (name!=null && name!="") { document.write("Hello " + name + "! How are you today?"); }

JavaScript If...Else Statements


Same as C++ and Java <script type="text/javascript"> var d = new Date(); var time = d.getHours(); if (time < 10) { document.write("Good morning!"); } else { document.write("Good day!"); } </script>

JavaScript For Loop

Same as C++ and Java


for (i = 1; i <= 6; i++) { document.write("<h" + i + ">This is heading " + i); document.write("</h" + i + ">"); }

For...In Statement

var person={fname:"John",lname:"Doe",age:25}; for (x in person) { document.write(person[x] + " "); }

JavaScript Functions

To keep the browser from executing a script when the page loads, you can put your script into a function. A function contains code that will be executed by an event or by a call to the function. You may call a function from anywhere within a page (or even from other pages if the function is embedded in an external .js file). Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that a function is read/loaded by the browser before it is called, it could be wise to put functions in the <head> section.

Function Example

There are no Procedures


<head> <script type="text/javascript"> function displaymessage() { alert("Hello World!"); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body>

With Return

<head> <script type="text/javascript"> function product(a,b) { return a*b; } </script> </head> <body> <script type="text/javascript"> document.write(product(4,3)); </script> </body>

JavaScript Events

JavaScript Events are actions that can be detected by JavaScript.


<html> <head> <script type="text/javascript"> function displayDate() { document.getElementById("demo").innerHTML=Date(); } </script> </head> <body> <h1>My First Web Page</h1> <p id="demo"></p> <button type="button" onclick="displayDate()">Display Date</button> </body> </html>

JavaScript Form Validation


JavaScript can be used to validate data in HTML forms before sending off the content to a server. This function below can be used to validate email addresses function validateForm() { var x=document.forms["myForm"]["email"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } }

The Form to be validated

<form name="myForm" action="demo_form.php" onsubmit="return validateForm();" method="post"> Email: <input type="text" name="email"> <input type="submit" value="Submit"> </form>

Get Monitor Screen Resolution with Javascript


<SCRIPT language="JavaScript"> <!-height = screen.height; width = screen.width; document.write( width + " x " + height); //--> </SCRIPT>

Use screen resolution info


<script type="text/javascript"> if ((screen.width<=800) && (screen.height<=600)) { window.location.replace('http://example.com/800600-or-less'); } else { window.location.replace('http://example.com/great er-than-800-600'); } </script>

DOM

DOM = Document Object Model The HTML DOM defines a standard way for accessing and manipulating HTML documents. The DOM presents an HTML document as a tree-structure.

Changing HTML Elements using DOM


<html> <body> <h1>My First Web Page</h1> <p id="demo"></p> <script type="text/javascript"> document.getElementById("demo").innerHTML=Dat e(); </script> </body> </html>

Vous aimerez peut-être aussi