��� ����: ����� ��� / ����� 1 / �� 3
 


������ �� ��� �- JavaScript
 


���� ������ ��:
1: �����
2: ������ ��� JavaScript
3: �������� (����������)
4: ������
5: ��� ���� ���
|
|
|
|
|
|

����� ���

�� 3: �������� (����������).

��� ��� ���� �� ��� ���� ��� (������� ������):

function setBackgroundColor(color1, color2, color3, color4)
{
  theColors = new Array(color1, color2, color3, color4);
  var whichColor = Math.round(Math.random()*1000) % 4;
  document.bgColor = theColors[whichColor];
}

��� �� ���� �������, ��� ����� ���� �� ������ ������� ����� ����� �������� ��� �� ��� ������ ���. �� ���� ����� �������, ������ ���� ���� ������ ����� (�����!) Function, ������ ��� �������� (����� �� "setBackgroundColor"). ��������, ����� ��� ������� ���������� �� ��������. ��� �������� ���� �"� �������� ��������� ({ ... }).

���������, ������ �������� (Arguments, Variables and Type)

������, ����� ������ �� �������� �� ����� ��� ����� �����. ������ �����, ����� ���� �������� ����� ��� ��� ��������� ����� �� ����. ��� ����� ���, ������� ���������� �� ��������, ��:

<code>setBackgroundColor('red','green','blue','black');</code>

(������ �� ���� ���� ��������, ��� ��� ���).

��� �����, ������ ���� �� ��� ������ ��� ���� ������ (��� ������) ���� ��������, ��� �"� ����� ����� �� ������ (variables). ������ ����, ���� �� ���� ��:

var color1 = '#FF0000';
var color2 = 'red';
var color3 = 'green';
var color4 = 'blue';

function setBackgroundColor()
{
  theColors = new Array(color1, color2, color3, color4);
  var whichColor = Math.round(Math.random()*1000) % 4;
  document.bgColor = theColors[whichColor];
}

������, ������ ��� ����� ������ ����. ������ var color1 = 'FF0000'; ������ ����� ������ color1 ����� ���� 'FF0000'. ����, ��� ���� ����� �� ������ �������� ��� ��� ��.

theColors = new Array('#FF0000','red','green','blue');
function setBackgroundColor()
{
  var whichColor = Math.round(Math.random()*1000) % 4;
  document.bgColor = theColors[whichColor];
}

����� ����� ��� ���� ���� �� ���, ����� ������ ����� ������ ����� �� ���� ������� ���� �- JavaScript ������, �� ��� ����� �� ����� ��� ���� ����. ����, �� ���� ���� �����, ������ ����� (Domain) ����� ����� ����, ���� ����� ������ ����� �� ����� ����� ��� ���� �� ���� � �� ��� ����� ���� �����.

���� ������ ����� ���� ����� ������ ��� ����� ������ (Type), ������� ���� ����� ��� ���� ���� ����� ������: �������, ������ ���������. ��� �� ���� ����� �� ����� ��� �� ��� ��������� ������. ����, ���� ���� �������. ��� ����� ����� ���� ������.

������ ��� ���� ���� ������: "��� ���� ��� �'����" �� "I steal Java Script". ������ ��, ����, ������: 2 �� 4 �� 2001. ���� ����� ������ ���� ������, ������ �������. ���� 2 ��� ���� ���� '2' ��� ������ (����� ����, 2+2 ��� ����� 4 ������ � '2'+'2' ���� ������� '22').

��� ���� ������ ��� ������� ������� ���� ���� (') ������� ������� ������ ("). �- JavaScript �� ��� ���� ��� ������� ���� �����, �� ��� �� ���� �� ���� �����:

<body onload="alert("this is bad")">
<body onload="alert('this is good')">

(�� ��� �� ���� �� �����, �� ��� ��� �������).

���� ������ �� �������, ������, ��� ��� ���� �����. ������ ��� ������ ������ ���� ����� ����, ���� �� ���� ���������. �������� �������� ������ �����: ���� (document) ��� ������, ����� ��� ������, ��� ���� ����� HTML ��� ������. �� ����� ���� ����� ��� ������ (��� ���� ������ ���������� �� �������), ���� ��� ���� ����, ��� ����� ������:

<body onload="alert(document.URL)">

����� ������ ������ ���� ������� �� ���������, ��� ����� ��� ���� ������ ������� ������ ��� ���. ��� ����� ����. ���� ���� ���� ���� ����� ��� ��� ��� �� ����� �- submit ������.

�� ���� ���� ��� ��� �� �����:

<form name="myForm"
  onSubmit="alert('You entered: ' + document.myForm.myTextField.value);
  return false;">
  <input type="text" name="myTextField">
  <input type="submit" name="Submit" value="Submit">
</form>

��� ������ ��� ���� ��� ���� ���� ��� JavaScript �� ���� ���, ��� ����� ���� ����� �������� ������� (��� ���� �����) ���� �� ������. �� ��� ����� ���� ����� ���� ����� �� ���� ���������� �� ������� ��� ��� �- JavaScript ����� ���� ���� ���.

��� ���� ����, ������ �� �� ��� �� ������. ����, ���� ����� ������ ����� ������ ��� �- URL �� �����:

var myLocation = document.URL;

�����! ��� ���� ������ ��� ���� �� ��� ���� ���� �������. �� ���� �� ���� �� ��� ���� �����, �� ��� ����� ��� ��� ����� ����� ���� �� ����� ���, �� ���� ������� ������ ����.

����� ���������

��� ���� ������� ���������? ����, ���� ��� ����� ������ ������ ��������. ���� ������ ��� �"� ���� �� �������� ��� ����� <script> ��� �� ����� JavaScript ����. ����:

<script>
  <!-- remember to comment your script
  setBackgroundColor('#ff0000','#00ff00','#ffff00','#3333ff');
  // close the comment -->
</script>

��� �� ���� ������� ��� �������� (����� ������) �������.

���� ������ �� ���� ����� �������� ��� ��� ���� �������. ������ �� event handler, ������ ���� ����.

���� ������ ���

��� ��� «--

 
       
       
   
 
 

���� ����
����� �����
����� �����
����� ������
����� ���
��� ����