P3 describe the main features of the chosen scripting language and outline some typical uses

colonteeΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

350 εμφανίσεις

Andrew Hill
colontee_bc6597c8
-
db25
-
4bbd
-
9462
-
db49e66c27f7.docx

Page
1


P3 describe the main features of the chosen scripting language and
outline some typical uses

JavaScript is a powerful, complicated computer language. A good example of one of
its

applications is

Google Maps
.

The primary advantage to JavaScript
centres

on

the web browser,
so

produce
s

the same results
on all platforms supported by the browser.
It will

run on Linux, Windows and the Mac OS. With
the growth of numerous javascript libraries it is now easier to navigate a document, select DOM
elements, create an
imations, handle events, and develop Ajax applications. JavaScript is really
the only cross platform technology that is both free and
universally

adopted.

JavaScript is a very easy language to start programming with. All you need is a text editor and a
web

browser to get started.

Getting started with JavaScript is very easy. You don't have to have complicated development
programs installed. You don't have to know how to use a shell
,

program Make or use a compiler.
JavaScript is interpreted by your web brows
er. All you have to do is save your program as a text
file and then open it up in your web browser.


Example: Catching a mouse click

'Mouse' events are a subset of the total events issued by a web browser in response to user
actions. The following is a lis
t of the events in response to a user's mouse action:



Click
-

issued when a user clicks the mouse



DblClick
-

issued when a user double
-
clicks the mouse



MouseDown
-

issued when a user depresses a mouse button (the first half of a click)



MouseUp

-

issued when a user releases a mouse button (the second half of a click)



MouseOut
-

issued when the mouse pointer leaves the graphical bounds of the object



MouseOver
-

issued when the mouse pointer enters the graphical bounds of the object



MouseMove
-

is
sued when the mouse pointer moves while within the graphical bounds of the
object



ContextMenu
-

issued when the user clicks using the right mouse button

The simplest method for capturing these events, to register event handlers
-

using HTML
-

is to
specify

the individual events as attributes for
the

element. Example:


<span onclick="alert ('Hello World!');">Click Here</span>

The JavaScript code to execute can be inlined as the attribute value or you can call a function
which has been defined in a <script>
block within the HTML page:

Andrew Hill
colontee_bc6597c8
-
db25
-
4bbd
-
9462
-
db49e66c27f7.docx

Page
2


<script type="text/javascript">



function onclick_callback ()


{



alert ("Hello, World!");


}

</script>

<span onclick="onclick_callback();">Click Here</span>

Additionally, the event object which is issued can be captured
and referenced; providing the
developer with access to specifics about the event such as which object received the event, the
event's type, and which mouse button was clicked. Using the inline example again:

<script type="text/javascript">



function
onclick_callback(event)



{


var eType = event.type;



/* the following is for compatability */



/* Moz populates the target property of the event object */



/* IE populates the srcElement property */



var eTarget = event.target || event.srcE
lement;




alert ("Captured Event (type="+ eType +", target="+ eTarget);



}

</script>

<span onclick="onclick_callback(event);">Click Here</span>

Uses

This script will find the date from the user’s computer and display it

<script language="JavaScript">

<!
--

function makeArray() {

for (i = 0; i<makeArray.arguments.length; i++)

this[i + 1] = makeArray.arguments[i];

}

var months = new
makeArray('January','February','March','April','May','June',
'July','August','September','October','November','December');

v
ar date = new Date();

var day = date.getDate();

var month = date.getMonth() + 1;

var yy = date.getYear();

var year = (yy < 1000) ? yy + 1900 : yy;

document.write(day + " " + months[month] + " " + year);

//
--
>

</script>


This will display a welcome message
depending on the time:


<script language=javascript>

<!
--

function greeting() {

Now = new Date()

Hour = Now.getHours();

if (Hour < 5)

msg ="We don't get many visitors at this time of night!"

else if(Hour <12)

Andrew Hill
colontee_bc6597c8
-
db25
-
4bbd
-
9462
-
db49e66c27f7.docx

Page
3


msg ="Good morning!"

else if(Hour < 18)

msg

="Good afternoon!"

else if (Hour < 24)

msg ="Good evening
-

looks like you're working late again!"

return( msg )

}

document.write(greeting())

//
--
>

</script>