User Defined Functions: Syntax
User Defined Functions: Syntax
Declaring Functions
A list of parameters
does
Syntax:
}
A function_name is case sensitive, can include underscores
Place of Declaration
Passing parameters
multiple parameters.
Example:
function printName(user){
document.write(user);
document.write(“</B></I><HR>”);
Recursive Functions
Example:
function factorial(number){
else{
return number;}}
Example program
<HTML>
<HEAD>
FUNCTIONS</TITLE>
<SCRIPT Language="JavaScript">
var name="";
function hello(){
function goodbye(){
}
</SCRIPT>
</HEAD>
</BODY>
</HTML>
Output
Placing text in a browser
Objectname.Method_name
document.write(“Test”);
Example program
<HTML>
<HEAD>
<TITLE>Outputting text</TITLE></HEAD>
<BODY><CENTER><BR><BR>
document.write("<BR><BR>");
document.write('<IMG Height=100
Src="C:\\Users\\KADHAR\\Desktop\\1.jpg" Width=100>');
</SCRIPT>
</CENTER>
</BODY>
</HTML>
Output
Dialog Boxes
JavaScript script and does not affect the content of the page in
any way.
The alert dialog box displays the string passed to the alert()
date/time
Syntax:
alert(“<Message>”);
Example:
alert(“Click OK to continue”);
Example Program
<HTML>
<HEAD>
<BODY><SCRIPT Language="JavaScript">
document.write('<IMG
Src="C:\\Users\\KADHAR\\Desktop\\1.jpg">');
</SCRIPT>
</BODY>
</HTML>
Output
The Prompt Dialog Box
Can pass what the user keyed into the textbox back to the
javascript
until user action takes place. This could be the ok button being
Syntax
prompt(“<Message>”,”<Default value>”);
Example
Example program
<HTML>
<HEAD>
<BODY><SCRIPT Language="JavaScript">
document.write('<IMG
Src="C:\\Users\\KADHAR\\Desktop\\1.jpg">');
document.write("<H1>Greetings,");
Information Technology!</H1>");
</SCRIPT>
</BODY>
</HTML>
Output
The Confirm Dialog Box
A pre-defined message
Syntax:
confirm(“<Message>”);
Example:
Example Program
<HTML>
<HEAD>
<TITLE>Confirm Method</TITLE>
<SCRIPT Language="JavaScript">
var answer=20;
var correct='<IMG
Src="C:\\Users\\KADHAR\\Desktop\\1.jpg">';
var incorrect='<IMG
Src="C:\\Users\\KADHAR\\Desktop\\2.jpg">';
for(count=1;count<3;count++)
if(Response != answer)
Response =prompt(question,"0");
else
count=3;
}
else
count=3;
document.write("<BR>");
document.write(output);
</SCRIPT>
</HEAD>
<BODY></BODY>
</HTML>
Output
JavaScript DOM introduction
window.
Instance
No HTML object is registered in the DOM by a javascript
does not have any links described in it the links object will
Hierarchy
All objects on a web page are not created equal. Each exists in
the browser from the web server, with a style based on this
a client’s browser.
Since javascript understands the DOM and can extend the
understands objects.
environment.
executes.
the DOM and the JSSS DOM. Hence, using javascript, truly
Browser objects
listed below:
into a window
of an event
object
Images
Image maps
Hyperlinks
Frames
Anchors
Applets
files
page then these arrays will hold indexed elements, which will
object are. Otherwise, the array will exist, but will be empty.
index as [0], the next image in the document will have the
actually are.
listed below:
Image/Images array To access an image that is embedded
objects in a document
image map
applets in a document
supported by a browser
communicator
set
submit button
associated with that specific HTML object will exist, but will
have no elements.
Access to elements of a web page
able to interact with the web page, each element of the web
elements.
Example
The change event of the text box talks to the onChange event
<input type=”text”onChange=”<myFunction>”>
executes.
page and so on
DOM.
Other web page events could be the opening or closing of a
multistep process
correctly.
page.
handler
document is clicked
window or frame
input focus
onKeyDown The user presses a key
loaded
area
button