��� ����: ����� ����� / ����� 2 / �� 3
 


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


���� ������ ��:
1: ������ ������
2: ����� ����� ��������
3: substring
4: ����� split
5: ������ �����������
6: ���' ����� ����������
7: cookies � ������
8: ��� ����� cookies
9: ����� cookies - �'
10: ����� cookies - �'
11: ����� ������
12: ��� ����� cookies
13: cookie - path/domain
14: ����� ����� 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

����� 2

�� 3: substring

����� �- substring ��� ����� ��� ����� �- charAt, ��� ������ ���� ����� ������ �� ������ ���� (��� �� �� ����). ��� ���� ������:

var the_substring = the_string.substring(from, to);

"from" ������ ������ �� ���� ������� ��� �������, �- "to" ������, ����� �����, ������ �� �� ��� ���� ��� ���-������ ����� �����. ����� ������ ����� ������ �� ����� ���� ������� ���� �� ��� ������ ��� ������ ��� ����� �� ���� ���-������ ����� ������:

var the_string = "monkey";
var clergy = the_string.substring(0,4);
var tool = the_string.substring(3,6);

���� ���� ����, ������ clergy (�����) ���� ��� ���� "monk" (����); ������ "tool" (����) ���� ��� ���� "key" (����).

������ ������ ���� ����� ������ substring ��� �� ������ indexOf, �� ��� ������ ��� ����� ����� �������. ������, ���� ������ �� �� ����� (domain) ���� ������ URL ������, �"� ����� ���� ���:

var the_url = prompt("What's the URL?","");
var lead_slashes = the_url.indexOf("//");
var domain_start = lead_slashes + 2;
var without_resource = the_url.substring(domain_start, the_url.length);
var next_slash = without_resource.indexOf("/");
var domain = without_resource.substring(0, next_slash);

�����, �� ���� "http://www.webmonkey.com/javascript/index.html" ��� ��� �- domain ����: "www.webmonkey.com". ��� �� ���� �- domains �� ��� ����� ���� ������. �� ���� ���� �� ��� �����, �� ����, ���� ������ ���� ���� ����, ��� ���� �� ��� ����� ����� ����� ���� ���� ����� �"� ����� ������ split. �����, ����� ����.

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

var the_url = prompt("What's the URL?","");

���� �� ���� ����� ������� ����� URL, ���� ����� �������. ���� ���� ������ �� �������: "http://www.webmonkey.com/javascript/index.html".

var lead_slashes = the_url.indexOf("//");

���� �� ����� �� ����� ��� �������� ��������. ����� ����, lead_slashes ���� ���� "5", ������ ���� �������� ����� ������ 5.

�� ��? ��� ��� ���� "��� ���� URL ����� �� //:http �� ���� ���� ���� ������ ����� ������ ����� ����� ���?" ������ ���, ��� ���� ������� ���� �� ���� ������ ����� �����. ���� ����� �� ���� �����, ����� ���� �����: "/the URL I want is: http://blah.com", �� ����� ���� �- URL ���� �� ��� ��� ������ ����� ���� ��: "https://www.secure.com/me.html".

��� ������ �������� ������, ��� ������ ����� �� �� �������� ��������, ������ ��� ���� ���. ��� �������� ����� ���� ���� ���� ����, ������ �� ���� ��� ���� ��� �����. ���, �� ���� ���� ����� ����� �� ��� ������� ������ 5. ����� ���, ����� ������ ������ indexOf �� ��� ����� ������ �� ����� �� �� �������.

var domain_start = lead_slashes + 2;

����� �� ����� �� ������ �� ��� ������ ��� �- domain. ������ ����� ��� �������, ��� ������ ����� ��� ������ ���� ����� ������ ������.

var without_resource = the_url.substring(domain_start, the_string.length);

����� �� ����� �� �� �� ������� ����� ����� �� �- domain (����� ����) ��� ���� �������. �� ������� ���� ����: "www.webmonkey.com/javascript/index.html".

var next_slash = without_resource.indexOf("/");

����� �� ����� �� ������ �� ������ ��� (���� ��� ��������) �������. �� �� ���� ����� ������� ��� ��� ������ �� ��� ����� �� �- domain. ����� �� ����� �� ������ ��� ���: 17.

var domain = without_resource.substring(0, next_slash);

����� ������ ��, ���� ����� �� �� �� ������ ������ ������� �� ����� �� �� ��� ���� ������. ������ ��: "www.webmonkey.com".

���� ��. ��, �� ����. �� ��� ���� ���� ����, �� �� �� ������ ������� ��� ����� ����� ���� ��� ���. JavaScript 1.2 ������ �- Netscape 4.0, ���� ���� ����� ����� ����� �� ����� �������� ������ ��� ���� ���. ����, ��� ��� ��� �����.

�� ���� ���� ������ ���� ���� �"� ����� ������ split.

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

��� ��� «--

 
       
       
   
 
 

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