Vous êtes sur la page 1sur 30

JavaScript Codes

Click to edit Master subtitle style

7/27/12

Creating a script Block


<script ...> JavaScript code goes here </script>

7/27/12

Hiding Your JavaScript Code


<body> <script language=JavaScript> <!-document.write(Hello); // --> </script> </body>


7/27/12

Providing Alternatives to Your JavaScript Code <body>


<script language=JavaScript> <!-document.write(Hello); // --> </script> <noscript> Hello to the non-JavaScript browser. </noscript>
7/27/12

</body>

Including Outside Source Code

<script language=JavaScript src=filename.js> </script>

7/27/12

Commenting Your Scripts

<body> <script language=JavaScript> // This is a one-line comment document.write(Hello); /* This is a multiline comment */ </script>

7/27/12

Writing a JavaScript Command


<body> <script language=JavaScript> <!-document.write(Hello); document.write( there); document.write(.); // --> </script>

7/27/12

Using Curly Brackets


<body> <script document.write(Hello language=JavaScript> ); <!-{ document.write( document.write(Hello); there); document.write( there); document.write(.); } // --> } </script> </body> if (condition) {

7/27/12

Writing Output to the Browser

if you issue the following document.write command: document.write(<strong>Hello</str ong>); the browser receives the following HTML and renders it: <strong>Hello</strong> { document.writeln(a);

7/27/12

Creating and Outputting any Variable

var myVariable; ... myVariable = some value; var myVariable = some document.write(myVari value; able);

7/27/12

String

var aVar = This is a string; Concatenating Strings var myVariable = ab + cd; Makes myVariable = abcd .

7/27/12

Searching for Text in Strings

variableName.search(is); <body> <script language=JavaScript> <!-var myVariable = Hello there; var therePlace = myVariable.search(there); 7/27/12

Strings

Replacing Text in Strings thisVar.replace(Monday,Friday); big: Returns the string in big tags blink: Returns the string in blink tags bold: Returns the string in b tags fixed: Returns the string in tt tags (for fixed-width display) 7/27/12

italics: Returns the string in i tags small: Returns the string in small tags strike: Returns the string in strike tags (for a strikethrough effect) sub: Returns the string in sub tags (for a subscript effect) sup: Returns the string in sup tags 7/27/12 a superscript effect) (for

document.write(myVariable.big() + <br>); document.write(myVariable.blink() + <br>); document.write(myVariable.bold() + <br>); document.write(myVariable.fixed() + <br>); document.write(myVariable.fontcolor (red) + <br>);
7/27/12

Arrays

var arrayName = new Array(number of elements); arrayName[0] = value 1; var arrayName = new Array(value 1, value 2, value 3, etc.) arrayName.sort(); document.write(myArray.sort()); The elements will be separated by commas
7/27/12

Looping through an Array


for (i = 0; i < myArray.length; i++){ statement; }

7/27/12

Splitting a String at a Delimiter

First element,Second element,Third element var thisVar = First element,Second element,Third element; var anotherVar = thisVar.split(,);

anotherVar is now an array containing three elements.

7/27/12

Functions

functionName(argument 1, argument 2, etc.);

7/27/12

Alerting the User


<head> <script language=JavaScript> <!-window.alert(Hello); // --> </script> </head>


7/27/12

Confirming with the User

<body> <script language=JavaScript> <!-var result = window.confirm(Click OK to continue); var userChoice = window.confirm(Choose OK or Cancel); if (userChoice == true) {
7/27/12

document.write(OK);

Creating Your Own Functions


<head> <script language=JavaScript> <!-function head() { document.write(Hello); } // --> </script>

7/27/12

Calling Functions from Tags


<head> <script language=JavaScript> <!-function hello() { window.alert(Hello); } // --> </script>


7/27/12

</head>

Calling Your JavaScript Code after the Page Has Loaded


<head> <script language=JavaScript> function functionName() { Code to execute when the page finishes loading } </script> </head> 7/27/12

Loops

for (i = 1; i <= 10; i ++) { Code to execute in the loop } while (condition) { JavaScript command JavaScript command etc. }
7/27/12

Scheduling a Function for Future Execution

window.setTimeout(function to execute,schedule time); <head> <script> <!-function hello() { window.alert(Hello); } 7/27/12

Scheduling a Function for Recurring Execution


function functionName() { some JavaScript code window.setTimeout(functionName() ,schedule time); } window.setTimeout(functionName() ,schedule time);
7/27/12

Canceling a Scheduled Function


var pointer = window.setTimeout(...); window.clearTimeout(pointer);

7/27/12

Calling Your JavaScript Code after the Page Has Loaded

<body onUnload=functionName();> Body of the page </body> Check If Java Is Enabled with JavaScript var haveJava = 7/27/12 navigator.javaEnabled();

Check If Java Is Enabled with JavaScript


<body> <script language=JavaScript> <!-var haveJava = navigator.javaEnabled(); // --> </script> </body> 7/27/12

Vous aimerez peut-être aussi