JavaScript. Variables and functions

JavaScript is the scripting language of the Web. All modern HTML pages are using JavaScript.

JavaScript allows you to work with three primitive data types:

Numbers eg. 123, 120.50 etc. Strings of text e.g. "This text string" etc.

Boolean e.g. true or false.

Like many other programming languages, JavaScript has variables. Variables can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container. Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows:

<script type="text/javascript">

<!-- var money; var name; //--> </script>

For instance, you might create a variable named money and assign the value 2000.50 to it later. For another variable you can assign a value the time of initialization as follows:<script type="text/javascript">

<!-- var name = "Ali"; var money; money = 2000.50; //-->

</script>

A function is a block of code that will be executed when "someone" calls it:

<head> <script> function myFunction() {alert("Hello World!");}</script> </head>

A function is written as a code block (inside curly { } braces), preceded by the function keyword:

function functionname() { some code to be executed }

The code inside the function will be executed when "someone" calls the function. The function can be called directly when an event occurs (like when a user clicks a button), and it can be called from "anywhere" by JavaScript code.

JavaScript. Date and time.

function date_time(id)

{date = new Date; year = date.getFullYear(); month = date.getMonth();

months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'Jully', 'August', 'September', 'October', 'November', 'December');

d = date.getDate();

day = date.getDay();

days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

h = date.getHours();

if(h<10)

{ h = "0"+h; }

m = date.getMinutes();

if(m<10)

{m = "0"+m; }

s = date.getSeconds();

if(s<10)

{

s = "0"+s;

}

result = ''+days[day]+' '+months[month]+' '+d+' '+year+' '+h+':'+m+':'+s;

document.getElementById(id).innerHTML = result;

setTimeout('date_time("'+id+'");','1000');

return true;

}


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: