Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Methods
Methods are the actions that can be performed on objects.
<script type="text/javascript">
var str="Hello world!";
document.write(str.toUpperCase());
</script>
Methods
• Methods are actions applied to particular objects. Methods are
what objects can do.
– e.g., document.write(”Hello World")
– document is the object. write is the method.
• function person(firstname,lastname,age)
{ this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.changeName = function (name)
{
• this.lastname = name;}}
Ex: var obj1 =new person(“komal”,”valli”,40);
obj1.changeName(“chakra”);
JavaScript is prototype based, not class based.
Events
• Events associate an object with an action.
– e.g., the OnMouseover event handler action can
change an image.
– e.g., the onSubmit event handler sends a form.
• User actions trigger events.
Popup Boxes
• JavaScript has three kind of popup boxes: Alert box,
Confirm box, and Prompt box.
• Alert Box
• An alert box is often used if you want to make sure information comes
through to the user.
• When an alert box pops up, the user will have to click "OK" to proceed.
Ex: alert(text)
• Confirm Box
• A confirm box is often used if you want the user to verify or accept
something.
• When a confirm box pops up, the user will have to click either "OK" or
"Cancel" to proceed.
• If the user clicks "OK", the box returns true. If the user clicks "Cancel", the
box returns false.
Ex: confirm("sometext");
• Prompt Box
• A prompt box is often used if you want the user to input a value before
entering a page.
• When a prompt box pops up, the user will have to click either "OK" or
"Cancel" to proceed after entering an input value.
• If the user clicks "OK" the box returns the input value. If the user clicks
"Cancel" the box returns null.
Ex: prompt("sometext","defaultvalue");
Exception
• The try...catch statement allows you to test a block of code for
errors.
• Syntax :
try
{
//Run some code here
}
catch(err)
{
//Handle errors here
}
• The throw statement can be used together with the try...catch
statement, to create an exception for the error.
HTML DOM
• With the HTML DOM, JavaScript can access all the elements of an HTML
document.
• When a web page is loaded, the browser creates a Document Object Model
of the page.
• The HTML DOM model is constructed as a tree of Objects:
• JavaScript can change all the HTML elements in the page
• JavaScript can change all the HTML attributes in the page
• JavaScript can change all the CSS styles in the page
• JavaScript can react to all the events in the page.
• If we want to manipulate HTML elements, first we have to access the
elements :
Finding HTML elements by id - getElementById(“t1");
Finding HTML elements by tag name –getElementsByTagName(“p”)
Finding HTML elements by class name(CSS)
• The HTML DOM allows JavaScript to change the content of HTML
elements.
• The easiest way to modify the content of an HTML element is by using the
innerHTML property.
Ex: document.getElementById(id).innerHTML=new HTML
• HTML attribute also can be changed.
Ex : document.getElementById(id).attribute=new value
• The HTML DOM allows JavaScript to change the style of HTML elements.
Ex: document.getElementById(id).style.property=new style
• HTML DOM allows JavaScript to react to HTML events.
• HTML DOM allows to create the elements also.
document
• Manipulate document that is currently visible in the
browser window
• - Every Document object has:
• - forms - an array of references to the forms of the
document
• Each Form object has an elements array, which has
references to the form’s elements
• Document also has anchors, links, & images
• Properties:
• cookie - Used to identify the value of a cookie.
• defaultCharset
• domain - The domain name of the document server.
• embeds - An array containing all the plugins in a
document.
• fgColor - The text color attribute set in the <body> tag.
• File
• CreatedDate - Use this value to show when the loaded
HTML file was created
• fileModifiedDate - Use this value to show the last change
date of the loaded HTML file
• fileSize
• fileUpdatedDate
• lastModified - The date the file was modified last.
• layers - An array containing all the layers in a document.
• linkColor - The color of HTML links in the document. It is
specified in the <body> tag.
• location
• mimeType
• nameProp
• protocol
• referrer - The Universal Resource Locator (URL) of the
document that we got the link to the present document
from.
• title - The name of the current document as described
between the header TITLE tags.
• URL - The location of the current document.
• vlinkColor - The color of visited links as specified in the
<body> tag
• close() - Closes an output stream that was
used to create a document object
• Write(String)
• Writeln(string)
• open([mimeType]) - Opens a new
document object with the optional MIME
type
Window
• Properties:
• clientInformation
• closed - A boolean value that indicates whether the
window is closed.
• defaultStatus - This is the default message that is loaded
into the status bar when the window loads.
window.defaultStatus = "Click on a link on the left to
navigate this website."
• event
• external
• lastModified
• length - The number of frames that the window contains
• Methods :
alert(string)
blur()
clearInterval(interval)
clearTimeout(timeID)
close()
confirm("message")
focus()
open("URLname","Windowname",["options"])
• moveBy(x,y) - The window is moved the specified number
of pixels in the X and Y direction.
• moveTo(x,y) - The window is moved to the specified X and
Y pixel location in the browser
• prompt("message","defaultmessage") - A prompt dialog box is
displayed with the message passed as the prompt question or phrase.
The defaultmessage appears in the box, to be replaced by user
entered text. If no default message string is present, no default string
is displayed.
• resizeBy(X,Y) - Adjusts the window size relative to the current value.
• resizeTo(X,Y) - Adjusts the window size to set X and Y width and
height values.
• scroll(X,Y) - The window is scrolled to the location specified by the X
and Y values in pixels.
• scrollBy(X, Y)
• scrollTo(X,Y)
• setInterval(function(),milliseconds,[functargs]) - Cause a function to be
called periodically based on the time value specified. The functargs
are arguments passed to the function.
• setTimeout(function(),milliseconds,[functargs]) - Used to call a function
after the specified time in
Screen