Java Script.pdf
(
1265 KB
)
Pobierz
How to put a JavaScript into an HTML document
How to put a JavaScript into an HTML document
<html>
<head>
</head>
<body>
<script language="JavaScript">
document.write("Hello World!")
</script>
</body>
</html>
And it produces this output:
Hello World!
To insert a script in an HTML document, use the <script> tag. Use the language attribute to define the
scripting language.
<script language="JavaScript">
Then comes the JavaScript: In JavaScript the command for writing some text on a page is
document.write
:
document.write("Hello World!")
The script ends:
</script>
How to handle older browsers
Older browsers that do not support scripts will display the script as page content. To prevent them from
doing this, you can use the HTML comment tag:
<script language="JavaScript">
<!--
some statements
//-->
</script>
The two forward slashes on front of the end of comment line (//-->) are a JavaScript comment symbol, and
prevent the JavaScript from trying to compile the line.
Note that you cannot put // in front of the first comment line (like //<!--), because older browser will display it.
Funny? Yes ! But that's the way it is.
Where to put the JavaScript
Scripts in a page will be executed immediately while the page loads into the browser. This is not always
what we want. Sometimes we want to execute a script when a page loads, other times when a user trigger
an event.
Scripts in the head section:
Scripts to be executed when they are called, or when an event is triggered,
goes in the head section. When you place a script in the head section, you will ensure that the script is
loaded before anyone uses it.
<html>
<head>
<script language="JavaScript">
some statements
</script>
</head>
Scripts in the body section:
Scripts to be executed when the page loads, goes in the body section. When
you place a script in the body section it generates the content of the page.
<html>
<head>
</head>
<body>
<script language="JavaScript">
some statements
</script>
</body>
Scripts in both the body and the head section:
You can place an unlimited number of scripts in your
document, so you can have scripts in both the body and the head section.
<html>
<head>
<script language="JavaScript">
some statements
</script>
</head>
<body>
<script language="JavaScript">
some statements
</script>
</body>
How to use external script
Sometimes you might want to run the same script on several pages, without writing the script on each and
every page. To simplify this you can write the script in a external file, and save it with a .js file extension.
Like this:
document.write("This script is external")
Save the external file as xxx.js
-The filename can not contain more than 8 letters
-The external script can not contain the <script> tag
Now you can call this script, using the "src" attribute, from any of your pages:
<html>
<head>
</head>
<body>
<script src="xxx.js">
</script>
</body>
</html>
Remember to place the script exactly where you normally would write the script.
Variables
A variable is a "container" for information you want to store. A variable's value can change during the script.
You can refer to a variable by name to see its value or to change its value.
Rules for Variable names:
•
Variable names are case sensitive
•
They must begin with a letter or the underscore character
Declaring Variables
You can create a variable with the var statement:
var strname =
some value
You can also create a variable without the var statement:
strname =
some value
Assigning Values to Variables
You assign a value to a variable like this:
var strname = "Hege"
Or like this:
strname = "Hege"
The variable name is on the left side of the expression and the value you want to assign to the variable is
on the right. Now the variable "strname" has the value "Hege".
Assignment Operators
Operator
Example
Result
=
i = 5
i equals 5
+=
i += 5
i equals i + 5
-=
i -= 5
i equals i - 5
*=
i *= 5
i equals i * 5
/=
i /= 5
i equals i / 5
%=
i %= 5
i equals i %5
++
i++
i equals i+1
--
i--
i equals i-1
Lifetime of Variables
When you declare a variable within a function, only code within that function can access or change the
value of that variable. When the function exits, the variable is destroyed. These variables are called local
variables. You can have local variables with the same name in different functions, because each is
recognized only by the function in which it is declared.
If you declare a variable outside a function, all the functions in your script will recognize it. These variables
exists from the time they are declared until the time the script is finished running.
The Boolean object
The Boolean object is one of JavaScript's built-in objects. It can return two values: 0 for false and any other
integer value for true. It is used to return a result of a conditional test. For example if you want some code to
be executed if a condition is false/true.
Methods
Explanation
NN IE ECMA
toString()
Returns a string Boolean value. (true or false)
3.0 3.0 1.0
valueOf()
4.0 J3 1.0
Functions
A function contains some code that will be executed by an event or a call to that function. A function is a set
of statements. You can reuse functions within the same script, or in other documents. You define functions
at the beginning of a file (in the head section), and call them later in the document. It is now time to take a
lesson about the alert-box:
This is JavaScript's method to alert the user.
alert("here goes the message")
How to define a function
To create a function you define its name, any values ("arguments"), and some statements:
function myfunction(
argument1,argument2,etc
)
{
some statements
}
A function with no arguments must include the brackets:
function myfunction()
{
some statements
}
Arguments are variables that will be used in the function. The variable values will be the values passed on
by the function call.
By placing functions in the head section of the document, you make sure that all the code in the function
has been loaded before the function is called.
Some functions returns a value to the calling expression
function result(a,b)
{
c=a+b
return c
}
How to call a function
A function is not executed before it is called.
You can call a function containing arguments:
myfunction(
argument1,argument2,etc
)
or without arguments:
myfunction()
The return statement
Functions that will return a result, must use the return statement, this statement specifies the value which
will be returned to where the function was called from. Say you have a function that returns the sum of two
numbers:
function total(a,b)
{
result=a+b
return result
}
When you call this function you must send two arguments with it:
sum=total(2,3)
Conditionals
Very often when you write code, you want to perform different actions for different decisions. You can use
conditional statements in your code to do this.
Plik z chomika:
musli_com
Inne pliki z tego folderu:
JavaScript_ The Missing Manual [McFarland 2008-07-01].pdf
(21906 KB)
Professional jQuery [Otero 2012-05-01].pdf
(24031 KB)
Smashing Node.js_ JavaScript Everywhere [Rauch 2012-09-04].pdf
(16688 KB)
Practical Dojo Projects [Zammetti 2008-09-24].pdf
(7145 KB)
Beginning JavaScript (5th ed.) [McPeak & Wilton 2015-03-09].pdf
(37093 KB)
Inne foldery tego chomika:
3D Design - Programming
ActionScript
Actionscript - Flash - Flex - Air
Ada
ADO
Zgłoś jeśli
naruszono regulamin