Java Script
Java Script
Sample methods:
location- to go to a new location
open- to open a new window
setTimeout- to set a time interval before activating an event
document Document is derived from the window object. It is a container for all HTML,
HEAD, and BODY objects associated within the HTML tags of the HTML
documents.
Sample methods:
fgcolor- to set the document foreground color
cookie- to read the information stored in a cookie text file
write- to display a message
lastModified- to display the date it was last modified
Commonly used properties and methods:
Object Definition
math The math object provides the capability to perform math operations. You need to
write the object name to use these methods.
Sample methods:
PI- has the value
pow (a, b)- takes the value a to the power b
max (a, b)- returns the larger value between a and b
min (a, b)- returns the smaller value between a and b
navigator The navigator object determines which browser in running. You need to write the
object name to use these methods.
Sample methods:
appName- to determine the browse’s code name
appVersion- to determine the browser’s release version
cookieEnabled- to determine whether cookies are enabled or not
platform- a description of the operating system
Commonly used properties and methods:
An event is the result of the user’s action. Event handlers are the way to connect that action to a
function or a set of JavaScript codes to be executed. Loading of a HTML document, mouse clicks,
and even keyboard press are examples of events. Events are objects with properties.
Example an event
Where Can You Place Scripts?
<html>
<head>
<script type=“text/JavaScript” function intro ()
{ JavaScript inside
alert (“Welcome Visitors!”); <head></head>
}
</script>
</head>
<body onload=“intro()”></body>
</html>
Whenever the scripts are placed here, you are assured that they will be pre-loaded– it means the
scripts will be executed before anyone triggers an event. Scripts that are ideally placed here are for
functions calls.
Where Can You Place Scripts?
<html>
<head>
</head> JavaScript inside
<body><script type=“text/JavaScript”> <body></body>
document.write (“Message by JavaScript”);
</script>
</body>
</html>
Scripts that are placed here are executed when the page loads.
Where Can You Place Scripts?
<html>
<head>
<script type=“text/JavaScript”>
document.write (“Head Section”);
</script> JavaScript inside
<body></body> and
</head> <head></head>
<body><script type=“text/JavaScript”>
document.write (“Body Section”);
</script>
</body>
</html>
You can place your script tags wherever and how many you want to.
Where Can You Place Scripts?
External JavaScript
External JavaScript files are helpful especially if you are using them to control different HTML files.
There is no need to rewrite them all over again. Save the file having an extension of .js. To use the
JavaScript file, all you have to do is use the src attribute and provide the destination of the file.
The src attribute will point to the source of the JavaScript external file. Take note that the JavaScript
file does not contain the opening and closing script tags.
What are the JavaScript Guidelines?
The Do and Don’ts in coding JavaScript.
Case sensitive- You must be careful in naming your variables. The variable myHome is not equal
to MyHome. It is important to keep track of your variables so there won’t be problems in calling
variables, objects, or functions. Unlike HTML, uppercase letters are not equivalent to lowercase
letters in JavaScript.
White Space- JavaScript ignores white space as long as it is within the line of the code. It is good
practice to use white spaces between expressions to make your codes easier to read.
Breaking up of codes lines – you can break a code line by using a backslash (‘/’). However, do
not break the code in between commands or else it will not work properly.