Professional Documents
Culture Documents
2
2
دَلران پی سی
Hadi Ahmadi Learning Java Script
دَلران پی سی
Hadi Ahmadi Learning Java Script
با سپاس
دَلران پی سی
Hadi Ahmadi Learning Java Script
(ص)
حممد و آل حممد
رایگان می باشد.
دَلران پی سی
Hadi Ahmadi Learning Java Script
ضوا هی تَاًیذ:
در داخل صفحات ایي کتاب آرم یا لَگَي خَد را قزار دّیذ .
در آخز تا تَجِ تِ سحوات تسیار سیادی کِ در ًگارش ایي کتاب کطیذُ ضذُ است ,تِ ًام خَد
دَلران پی سی
Hadi Ahmadi Learning Java Script
دَلران پی سی
Hadi Ahmadi Learning Java Script
تَجِ
هطالعِ ایي کتاب تزاي افزاد هثتذي کِ ّیچ آضٌایی تا تزًاهِ ًَیسی جاٍا اسکزیپت ًذارًذ ٍ
ّوچٌیي افزادي کِ تا سطح هتَسط تا ایي ستاى آضٌایی دارًذ تَصیِ هی ضَد.
دَلران پی سی
Hadi Ahmadi Learning Java Script
1 هقذهَ
دَلران پی سی
Hadi Ahmadi Learning Java Script
27 رّیذادُا
51 THE ABORT EVENT (ONABORT)
21 رّیذادُای هزبْط بَ هاّص
58 THE CLICK EVENT (ONCLICK)
59 THE DOUBLECLICK EVENT (ONDBCLICK)
59 THE MOUSEOVER EVENT (ONMOUSEOVER)
62 THE MOUSEOUT EVENT (ONMOUSEOUT)
62 THE MOUSEDOWN EVENT (ONMOUSEDOWN)
62 THE MOUSEUP EVENT (ONMOUSEUP)
62 THE MOUSEMOVE EVENT (ONMOUSEMOVE)
33 رّیذادُای صفحَ کلیذ
63 THE KEYDOWN EVENT (ONKEYDOWN)
63 THE KEYPRESS EVENT (ONKEYPRESS)
63 THE KEYUP EVENT (ONKEYUP)
32 : رّیذادُای هزبْط بَ فزم
65 THE FOCUS EVENT (ONFOCUS)
65 THE BLUR EVENT (ONBLUR)
66 THE CHANGE EVENT (ONCHANGE)
66 THE SUBMIT EVENT (ONSUBMIT)
66 THE RESET EVENT (ONRESET)
61 THE SELECT EVENT (ONSELECT)
31 رّیذادُای پٌجزٍ هزّرگز
69 THE LOAD EVENT (ONLOAD)
12 THE UNLOAD EVENT (ONUNLOAD)
12 THE RESIZE EVENT(ONRESIZE)
12 THE SCROLL EVENT(ONSCROLL)
دَلران پی سی
Hadi Ahmadi Learning Java Script
11 PROPERTIES
13 هتذُا
84 THE HISTORY OBJET
121 PROPERTIES_خصْصیات
دَلران پی سی
Hadi Ahmadi Learning Java Script
دَلران پی سی
Hadi Ahmadi Learning Java Script
121 PROPERTIES
258 استفادٍ اس ویژگی ُا
121 METHODS
262 هتذُای اصلی
262 هتذُای دو پاراهتزی
263 دیگز هتذُا
132 اشیای ػذدی
265 ویژگی ُا
265 هتذُا
137 اشیای سهاى
261 ویژگی ُا
268 هتذُا
215 دیگز هتذُا
دَلران پی سی
Hadi Ahmadi Learning Java Script
دَلران پی سی
Hadi Ahmadi Learning Java Script
دَلران پی سی
Hadi Ahmadi Learning Java Script
هقذهِ
وشبثی و ٝدیً ـ ٚؼاـیؽ ٚیفایً ؼ ْٚوشبثی اوز ثب ٕٞیٗ ػٛٙاٖ و ٝؼـ سبثىشبٖ ٍ٘ ,.4-بـي ٚـاٞی
ایٙشف٘ز ٌؽ) ؼـ اثشؽا لّؽ ثف ٌ٘ٛشٗ وشبثی ٔدكا اق وشبة ا َٚثٛؼ ِٚی ثب ٔٛـؼ سٛخ ٝلفاـ ٌففشٗ ٚیفایً اَٚ
ایٗ وشبة سّٕیٓ ث ٝثىٗ ٌ ٚىشفي آٖ ٕ٘ٛؼْ ٚضبَ ثؼؽ اق ٌؿٌز زٙؽ ٔب ٜوبـ سؽٚیٗ آٖ ـا ث ٝدبیبٖ
ـوب٘ؽْ) ایٗ ٚیفایً و ٝفمٗ ؼـ ػٛٙاٖ ثب ٚیفایً أٍ َٚشفن اوز ثٛ٘ ٝػی وشبثی ؼیٍف اوز ثب وبغشبـی
سبق ٜسف ٚثب ـػبیز وّىّٔ ٝفاسجی و ٝؼـ ٔٙبثغ آٔٛقٌی السیٗ خبٚا اوىفیذز ٔٛخٛؼ اوز ٚث ٝؼالیُ قیف
سّٕیٓ ث ٝا٘شٍبـ آٖ ٌففشٓ5
-1وثًدن یک مىثع جامع کٍ تیشتر سرفصل َای جايا اسکریپت را شامل شًد.
-2تعذد مىاتع فارسی کٍ تٍ دلیل وذاشته ساختار ي سلسلٍ مراتة مىاسة تیشتر تاعث سردرگمی خًاوىذٌ می
شًد.
-3التیه تًدن مىاتع جامع مًجًد کٍ تری کساوی کٍ تٍ ایه زتان تسلط وذاروذ فرایىذ یادگیری را سخت ي کسل
کىىذٌ می کىىذ.
-4ي . . .
أیؽ اوز و ٝثب ا٘شٍبـ ایٗ وشبة ٌبٔی وٛزه ؼـ آٔٛقي ٌ ٚىشفي ثف٘بٔٛ٘ ٝیىی ٚة ثفؼاٌش ٝثبٌٓ)
ثب سٍىف
ٞبؼی اضٕؽی
ٌٟفیٛـ ,.4/
HadiAhmadi105@gmail.com
HTTP://www.DLP.lxb.ir
خبٚا اوىفیذز قثب٘ی اوز و ٝؼـ ِفطبر HTMLوبـثفؼ ؼاـؼ ٚلبثّیز ٞبی ثىیبـی اق لجیُ 5الكایً
ویفیز ٘فاضی ِفطبر،وٙشفَ ِفطبر ،وٙشفَ ٔفٚـٌف ثبقؼیؽ وٙٙؽ،ٜوبغز ٚاوشفبؼ ٜاق وٛوی ٞب ٚ
ثىیبـی زیك ٞبی ؼیٍف) خبٚا اوىفیذز یه قثبٖ ثف٘بٔٛ٘ ٝیىی اوىفیذشی اوز و ٝوؽ ٞبی آٖ ثىیبـ ٌجیٝ
#اوز)قثبٖ خبٚا اوىفیذز ث ٝضفٚف ثكـي ٚوٛزه ضىبن اوز ٚیه قثبٖ ثف٘بٔٛ٘ ٝیىی C ث ٝقثبٖ $
ٌیٍفا ٔی ثبٌؽ) ث ٝایٗ ٔؼٙب وٞ ٝف ػّٙف ؼـ ِفطٚ ٝة ـا ث ٝؼیؽ یه ٌی ٔی ثیٙؽ ٚثب آٖ ـفشبـ ٔی وٙؽ)
اق آ٘دبیی و ٝؼوشٛـار خبٚا اوىفیذز ٔب٘ٙؽ ؼوشٛـار ٘ ، HTMLیبق ثٔ ٝطیٗ غبِی ثفای ٌ٘ٛشٗ
٘ؽاـؼ ٚثفای ٌ٘ٛشٗ ؼوشٛـار آٖ ٔی سٛاٖ اق ٞف ٚیفایً ٌف ٔشٙی اوشفبؼ ٜوفؼ) أب ٘فْ افكاـ ٞبی قیبؼی ٓٞ
ٚخٛؼ ؼاـؼ و ٝثفای ٌ٘ٛشٗ وؽٞبی Java Scriptأىب٘بسی ـا ؼـ اغشیبـ ٔب لفاـ ٔی ؼٙٞؽ)ٔب٘ٙؽ ـٍ٘ی وفؼٖ
ؼوشٛـار ٚدیؽا وفؼٖ غٙبٞب … ٚاق خّٕ ٝایٗ ٘فْ افكاـٞب ٔیشٛاٖ ٘ dreamweaverبْ ثفؼ)
ثٛ٘ ٝـ وّی ث ٝزٟبـ ٘فیك ٔی سٛاٖ اق خبٚا اوىفیذز ؼـ اوٙبؼ HTMLاوشمبؼ ٜوفؼ
اوشفبؼ ٜاق وؽٞبی خبٚا اوىفیذز ؼـ ؼـ ٖٚثفزىت ٞبی $ HTMLاوىفیذز ٘ٛیىی #Inline (/
٘ىشٌٕ 5ٝب ٔی سٛا٘یؽ اق ٕٝٞـٚي ٞبی ثبال ثٛ٘ ٝـ ٕٞكٔبٖ ثفای ٚاـؼ وفؼٖ خبٚا اوىفیذز ؼـ ِفطٝ
اوشفبؼ ٜوٙیؽ )
٘ىش 5 ٝوؽٞبیی ؤ ٝی غٛاٞیؽ ثب ـٚیؽاؼ غبِی اخفا ٌ٘ٛؽ ـا ؼـ ایٗ لىٕز ٔی ٘ٛیىیؽ)
<html>
<head>
<title>untitled document</title>
<script type=”text/javascript”>
دستورات جاوا اسکزیپت
</script>
</head>
<body>
</body>
</html>
5 ,(, َٔثب
<html>
<head>
<title>untitled document</title>
<script type=”text/javascript”>
document.write(“hello world”)
</script>
</head>
<body>
</body>
</html>
)ؽ ٌؽٞاٛ غٜٔیص ؼاؼَٛ ثؼؽی سّٛ ؼـ فdocument.write ـٛـؼ ؼوشٛٔ ؼـ5 ٝ٘ىش
)یىیؽٛ٘ ٘ؽ ـا ؼـ ایٗ لىٕز ٔیٌٛ اخفاٝیؽ ثب ثبـٌكاـی ِفطٞاٛ ٔیػٝبیی وٞوؽ
4 دَلران پی سی
Hadi Ahmadi Learning Java Script
><html
<>head
<>title>my page</title
<>/head
<>body
>”<script type=”text/javascript
دستورات جاوااسکزیپت
<>/script
></body
<>html
ٔثبَ 5 ,(-
><head
><title>my page</title
<>/head
<>body
>”<script type=”text/javascript
)”Alert(“hello world
<>/script
></body
<>html
٘ىش 5 ٝؼـ ٔٛـؼ ؼوشٛـ alertؼـ فّ َٛثؼؽ سٕیص ؼاؼ ٜغٛاٞؽ ٌؽ)
ٔثبَ 5,(.
اثشؽا ٘ٛر دؽ غٛؼ ـا ثبق وٙیؽ ٚوذه ؼوشٛـار قیف ـا ؼـ آٖ ٚاـؼ وٙیؽ 5
;)”Document.write(“hello wold
jsfile1.js َـ ٔثبٛٙیؽ ثٙ وٜ ؾغیف.js ٘ؽٛ دىٚ ٜاٛوذه فبیُ ـا ثب اوٓ ؼِػ
5یؽٙ وٝؼ أبفٛ غhtml ؽٙ وٝـ قیف آٖ ـا ثٛ اق ؼوشٜوذه ثب اوشفبؼ
<html>
<head>
<title>my page name</title>
<script type=”text/javascript” src=”jsfile1.js></script>
</head>
<body>
</body>
</html>
) ا٘ؽٜ ٌؽِٜؽـ ؾغیفٛ ؼـ یه فjsfile1.js ُ فبیٚ html ُ فبیٝ وٜؼٛ ؼـ ٔثبَ ثبال ففْ ثفایٗ ث5 , ٝ٘ىش
)ؼٌٛ ٔیٜی اوشفبؼٞ وؽ ثبال ثفای آؼـن ؼٝؼٙ ؼـ لsrc اق5 - ٝ٘ىش
#Inline یىیٛ٘ اوىفیذز$ HTML بیٞ ٖ ثفزىتٚا اوىفیذز ؼـ ؼـٚبی خبٞ اق وؽٜاوشفبؼ
5,(/ َٔثب
>html<
>head<
>title<my page</title>
</head>
<body>
<input type=”button” value=”ok” onclick=alert”(„hello world‟)”
/>
</body>
>/html>
)ؼٌٛ ٔیٜ ٕ٘بیً ؼاؼhello world ٖٕٕٛٔ ٍؽاـی ثبٞ ْ دیبok ٕٝی ؼوٚثب وّیه ثف ـ
6 دَلران پی سی
Hadi Ahmadi Learning Java Script
ٌ٘ٛشٗ وبـوشف” ; ” ویٕیى ِٖٛٛؼـ ا٘شٟبی ؼوشٛـ اخجبـی ٘یىز أب ؼـ ٔٛالؼی وٌٕ ٝب زٙؽ ؼوشٛـ (-
ـا ؼـ یه غٗ ٔی ٘ٛیىیؽ ٌ٘ٛشٗ وٕیىبِ ٖٛاخجبـی اوز سب دبیبٖ ٞف ؼوشٛـ ٍٔػُ ٌٛؼ
ثفای ؼـج سٔٛیطبر زٙؽ غٙی ؼـاثشؽای آٖ * ٚ %ؼـا٘شٟبی آٖ *%اوشفبؼٔ ٜی ٌٛؼ) (/
ث ٝوبـثفؼٖ فبِّ ٝغبِی ؼـ وؽٞب اق وٛی ٔفٚـٌف ٘بؼیؽ ٌففشٔ ٝی ٌٛؼ) (0
وبٔٙشٟب ٔی سٛا٘ٙؽ یه سٔٛیص ـا ث ٝخبٚا اوىفیذز أبف ٝوٙٙؽ ٚیب آٖ ـا غٛا٘ب سف وٙٙؽ) وبٔٙشٟب ٞیسٍبٜ
اخفا ٕ٘یٍ٘ٛؽ ٚثیٍشفیٗ اوشفبؼ ٜآٖ ثفای غٛؼ ثف٘بٔٛ٘ ٝیه اوز زفاو ٝثف٘بٔٛ٘ ٝیه ٔیشٛا٘ؽ ثب افكٚؼٖ
سٔٛیطبر ث ٝوؽ غٛا٘بیی ٚؼـن آٖ ـا ثفای غٛؼ ٚوبیف ثف٘بٔٛ٘ ٝیىبر ثیٍشف وٙؽ!
ٔثبَ 5,(0
ایٗ ٔثبَ اق وبٔٙشٟبی یه غٙی ثفای سٔٛیص وؽٞب اوشفبؼ ٜوفؼ ٜاوز)
>”<script type=”text/javascript
//this will write a header
;)”>document.write(“<h1>this is a header</h1
//this will write two paragraphs
;)”>document.write(“<h1>this is a paragraph</h1
;)”>document.write(“<h1>this is a another paragraph </h1
></script
5,(1 َٔثب
)ٜ وفؼٜب اوشفبؼٞٔیص وؽٛی ثفای سٙؽ غٙبی زٟشٙٔایٗ ٔثبَ اق وب
<script type=”text/javascript”>
/*
the code below will write
one header and two paragraphs
*/
document.write(“<h1>this is a header</h1>”);
document.write(“<h1>this is a paragraph</h1>”);
document.write(“<h1>this is a another paragraph </h1>”);
</script>
5,(2 َٔثب
<script type=”text/javascript”>
document.write(“<h1>this is a header</h1>”);
document.write(“<h1>this is a paragraph</h1>”);
// document.write(“<h1>this is a another paragraph
</h1>”);
</script>
8 دَلران پی سی
Hadi Ahmadi Learning Java Script
5,(3 َٔثب
)ؽٌٙیفی ٔی وّٛی خٙؽ غٙ اق اخفای یه وؽ زٝز اوز وٙٔایٗ ٔثبِی اق وب
<script type=”text/javascript”>
/*
document.write(“<h1>this is a header</h1>”);
document.write(“<h1>this is a paragraph</h1>”);
document.write(“<h1>this is a another paragraph </h1>”);
*/
</script>
ٚ ٔیطبر ثیٍشفٛب ثفای سٟشٙٔ ؼـ آغف یه غٗ لفاـ ؼاـؼ ' ایٗ وبٝز اوز وٙٔ ای اق یه وبٕٝ٘ٛ٘ ٗای
)٘ؽٚ ثىبـ ٔی ـٝٔبی ثف٘بٞا٘بسف ٌؽٖ وؽٛغ
5,(4 َٔثب
<script type=”text/javascript”>
document.write(“hello”) // this will write “hello”
document.write(“dolly”) // this will write “dolly”
</script>
9 دَلران پی سی
Hadi Ahmadi Learning Java Script
11 دَلران پی سی
Hadi Ahmadi Learning Java Script
ٔشغییفٞب ٔىب٘ی ؼـ ضبفظٞ ٝىشٙؽ و ٝثفای ٍٟ٘ؽاـی ٚؾغیفٔ ٜمبؼیف ا٘ٛاع ٔػشّف ؼاؼٞ ٜب ٔٛـؼ اوشفبؼٜ
لفاـ ٔی ٌیف٘ؽ)
ٔشغییف ٞب ا٘ٛاع ٔػشّفی ؼاـ٘ؽ ٚؼـ اوثف قثبٟ٘بی ثف٘بٔٛ٘ ٝیىی ثبیؽ ٞف ٘ٛع ؼاؼ ٜـا ؼـ ٔشغییف آٖ ؼاؼٜ
اوشفبؼ ٜوٙیؽ ٚلجُ اق سؼفیف ٔشغییف ثبیؽ ٘ٛع ٔشمییف ـا ٍٔػُ وٙیؽ أب ؼـ خبٚا اوىفیذز ٌٕب ٘یبقی
٘ؽاـیؽ ٘ٛع ٔشمییف ـا ٍٔػُ وٙیؽ ثؼؽ اق ٚـٚؼ ؼاؼ ٜغٛؼ خبٚا اوىفیذز ٘ٛع ٔشغییف ـا ٍٔػُ ٔی وٙؽ)
ؼـ خبٚا اوىفیذز ثفای سؼفیف ٔشغییف اق وّٕ ٝوّیؽی varؤ ٝػفف ( ) variableثٔ ٝؼٙی ٔشغییف
اوشفبؼٔ ٜی ٌٛؼ ٚثؼؽ اق ایٗ وّٕ٘ ٝبٔی و ٝثفای ٔشغییف ؼـ ٘ظف ٔی ٌیفیؽ ٚوبـ سٍػیُ ٘ٛع ٔشغییف ثٝ
ػٟؽٔ ٜفىف خبٚا اوىفیذز اوز)
;Var variablename
ٔثبَ 5-(,
;Var I
٘ىش 5 ٝاوشفبؼ ٜاق ؤّٕ var ٝفٚـی ٘یىز ٚث ٝغٛا٘ب سف ٌؽٖ وؽٞب وٕه ٔی وٙؽ)
ٔثبَ 5-(-
;Var I = 10
;Var j = 12
undefined
ٔثبَ 5-(.
;var test
Boolean
ایٗ ٘ٛع ٔی سٛا٘ؽ ٔفبؼیف ٌف٘ی ـا ؼـ غٛؼ ؾغیف ٜوٙؽ ٔب٘ٙؽ true , false 5و ٝؼـ ػّٕیبسٟبی ٌف٘ی
وبـثفؼ ؼاـؼ)
null
٘ َٛثؽیٗ ٔؼٙبوز ؤ ٝشغییفٔفثٞ ٝ٘ٛر ٔمؽاـی ٘ؽاـؼ) ثؽیٗ ٔؼٙب وِ ٝفف ٚیب فٕبی غبِی ٘یىز '
ثّى ٝؼـ ضمیمز ٞیر اوز)
ٔثبَ 5-(/
Character
ایٗ ٘ٛع فمٗ یه وبـوشف ـا ؼـ غٛؼ ؾغیفٔ ٜی وٙٙؽ ٔب٘ٙؽ…”E”,”X”'”&”'″/“ 5
number
ثفای ؾغیفٔ ٜفبؼیف ػؽؼ ِطیص ٔٛـؼ اوشفبؼ ٜلفاـ ٔی ٌیفؼ ٔب٘ٙؽ …ٚ(30ٚ ,0 ٚ ,-. 5
هثال :1-5
5-(1 َٔثب
var paycheck=1200;
var phonebill=29.99;
var savings=0;
var sparetime=-24.5;
Float
String
… ٚ ”hello world“ ٚ ” “والْ ؼ٘یب5 ؽٙ٘ؽ ٔبٙٙ ٔی وٜؼ ؾغیفٛ ای ـا ؼـ غٝع ـٌشٛ٘ عٛ٘ ٗای
5-(2 َٔثب
5-(2 َٔثب
var mycar="Corvette";
var oldcar="Big Brown Station Wagon";
var mycomputer="Pentium 3, 500 MHz, 128MB RAM";
var jibberish="what? cool! I am @ home 4 now. (cool, right?)";
14 دَلران پی سی
Hadi Ahmadi Learning Java Script
٘ (-بْ ٔشغییف ٕ٘ی سٛا٘ؽ ٌبُٔ فبِّ ٚ ٝیب ٘مٌ ٝٙؿاـی ثبٌؽ
(0خبٚا اوىفیذز ث ٝثكـٌی ٚوٛزىی ضفٚف ضىبن اوز ث ٝػٛٙاٖ ٔثبَ url :ثب Urlثب URLیىی
٘یىز ٔ ٚی سٛاٖ ٘بْ ؤ ٝشغییف خؽا اق ٓٞثبٌؽ)
ٔثبَ 5-(3
#paycheck
1paycheck
pay check
pay_check 2
_pay check
ٔثبَ 5-(4
paycheck
_paycheck
pay2check
pay_check
pay_245
ٔشغییفی و ٝؼـ ثیف ٖٚسٛاثغ سؼفیف ٌٛؼ ٔشغییف وفاوفی (ٔ )Globalی ثبٌٙؽ ٚسٕبْ اوىفیذشٟب ٚسٛاثغ
ؼـِ ٖٚفط ٝث ٝآٖ ؼوشفوی ؼاـ٘ؽ)
٘ىشٔ 5ٝشغییف ٔطّی ثؼؽ اق ثىشٌ ٝؽٖ ِفط ٝاق ثیٗ ٔی ـٚؼ)
اٌف ٌٕب ٔشغیفی ـا سؼفیف وٙیؽ ٚاق وّٕ ”var“ ٝؼـ یه سبثغ اوشفبؼ ٜوٙیؽ آٖ ٔشغیف فمٗ ؼاغُ ٕٞبٖ
سبثغ ؼـ ؼوشفن اوز) ٚلشی ٌٕب اق یه سبثغ غبـج ٌٛیؽ آٖ ٔشغیف اق ثیٗ ٔی ـٚؼ) ایٗ ٔشغیفٞب ٔشغیفٞبی
ٔطّی ٘بٔیؽٔ ٜیٍ٘ٛؽ) ٌٕب ٔی سٛا٘یؽ ٔشغیفٞبی ٔطّی ٘ ٓٞبٔی ـا ؼـ سٛاثغ ٔػشّف اوشفبؼ ٜوٙیؽ قیفا ایٗ
ٔشغیفٞب فمٗ ؼـ ٕٞبٖ سٛاثؼی و ٝسؼفیف ٌؽ ٜا٘ؽ لبثُ ٌٙبوبیی ٞىشٙؽ)
اٌف ٌٕب ٔشغیفی ـا غبـج یه سبثغ سؼفیف وٙیؽ آٖ ٔشغیف ؼـ وُ ِفط ٝسبٖ ؼـ ؼوشفن غٛاٞؽ ثٛؼ) ایٗ
ٔشغیفٞب قٔب٘ی و ٝسؼفیف ٔی ٌ٘ٛؽ ٌفٚع ٌؽٚ ٚ ٜلشی وِ ٝفط ٝـا ٔی ثٙؽیؽ دبیبٖ ٔی یبثٙؽ)
ؼـ لٙؼ ٝوؽ قیف ٌٕب ٘طِ ٜٛؽا وفؼٖ یه ٔشغیف ثٚ ٝویّ ٝسبثغ document.writeـا غٛاٞیؽ
آٔٛغز)
ٔثبَ 5-(,+
>"<script language="JavaScript
;"var mycar="Corvette
;)document.write(mycar
></script
ٌٕب ٔیٛا٘یؽ لٙؼ ٝوؽ ثبال ـا ثِٛ ٝـسٟبیی و ٝؼـ ٔجبضث لجُ ٌفشٌ ٝؽ ؼـ وٙؽ htmlغٛؼ ثىبـ ثفؼٚ ٜ
اخفا ٕ٘بییؽ)
فبـؽ اق سٍٟبی scriptؼـ اثشؽا ٔب ٔمؽاـ corvetteـا ثٔ ٝشغیف ٘ mycarىجز ٔیؽٞیٓ ' وذه ثٝ
ٚویّ ٝسبثغ )(ٔ document.writeشغیف mycarـا ففاغٛا٘ی ٔی وٙیٓ و ٝؼـ اُِ ٔمؽاـ آٖ یؼٙی
corvetteـا ثفای ٔب ٕ٘بیً ٔی ؼٞؽ)
ٔثبَ 5-(,,
>"<script type="text/javascript
;"var mycar="Corvette
;)document.write("I like driving my "+mycar
></script
٘شید5 ٝ
ٔمؽاـٔشغیفٚ I like driving my لىٕزٚ٘ؽ ؼاؼٖ ؼٛ" ثفای دی+" اق ػٍّٕفٜ ٔثبَ ثبال اوشفبؼٝب ٘ىشٟٙس
)ؽ ٌؽٞاٛ غٜٔیص ؼاؼٛـؼ آٖ سٛٔ ؼـ ٔجبضث ثؼؽ ؼـٝ ٔی ثبٌؽ وcorvette یٙ یؼmycar
5-(,- َٔثب
<script type="text/javascript">
var mycar="Corvette";
document.write("I like driving my "+mycar+" every day!");
</script>
5 ٝ٘شید
)ؼٌٛ ٔیٜـر ٕ٘بیً ؼاؼِٛ ٖٕبٞ ٝسیٍٗ" ٔی آیؽ ؼلیمب ثٛػالٔز" وٚ ثجٗ ؼٝ زیكی وٝیؽ وٙؼلز و
5 یؽٙ وٝخٛع سٛٔٛٔ ٗ ثب ایٝٙ ٔثبَ قیف ؼـ ـاثٝث
5-(,. َٔثب
<script type="text/javascript">
var mycar="Corvette";
document.write("I like driving my +mycar+ every day!");
</script>
5ٝ٘شید
ثاتت ّا
ب ؼـ ایٗ اوزٞ ب ثب ٔشغییفٞ ر ثبثزٚؽ)سفبٙٙ سؼفیف ٔی وConst وّیؽیّٕٝ اق وٜب ـا ثب اوشفبؼٞ ثبثز
اٖ ایٗ ٔمؽاـ ـا سغییفٛ ؼـخبیی ؼیٍف ٕ٘ی سٚ #یه ٔفؽاـ ثبثز$ ؽ فمٗ یه ٔمؽاـ ثٍیف٘ؽٙ٘اٛب ٔی سٞ ثبثزٝو
18 دَلران پی سی
Hadi Ahmadi Learning Java Script
ؼاؼ )یفای ٔثبَ ؼـ یه ثف٘بٔ ٝثفای ثؽوز آٚـؼٖ ٔىبضز ؼایف ٜث ٝػؽؼ ٘ .٫,/یبق ؼاـیٓ ٚایٗ ػؽؼ ٕٛٞاـٜ
ثبثز اوز ٚؼـ ٘ َٛثف٘بٔٔ ٝمؽاـ pفبثُ سغییف ٘یىز)
ٔثبَ 5-(,/
;const p = 3.14
خبٚا اوىفیذز ٘یك ٔب٘ٙؽ اوثف قثبٟ٘بی ثف٘بٔٛ٘ ٝیىی اق لبثّیز سجؽیُ ا٘ٛاع ثفغٛـؼاـ اوز ٚـٌٟٚبی
وبؼ ٜای ـا ثفای اوشفبؼ ٜاق ایٗ لبثّیز ففا ٓٞآٚـؼ ٜاوز)
تثذیل تِ رضتِ
وٛ٘ ٝع ؼاؼٔ boolean , number & string ٜشؽی ث٘ ٝبْ ) ( .tostringثفای سجؽیُ ث ٝـٌشٝ
ؼاـ٘ؽ)
ایٗ ٔشؽ ثفای ٔشغیفٞبی اق ٘ٛع booleanیىی اق ٔمبیف ـٌش ٝای trueیب falseـا ثىش ٝثٔ ٝمؽاـ ٔشغیف
ثفٔی ٌفؼا٘ؽ5
ٔثبَ 5-(,0
ایٗ ٔشؽ ثفای ٔشغیفٞبی اق ٘ٛع numberـٌش ٝای ضبٚی آٖ ػؽؼ ـا ثف ٔی ٌفؼا٘ؽ5
ٔثبَ 5-(,1
تثذیل تِ ػذد
خبٚااوىفیذز ؼٔ ٚشؽ ثفای سجؽیُ ا٘ٛاع غیف ػؽؼی ث ٝػؽؼی ففا ٓٞوفؼ ٜاوز:
) (parseInt
) (parseFloat
ایٗ ٔشؽ ٞب فمٗ ثف ـٚی ـٌشٞ ٝبی ضبٚی ػؽؼ وبـ ٔی وٙٙؽ ٚثف ـٚی ثمی ٝا٘ٛاع ٔمؽاـ NaNـا ثفٔی
ٌفؼا٘ٙؽ)
هتذ ) (parseInt
اق اِٚیٗ وبـاوشف ـٌشٌ ٝفٚع ٔی وٙؽ اٌف ػؽؼ ثٛؼ آٖ ـا ثف ٔی ٌفؼا٘ؽ ؼـ غیف ایٗ ِٛـر ٔمؽاـ
NaNـا ثفٔی ٌفؼا٘ؽ ایٗ ـ٘ٚؽ سب آغفیٗ وبـاوشف اؼأ ٝدیؽا ٔیىٙؽ سب ایٙى ٝث ٝوبـاوشفی غیف ػؽؼی ثفوؽ .
ث ٝػٛٙاٖ ٔثبَ ایٗ ٔشؽ ػجبـر "”123redـا ثِٛ ٝـر 123ثفٔی ٌفؼا٘ؽ)
ٔثبَ 5-(,2
هتذ )(parseFloat
ٔثُ ٔشؽ )( parseIntػُٕ وفؼ ٚ ٜاق اِٚیٗ وبـاوشف ٌفٚع ث ٝخىز ٚخٔ ٛی وٙؽ اِجش ٝؼـ ایٗ ٔشؽ اٚایٗ
وبـاوشف ٘م ٝٙضىبة ٕ٘ی ٌٛؼ ٚآٖ ـا ثٕٞ ٝبٖ ِٛـر ثفٔی ٌفؼا٘ؽ)
اٌف ؼ ٚوبـاوشف ٘م ٝٙؼـ ـٌشٚ ٝخٛؼ ؼاٌش ٝثبٌٙؽ ؼٔٚیٗ ٘م ٝٙث ٝػٛٙاٖ وبـاوشف ثی اـقي ٌٙبغشٔ ٝی ٌٛؼ ٚ
ػّٕیبر سجؽیُ ٔشٛلف ٔی ٌٛؼ
ؼـ خبٚااوىفیذز أىبٖ اوشفبؼ ٜاق ـٌٚی ٔٛو ْٛث Type Casting ٝثفای سجؽیُ ا٘ٛاع ٚخٛؼ ؼاـؼ) وٛ٘ ٝع
type castingؼـ خبٚااوىفیذز ٚخٛؼ ؼاـؼ:
)( Boolean
)( Number
)( String
سبثغ )( Booleanقٔب٘ی ٔمؽاـ trueـا ثف ٔی ٌفؼا٘ؽ و ٝدبـأشف ؼـیبفشی اي ،ـٌش ٝای ٌبُٔ ضؽالُ
یه وبـوشف ،یه ػؽؼ غیف اق ِفف ٚیب یه ٌی ثبٌؽ ٔ.مؽاـ falseـا ٘یك قٔب٘ی ثف ٔی ٌفؼا٘ؽ و ٝدبـأشف
ؼـیبفشی اي ـٌش ٝای سٟی ،ػؽؼ ِفف یب یىی اق ٔمبؼیف null ٚ undefinedثبٌؽ5
ٔثبَ 5-(,4
سبثغ )( Numberوبـی ٌجی parseFloat() ٚ parseInt() ٝـا ا٘دبْ ٔی ؼٞؽ أب سفبٚر ٞبیی ٓٞ
ؼاـؼ.
اٌف ث ٝیبؼ ؼاٌش ٝثبٌیؽ ٔشؽٞبی )( parseFloat() ٚ parseIntآـٌٔٛبٖ ؼـیبفشی ـا فمٗ سب اِٚیٗ
وبـاوشف ثی اـقي ثف ٔی ٌفؼا٘ؽ ٔثال ـٌش ” 4.5.6 “ ٝث 4.5 ٝسجؽیُ غٛاٙٞؽ وفؼ .أب وبـثفؼ ٔشؽ
)( Numberثفای ایٗ ـٌشٔ ٝمؽاـ NaNـا ثفٔی ٌفؼا٘ؽ قیفا ایٗ ـٌش ٝؼـ وُ ،اق ٘ظف ٔشؽ )(Number
أىبٖ سجؽیُ ث ٝیه ػؽؼ ـا ٘ؽاـؼ.
اق یىی اقٜ ثفای اوشفبؼ،ؼٛ غNumber() ثبٌؽ ٔشؽٝ یه ػؽؼ ـا ؼاٌشٝ ای أىبٖ سجؽیُ ثٝاٌف ـٌش
ثفایNumber() ؼـ ٔثبَ قیف ضبُِ اخفای سبثغ. سّٕیٓ ٔی ٌیفؼparseFloat() یبparseInt() اثغٛس
:ؽٞب ـا ٍ٘بٖ ٔی ؼٞ ٜاع ؼاؼٛ٘ا
5-(-+ َٔثب
Number(false) 0
Number(true) 1
Number(undefined) NaN
Number(null) 0
Number(“5.5”) 5.5
Number(“56”) 56
Number(“5.6.7”) NaN
Number(new Object()) NaN
Number(100) 100
: ثف ٔی ٌفؼا٘ؽٝاٖ ـٌشٛٙ ػٝ ٔی ٌیفؼ ثٕٝبٖ زیكی ـا وٞ ٝ اوز وString() ٓٞ سفیٗ سبثغٜوبؼ
5-(-, َٔثب
22 دَلران پی سی
Hadi Ahmadi Learning Java Script
23 دَلران پی سی
Hadi Ahmadi Learning Java Script
24 دَلران پی سی
Hadi Ahmadi Learning Java Script
یه سبثغ یه اوىفیذز دبی ٝاوز ؼـ ؼـ ٖٚاوىفیذز ثكـٌشف)وٚ ٝظیف ٝآٖ ا٘دبْ وبـ یب ٔدٕٛػ ٝای اق
وبـٞب اوز و ٝث ٝآٖ ٔطٔ َٛی ٌٛؼ ثفای ٔثبَ ٕٔىٗ اوز ٚظیفٌٛ٘ ٝشٗ یه غٗ ؼـ ٔفٚـٌف یب ٔطبوجٝ
یه ففٔ ٚ َٛیب غیف ٜـا ؼاٌش ٝثبٌؽ و ٝؼـ ٟ٘بیز ٘شید ٝـا ث ٝاوىفیذز اِّی ثف ٔی ٌفؼا٘ؽ)
سبثغ ٔدٕٛػ ٝؼوشٛـاسی اوز و ٝسٟٙب ؼـ ِٛـر ـظ ؼاؼٖ یه ـٚیؽاؼ ٚیب اق ٘فیك ففاغٛا٘ی اخفا ٔی
ٌ٘ٛؽ) ٌٕب ٔی سٛا٘یؽ ٞف سبثغ ـا اق ٞف خبیی ؼـ ِفط ٝففاغٛا٘ی وٙیؽ $یب ضشی اق ِفطٞ ٝبی ؼیٍف ؼـ
ِٛـسی و ٝسٛاثغ ـا ؼـ یه فبیُ )jsغبـخی ؾغیف ٜوفؼ ٜثبٌیؽ)#
اِّی سفیٗ ؼِیُ ثفای اوشفبؼ ٜاق سٛاثغ' ا٘ؼٙبف دؿیفی ٚلؽـسی اوز و ٝؼـ ٌ٘ٛشٗ وؽٞب ث ٝثف٘بٔٛ٘ ٝیه
ٔی ؼٞؽ سب ثشٛا٘ؽ وؽٞب ٚثف٘بٔ ٝغٛؼ ـا ثٟشف ٔؽیفیز وٙؽ ٕٞ ٚسٙیٗ غٙبیبثی ـا ثٟشف ٚوفیؼشف ا٘دبْ ؼٞؽ)
ؼـ غٗ ا َٚاق وبغشبـ یه سبثغ ٌٕب ثبیؽ یه اوٓ ثفای آٖ ا٘شػبة وفؼ ٚ ٜآٖ ـا ث ٝثف٘بٔ ٝغٛؼ ثٍٙبوب٘یؽ
ٔب٘ٙؽ 5
{)(function functionname
}
ٔثبَ 5.(,
{)(function reallycool
}
function functionname(){
javascript code here
}
5.(- َٔثب
function reallycool(){
a = 5; var b =3;
var c = 8; }
5.(. َٔثب
<head>
<title>Untitled Document</title>
<script type="text/javascript">
function dismsg()
{
alert("hello");
}
</script>
</head>
<body>
<form>
<input type="button" value="test" onclick="dismsg()" />
</form>
</body>
</html>
لشی اخفا ٔیٚ dismsg)( ؼ) سبثغٌٛ ؽ اخفا ٕ٘یٙ وّیه وٕٝی ؼوٚ وبـثف ـٝىٙـ لجُ اق ایٛایٗ ؼوش
)ؽٙ وّیه وٕٝی ؼوٚ وبـثف ـٝؼ وٌٛ
26 دَلران پی سی
Hadi Ahmadi Learning Java Script
ثفای ٘بْ ٌؿاـی سٛاثغ ثبیؽ اق ؼوشٛـار ٚلٛا٘یٙی دیفٚی وٙیؽ اق خّٕ ٝایٙى5 ٝ
وؼی وٙیؽ ٘بْ سبثغ ثب وبـی و ٝا٘دبْ ٔی ؼٞؽ ٕٞػٛا٘ی ٕٞ ٚبٍٙٞی ؼاٌش ٝثبٌؽ)
٘بْ سٛاثغ ضشٕب ثبیؽ ثب ضفٚف ٚیب ػالٔز"_" آغبق ٌٛؼ)
٘بْ سٛاثغ ٕ٘ی سٛا٘ؽ ٌبُٔ ٞفٌ ٝ٘ٛفٕبی غبِی ثبٌؽ)
ٔثبَ 5.(/
سبثغ قیف ثفای زبح ٕ٘ ٚبیً یه ٔشٗ ٌ٘ٛشٌ ٝؽ ٜاوز 5
{ )(function print_strong_text
document.write("<strong>This is a strong
;)">statement!</strong
}
ٕٞبٍ٘ ٝ٘ٛؤٍ ٝبٞؽٔ ٜی وٙیؽ ؼـ ٌ٘ٛشٗ ٘بْ سبثغ اق ٞف و ٝؼوشٛـ ثبال دیفٚی ٚسجؼیز ٌؽ ٜاوز)
دبـأشفٞب اوشفبؼٔ ٜی ٌ٘ٛؽ سب ث ٝسٛاثغ ایٗ لبثّیز ـا ثؽٙٞؽ و ٝیه یب زٙؽ ٔمؽاـ ـا اق خبیی اق غبـج سبثغ
ٚاـؼ وٙٙؽ )
)function functionname(variable1,variable2
ٔثبَ 5.(0
{ )function reallycool(coolcar,coolplace
JavaScript code here
}
5.(1 َٔثب
function reallycool(coolcar,coolplace) {
document.write("My car is a "+coolcar+" and I drive it to
"+coolplace);
}
ٝ وٝ ثبٌؽ آ٘سLas Vegas ثفاثفcoolplace ٔمؽاـCorvette ثفاثف ثبcoolcar ضبَ اٌف ٔمؽاـ
5ؼٛؽ ثٞاٛؼ غٗ قیف غٌٛ ٔیٜـٌف ٌٕب ٕ٘بیً ؼاؼٚؼـ ٔف
.(2 َٔثب
<head>
<title>Untitled Document</title>
<script type="text/javascript">
function test(a , b)
{
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(test(5,2));
</script>
</body>
</html>
ٗ ؼـ غٚ ٓ آٖ اغشّبَ ؼاؼیٝ ـا ثa,b ٔشغیفٚ ؼـ ؼاغُ دفا٘شك ؼٚ ٓؼـ ٔثبَ ثبال سبثغ سىز ـا لفاـ ؼاؼی
ٝ ثbody ٔی ثبٌؽ((( ؼـ لىٕزb ؼـa ـ ٔب ضبِّٕفةٛظٙٔ ٝ وفؼیٓ وٜ اوشفبؼreturn ـٛثؼؽ اق ؼوش
ؼـdocument.write ـٛوٗ ؼوشٛ سٝ ـا اغشّبَ ؼاؼیٓ و0 ٔمؽاـb ٔشغیفٝ ثٚ 3 ٔمؽاـa ٔشغیف
)ؼٌٛ ٔیٜخی ٕ٘بیً ؼاؼٚغف
28 دَلران پی سی
Hadi Ahmadi Learning Java Script
5.(3 َٔثب
function get_added_text() {
var textpart1="This is ";
var textpart2="fun!";
var added_text=textpart1+textpart2;
return added_text;
}
This is fun!
5.(4 َٔثب
5.(,+ َٔثب
function show_message() {
window.alert("This is an alert!");
}
show_message();
29 دَلران پی سی
Hadi Ahmadi Learning Java Script
5.(,, َٔثب
<head>
<title>Untitled Document</title>
<script type="text/javascript">
function test()
{
return ("hello world");
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(test());
</script>
</body>
</html>
5.(,- َٔثب
function show_message() {
window.alert("This is an alert!");
}
window.alert("I am first, ha!");
window.alert("I am second, ha ha!");
show_message();
5.(,. َٔثب
<script type="text/javascript">
function update_alert(){
window.alert("Welcome! This site is updated daily!");}
function call_alert() {
update_alert();}
call_alert();
</script>
31 دَلران پی سی
Hadi Ahmadi Learning Java Script
5.(,/ َٔثب
<script type="text/javascript">
function update_alert() {
window.alert("Welcome! This site is updated daily!");
}
function section_alert() {
window.alert("Please visit the picture section!");
}
function links_alert() {
window.alert("Also, check out my links page!");
}
function get_messages() {
update_alert();
section_alert();
links_alert();
}
get_messages();
</script>
5.(,0 َٔثب
<script type="text/javascript">
var mycar="Honda";
var paycheck=1200;
function new_car() {
mycar="Ferrari";
paycheck=3500;
window.alert("You need $"+paycheck+" to get a "+mycar);
}
new_car();
window.alert("You make $"+paycheck+" and have a "+mycar);
</script>
31 دَلران پی سی
Hadi Ahmadi Learning Java Script
5.(,1 َٔثب
اقٜ اوشفبؼَٜٛ لجُ ٘طّٛیؽ)ؼـ فٞؼ ا٘دبْ ؼٛا اوىفیذز غٚ فبیُ خبٝؼ اـخبػی ثٛ غhtml ُٖ فبیٚ)ؼـ ؼـ.
) اوزٜ ٌؽٜٔیص ؼاؼٛ سhtml ُبی اوىفیذز غبـخی ؼـ فبیٞ ُفبی
)یؽٙاـؼ وٚ ٖبی قیف ـا ؼـ آٞ وؽٚ ٜیفایٍٍف ٔشٗ ثبق وفؼٚ اق یهٜ ـا ثب اوشفبؼtest.js ُ)فبی/
function car_cost(mycar,paycheck) {
window.alert("You have a "+mycar+" and make $"+paycheck);
}
function get_added_text() {
var textpart1="This project ";
var textpart2="is almost fun!";
var added_text=textpart1+textpart2;
return added_text;
}
car_cost("Mustang",1500);
var alert_text=get_added_text();
window.alert(alert_text);
ٜؽٞ دیبْ قیف ـا ٍٔبٚیؽ ؼٙؼ ـا ثبق ٔی وٛ غhtml ُ ٌٕب فبیٝ قٔب٘ی وٚ ُ وفؼٖ فبیٜ)ثؼؽ اق ؾغیف1
5 ٔی ٕ٘بییؽ
32 دَلران پی سی
Hadi Ahmadi Learning Java Script
5 یؽٞـؼ ثفـوی لفاـ ؼٛٔ ٘شبیح ـاٚ یؽٙ وٜ ؾغیفhtml ُ وؽ قیف ـا ؼـ یه فبیٝؼٙی ؼیٍف لٙاٖ سٕفیٛٙ ػٝث
<body>
<h1>"Welcome to my Function Page," I said.</h1>
<script type="text/javascript">
function get_added_text(textpart1,textpart2) {
var added_text=textpart1+" "+textpart2;
return added_text;
}
function print_text() {
var myfood=get_added_text("cheese","bread");
document.write(myfood);
}
var alert_text=get_added_text("soup","crackers");
window.alert(alert_text);
print_text();
</script>
<p style="color:red">I'm seeing red!</p>
</body
33 دَلران پی سی
Hadi Ahmadi Learning Java Script
34 دَلران پی سی
Hadi Ahmadi Learning Java Script
ػولگزّا:فصل چْارم
35 دَلران پی سی
Hadi Ahmadi Learning Java Script
ؼـ خبٚا اوىفدز ثفای خٕغ ،سففیكٔ ،مبیى ٚ ٝثىیبـی اق ػّٕیبر ؼیٍف اق ػٍّٕفٞب اوشفبؼٔ ٜی وٙیٓ)
ػٍّٕفٞب ث ٝزٙؽ ؼوش ٝسمىیٓ ٔی ٌ٘ٛؽ 5ػٍّٕفٞبی ٔطبوجبسیٔ ،مبیى ٝای ،خبیٍكیٙیٙٙٔ ،می ،ـٌش ٝایٚ ،
ٌف٘ی) ؼـ اؼأ ٝث ٝاـائ ٝسٔٛیطبسی ثفای ٞف ٘ٛع اق ایٗ ػٍّٕفٞب غٛاٞیٓ دفؼاغز)
ػولگزّای هحاسثاتی
اق ایٗ ٘ٛع ػٍّٕفٞب ثفای ا٘دبْ ػّٕیبر ـیبٔی ٘ظیف خٕغ ٔ ٚفة اوشفبؼٔ ٜی ٌٛؼ) ٔی سٛا٘یؽ ایٗ
ػٍّٕفٞب ـا ؼـ ایٙدب ٍٔبٞؽ ٜوٙیؽ $ؼـ ٔثبَ قیف ٔشغیف xـا ثفاثف ٔ ٚ 3شغیف yـا ثفاثف /ؼـ ٘ظف ثٍیفیؽ 5 #
اق ػٍّٕفٞبی افكایً ٚوب ًٞث ٝسفسیت ثفای افكٚؼٖ ,ثٔ ٝمؽاـ لجّی ٚوٓ وفؼٖ ,اق ٔمؽاـ لجّی
اوشفبؼٔ ٜی ٌٛؼ)
ػولگزّای جایگشیٌی
ایٗ ػٍّٕفٞب سفویجی اق ػٍّٕفٞبی ؼیٍف ٞىشٙؽ ٚثٕٞ ٝیٗ ؼِیُ آٟ٘ب ـا ػٍّٕفٞبی سفویجی ٔ ٓٞی
٘بٔٙؽ) ٔی سٛا٘یؽ ایٗ ػٍّٕفٞب ـا ؼـ خؽ َٚقیف ٍٔبٞؽ ٜوٙیؽ $ؼـ ٔثبَ قیف ٔشغیف xـا ثفاثف ٔ ٚ 3شغیف yـا
ثفاثف /ؼـ ٘ظف ثٍیفیؽ 5 #
اق ایٗ ػٍّٕفٞب ثفای ٔمبیى ٝؼٔ ٚمؽاـ اوشفبؼٔ ٜی ٌٛؼ وٌ ٝبُٔ ػٍّٕفٞبی قیف ٞىشٙؽ)
x ؼـ خؽ َٚقیف ٔمبؼیف z ٚ y ٚxـا ٔٙبثك قیف ؼـ ٘ظف ثٍیفیؽ ٚسٛخ ٝؼاٌش ٝثبٌیؽ ؤ ٝمؽاـ ٔشغیفٞبی
y ٚػؽؼی ٔ ٚمؽاـ ٔشغیف zاق ٘ٛع ـٌش ٝای اوز 5
سفبٚر ػٍّٕف سىبٚی ثب ػٍّٕف ٕٞب٘ی ؼـ ایٗ اوز و ٝؼـ ػٍّٕف سىبٚی ٘ٛع ٔشغیف ٞب ؼـ ٘ظف ٌففشٕ٘ ٝی
ٌٛؼ ٚاٌف ٔمؽاـ ٔشغیفٞب ثب ٓٞثفاثف ثبٌؽ ضشی اٌف اق یه ٘ٛع ٘ ٓٞجبٌٙؽ ٘شید True ٝغٛاٞؽ ثٛؼ) ِٚی ؼـ
ػٍّٕف ٕٞب٘ی ثبیؽ ٔشغیفٞب ٔمؽاـی ٔىبٚی ؼاٌش ٝثبٌٙؽ ٚاق یه ٘ٛع ٓٞثبٌٙؽ سب ٘شید True ٝثبٌؽ)
ػولگزّای هٌطقی
ایٗ ػٍّٕفٞب ثفای ا٘دبْ ػّٕیبر ٔٙٙمی ثف ـٚی ؼ ٚػجبـر ث ٝوبـ ٔی ـ٘ٚؽ $ؼـ ٔثبَ قیف ٔشغیف xـا ثفاثف
ٔ ٚ 3شغیف yـا ثفاثف /ؼـ ٘ظف ثٍیفیؽ 5 #
اق ایٗ ػٍّٕف ثفای زىجب٘ؽٖ ؼ ٚـٌش ٝث ٓٞ ٝاوشفبؼٔ ٜی ٌٛؼ) ٔثالً ثفای اسّبَ ؼٔ ٚشغیف اق ٘ٛع ـٌش ٝاق
ایٗ ػٍّٕف اوشفبؼٔ ٜی ٌٛؼ)
ثفای ایدب فبِّ ٝثیٗ وّٕٚ ٝة ٚوّٕ ٝثب ٔی سٛا٘یٓ یه فبِّ ٝغبِی ث ٝیىی اق ٔشغیفٞبی aیب bأبفٝ
وٙیٓ یب ثب اوشفبؼ ٜاق ػّٕ٘ٛؽ ـٌش ٝای یه فٕبی غبِی ثٔ ٝشغیف cأبف ٝوٙیٓ 5
5یؽٙبی قیف أشطبٖ وٍٟا٘یؽ ثب لفاـ ؼاؼٖ ؼـثیٗ سٛ٘ؽ ـا ٔی سٌٛ ٔیٜـؼٚ ؼـ قیف آٝبیی وِٟسٕبٔی ٔثب
<script type="text/script>
هحل قزار گیزی کذُا
</script>
var thesum=4+7;
window.alert(thesum);
var num1=4;
5/(- َٔثب
var thesum=num1+7;
window.alert(thesum);
39 دَلران پی سی
Hadi Ahmadi Learning Java Script
5/(. َٔثب
var num1=4;
var num2=7;
var thesum=num1+num2;
window.alert(thesum);
5/(/ َٔثب
var num1=4.73;
var num2=7;
var thesum=num1+num2;
window.alert(thesum);
5/(0 َٔثب
var num1=4;
var num2="7";
var thesum=num1+num2;
window.alert(thesum);
ػٍّٕف خٕغٝ ؼـ ٘شیدٚ ٓ ایٜاـؼ وفؼٚ ٝاٖ یه ـٌشٛٙ ػٝ ـا ث7 اوز قیفا ٔب47 اة ٔثبَ آغفٛ خ5 ٝ٘ىش
)ؽٙٓ اِطبق ٔی وٞ ٝب ـا ثٟ٘ب آٟٙس
var theresult=10-3;
window.alert(theresult);
1
5/(2 َٔثب
var num1=10;
var num2=3;
41 دَلران پی سی
Hadi Ahmadi Learning Java Script
var theresult=num1-num2;
window.alert(theresult);
var num1=4;
var num2=5;
var thetotal=num1*num2;
window.alert(thetotal);
var num1=10;
var num2=2;
var theresult=num1/num2;
window.alert(theresult);
5/(,+ َٔثب
var num1=3;
var num2=4;
var theresult=num1/num2;
window.alert(theresult);
5/(,, َٔثب
var num1=10;
var num2=0;
var theresult=num1/num2;
window.alert(theresult);
41 دَلران پی سی
Hadi Ahmadi Learning Java Script
) اوزٜ ػؽؼ ثف ِفف سمىیٓ ٌؽٝؼ زفا وٌٛ بیز ٔیٟ٘ اة ثیٛؼـ ٔثبَ ثبال خ
var num1=11;
var num2=2;
var theresult=num1%num2;
window.alert(theresult);
var num1=2;
var theresult=++num1;
var num1=2;
var theresult=num1++;
5/(,0 َٔثب
<script type="text/javascript">
num1=2;
result= ++num1;
alert("num1= "+num1+" result= "+result);
num1=2;
42 دَلران پی سی
Hadi Ahmadi Learning Java Script
result= num1++;
alert("num1= "+num1+" result= "+result);
</script>
var num1=2;
var theresult=--num1;
5/(,2 َٔثب
var num1=2;
var theresult=num1--;
43 دَلران پی سی
Hadi Ahmadi Learning Java Script
44 دَلران پی سی
Hadi Ahmadi Learning Java Script
اق ؼوشٛـار ٌف٘ی ثفای ا٘دبْ ؼوشٛـاسی ثٌ ٝفٖ ثفلفاـی یه ـاث ٝٙؼیٍف اوشفبؼٔ ٜی ٌٛؼ) ؼـ ٘فاضی
ِفطبر ٚة ثىیبـ دیً ٔی آیؽ ؤ ٝی غٛاٞیٓ ؼـ ٌفایٗ غبِی ِفط ٝیه ـفشبـ ٍٔػُ ؼاٌش ٝثبٌؽ ٚ
ؼـ ٔٛاـؼ ؼیٍف ـفشبـ ِفط ٝسفبٚر ؼاٌش ٝثبٌؽ) ؼـ ایٗ ٔٛالغ ٔی سٛا٘یٓ اق ؼوشٛـ ٌف٘ی ifاوشفبؼ ٜوٙیٓ) ثب
ایٗ ؼوشٛـ ٍٔػُ ٔی وٙیٓ اٌف ٌفٖ غبِی ثفلفاـ ثٛؼ یه ػُٕ غبَ ا٘دبْ ٌٛؼ) وبغشبـ ایٗ ؼوشٛـ ثٝ
ٌىُ قیف اوز 5
)if(condition
{
دستوری کَ در صورت درستی ضزط اجزا هی ضود در ایٌجا قزار هی گیزد
}
ٔثبَ 50(,
)"اگز ایي پیام را تاییذ کٌیذ دستور ضزطی اجزا هی گزددvar test = confirm(".
{)if (test==true
)"ضوا دکوَ هورد ًظز را کلیک کزدیذ و ایي پیام بَ ًوایص درآهذ"(alert
}
ثفای ایٙى ٝوؽ ثبال ـا آقٔبیً وٙیؽ ِیٙه قیف ـا وّی وٙیؽ ٚؼـ دیبٔی وٕ٘ ٝبیً ؼاؼٔ ٜی ٌٛؼ ؼوٕٝ
OKـا وّیه وٙیؽ) اٌف ؼوٕ Cancel ٝـا وّیه وٙیؽ ٌفٖ ثبال ثفلفا ٕ٘ی ٌٛؼ ٚثؽ ٖٚاخفا ٌؽٖ ؼوشٛـ
ٌف٘ی ِفطٔ ٝثبَ ثبق ٔی ٌٛؼ)
ثفغی ٔٛالغ ٕٔىٗ اوز ثػٛاٞیٓ ؼوشٛـ ٌف٘ی یه ٌفٖ ـا زه وٙؽ سب ؼـ ِٛـر ثفلفاـ ثٛؼٖ آٖ
ٌفٖ یه ففٔبٖ غبَ ـا اخفا وٙؽ ٚؼـ ِٛـسی وٌ ٝفٖ ثفلبـ ٘جٛؼ یه ففٔبٖ ؼیٍف ـا ث ٝاخفا ؼـ آٚـؼ) ؼـ
ایٗ ٔٛاـؼ ٔی سٛا٘یٓ اق ؼوشٛـ ifثٕٞ ٝفا else ٜاوشفبؼ ٜوٙیٓ) وبغشبـ ایٗ ؼوشٛـ ثٌ ٝىُ قیف اوز 5
{)if(condition
}دستوراتی کَ در صورت درستی ضزط اجزا هی ضوًذ
{else
}دستوراتی کَ در صورت عذم بزقزاری ضزط اجزا هی ضوًذ
ؼـ وؽ ثبال اثشؽا ٌفٖ ثفـوی ٔی ٌٛؼ ٚؼـ ِٛـر ثفلفاـی آٖ ؼوشٛـار ٔفث ٝ٘ٛاخفا ٔی ٌ٘ٛؽ ِٚی ؼـ
ِٛـسی وٌ ٝفٖ ثفلفاـ ٘جبٌؽ ؼوشٛـار ٔفث ٖٛث ٝلىٕز elseاخفا ٔی ٌٛؼ)
ٔثبَ 50(-
>"<script type="text/javascript
;)'var yourmark = prompt('enter a number between 0 to 20
{)if (yourmark>10
;)'alert('your number great than 10
}
{else
;)'alert('your number not great than 10
}
></script
ثفای ٍٔبٞؽ٘ ٜشیدٔ ٝثبَ ثبال آٖ ـا أشطبٖ وٙیؽ ٚؼـ وبؼـی وٕ٘ ٝبیً ؼاؼٔ ٜی ٌٛؼ یه ػؽؼ اق +سب
ٚ ,+اـؼ وٙیؽ ٚؼوٕ OK ٝـا وّیه وٙیؽ)
ٕٔىٗ اوز ؼـ ٘ظف ؼاٌش ٝثبٌیٓ سب ؼـ ِٛـر ثفلفاـی یه ٌفٖ یه ؼوشٛـ غبَ اخفا ٌٛؼ ٚؼـ
ِٛـسی وٌ ٝفٖ ثفلفاـ ٘جٛؼ ٌفٖ ؼیٍفی ثفـوی ٌٛؼ ٚؼوشٛـ ٔفث ٖٛث ٝآٖ اخفا ٌٛؼ ٚؼـ ِٛـسی وٞ ٝیر
یه اق ٌفٟ٘بی لجُ ثفلفاـ ٘جٛؼ ؼوشٛـ ؼیٍفی اخفا ٌٛؼ) ؼـ ایٗ ٔٛالغ ٔی سٛا٘یٓ اق ؼوشٛـ else ifؼـ
ؼوشٛـ if...elseلجّی اوشفبؼ ٜوٙیؽ) ثِٛ ٝـر قیف 5
ؼـ ایٗ ـٚي ٔطؽٚؼیشی اق ٘ظف سؼؽاؼ ٌفٟ٘ب ٚخٛؼ ٘ؽاـؼ ٔ ٚی سٛا٘یٓ ث ٝسؼؽاؼ ٘بٔطؽٚؼ اق ؼوشٛـ else if
اوشفبؼ ٜوٙیٓ ِٚی ؼـ ٔٛاـؼی و ٝسؼؽاؼ ایٗ ٌفٟ٘ب غیّی قیبؼ ٔی ٌٛؼ ثٟشف اوز ث ٝخبی ایٗ ـٚي اق
ؼوشٛـ switchو ٝثؼؽاً سٔٛیص ؼاؼ ٜغٛاٞؽ ٌؽ اوشفبؼ ٜوٙیٓ)
ؼـ ایٙدب ٔثبَ لجُ ـا ثب وٕی سغییف ثفای ایٗ ـٚي اوشفبؼٔ ٜی وٙیٓ 5
ٔثبَ 50(.
>"<script type="text/javascript
;)'var yourmark = prompt('enter a number between 0 to 20
{)if (yourmark>0 && yourmark<20
;)'ضوارٍ وارد ضذٍ بیي 0تا 00است'(alert
}
{)else if(yourmark>20
;)'ضوارٍ وارد ضذٍ اس 00بشرگتز است'(alert
}
{else
)'عبارت وارد ضذٍ یا 0است اس بَ جای عذد اس حزف استفادٍ کزدٍ ایذ'(alert
}
></script
دستور switch
اٌف ثػٛاٞیٓ ثفای اوىفیذز ٞبی غٛؼ ٔىیفٞبی ثیٍشفی ؼـ ٘ظف ثٍیفیٓ یه ـاٛٔ ٜخٛؼ اوشفبؼ ٜاق
ؼوشٛـار ifس ٛؼـ س ٛاوز) ایٗ ـٚي ؼـ ٔٛاـؼ خكئی وبـاییؽاـؼ ٚؼـ ِٛـسی و ٝسؼؽاؼ ٌفٟ٘ب قیبؼ ٌٛؼ ٔب ـا
ٔدجٛـ ٔی وٙؽ اق سؼؽاؼ قیبؼی else ٚ ifؼـ اوىفیذز غٛؼ اوشفبؼ ٜوٙیٓ و ٝایٗ ٔىئّ ٓٞ ٝثبػث ٘ٛال٘ی
ٌؽٖ ثف٘بٔ ٚ ٝاق ٘ففی أىبٖ اٌشجب ٜـا ٓٞثبال ٔی ثفؼ)
ـا ٜضُ ؼیٍف اوشفبؼ ٜاق ؼوشٛـ ٌف٘ی switchاوز) ایٗ ؼوشٛـ ـا ٔی سٛاٖ ؼـ ٍٙٞبٔی ؤ ٝی غٛاٞیٓ
یه ٔشغیف ـا ثب زٙؽیٗ ٔمؽاـ ٔمبیى ٝوٙیٓ ٔ ٚمؽاـ ٔشٙبوت ثب آ٘فا دیؽا وٙیٓ وبـ ثفؼ ؼاـؼ)
ثفای ؼـن ثٟشف ثٌ ٝىُ وّی ایٗ ـٚي سٛخ ٝوٙیؽ)
)switch(x
{
case 1 : ;//if x=value(case1) it's run
;break
case 1 : ;//if x=value(case2) it's run
;break
;default )//if x not equal to value(case1) or value(case2
ؼوشٛـ ثبال ث ٝایٗ ِٛـر ػُٕ ٔی وٙؽ و ٝؼـ اثشؽا ٔب یه ػجبـر xـا ؼاـیٓ ؤ ٝشغیف ٔب ٔی ثبٌؽ وٝ
یه ثبـ ثفـوی ٔی ٌٛؼ ٚوذه ٔمؽاـ ایٗ ػجبـر ثب ٞف وؽاْ اق ٞ caseب ٔمبیىٔ ٝی ٌٛؼ سب ثجیٙؽ و ٝثب
وؽاْ یه ثفاثف ٔی ثبٌؽ اٌف سٙبثمی ؼاٌش ٝثبٌؽ ٔدٕٛػ ٝوؽٞبی آٖ caseاخفاء ٔی ٌٛؼ
:break
اق breakثفای ایٗ اوشفبؼٔ ٜی ٌٛؼ و ٝؼـ ِٛـر ؼـوشی ٞف وؽاْ اق ٞ caseب ؼوشٛـ ث case ٝثؼؽی
٘فٚؼ ٚاق ؼوشٛـ غبـج ٌٛؼ
:default
ؼـ ِٛـسی ؤ ٝمؽاـ ٔشغیف ٔٛـؼ ٘ظف ثب ٞیسىؽاْ اق ٞ caseب ثفاثف ٘جٛؼ وؽ لىٕز defaultاخفاء ٔی
ٌٛؼ اِجش ٝاوشفبؼ ٜاق defaultاغشیبـی اوز ٌٕب ٔی سٛا٘یؽ اق آٖ ث ٝضؽ ٘یبق اوشفبؼ ٜوفؼ ٚ ٜیب آٖ ـا ضؿف
وٙیؽ
بٞ ٖ ٘یبق اق آٜ ا٘ؽاـٝا٘یؽ ثٛ ٔی سٚ ؼیشی ٘ؽاـیؽٚیر ٔطؽٞ case ـارٛ اق ؼوشٌٜٕب ؼـ اوشفبؼ5ًِکت
یؽٙ وٜاوشفبؼ
ثبٚ ـا ؼـیبفز#ٝفشٞ بیٞقٚاق ـ$ قٚف ـٞ بظف ثبٙ ػؽؼ ٔش،getDay ٚ Date اثغٛ اق سٜؼـ ٔثبَ قیف ثب اوشفبؼ
بیٞقٚ ثف اوبن ـٝیؽ وٙ ٔی وٝ ٔالضظ،ٓ ایٜخی اـوبَ وفؼٚ غفٝب ـا ثٞ )case( اـؼٛٔ یىی اق،ٖ آٝ ثٝخٛس
ؼـcase ٚ switch بٔیه اقٙـر ؼایِٛ ٝاٖ ثٛـر ٔی سّٛٙ ایٝ ث،ؼٛؽ ثٞاٛخی ٔب ٘یك ٔشغیف غٚ غفٝفشٞ
) وفؼٜا اوىفیذز اوشفبؼٚخب
50(/ َٔثب
<script type="text/javascript">
var date = new Date()
var today = date.getDay()
switch(today){
case 1 :
document.write("mon");
break;
case 2 :
document.write("tus");
break;
case 3 :
document.write("wen");
break;
case 4 :
document.write("tur");
break;
case 5 :
document.write("fri");
break;
case 6 :
document.write("sat");
break;
case 7 :
document.write("sun");
break;
default:
document.write("error")
}
</script>
49 دَلران پی سی
Hadi Ahmadi Learning Java Script
سبثغ getDay ٚ dateؼـ ضبِز ػبؼی ثب ـٚق ٚقٔبٖ ٔیالؼی وبـ ٔی وٙٙؽِ ،ؿا ثف اوبن ایٙى ٝیىٍٙجٝ
ـٚق سؼٙیُ ؼـ سمٛیٓ ٔیالؼی ٔطىٛة ٔی ٌٛؼ $ـٚق ٞفشٌٓ ،#فٚع ٌٕبـي ـٚقٞبی ٞفش ٝاق ؼٌٙٚجcase $ ٝ
#1غٛاٞؽ ثٛؼ)
اػؽاؼی و ٝث ٝػٛٙاٖ ٔمبؼیف ثٞ case ٝب ؼاؼٌ ٜؽ ٜا٘ؽ ،ؼـ ٚالغ ضبُِ ٘ ٚشید ٝثفٌفؼا٘ؽٌ ٜؽ ٜاق لىٕز
ٚ date.getDayثب سٛخ ٝث ٝسبـیع سٙظیٓ ٌؽ ٜویىشٓ اوز و ٝثیٗ ,سب 2ؼـ ٘ٛوبٖ اوز)
ثب سٛخ ٝث ٝسبـیع ویىشٓ ٌٕب ،أفٚق وٌٙ ٝج ٝاوز!
ٔؼٕٛال قٔب٘ی وٌٕ ٝب وؽی ـا ٔی ٘ٛیىیؽ٘ ،یبق ؼاـیؽ ؤ ٝدٕٛػ ٝوؽٞبیی دٍز وف ٓٞزٙؽ ثبـ اخفا
ٌ٘ٛؽ) ث ٝخبی زٙؽیٗ ثبـ أبف ٝوفؼٖ ایٗ زٙؽغٗ دٍز وف ٔ ٓٞی سٛا٘یؽ اق ؼوشٛـار قیف اوشفبؼ ٜوٙیؽ)
حلقِ For
ثفای ٌفٚع اثشؽا وبغشبـ وّی ضّم for ٝـا ٍ٘بٖ ٔی ؼٞیٓ 5
ٔثبَ 50(0
>"<script type="text/javascript
{ )for (var count=1;count<11;count+=1
;)">document.write("I am part of a loop!<br /
}
></script
ٔثبَ قیف ضّم ٝای ـا سؼفیف ٔی وٙؽ و ٝاق ٌ i=0فٚع ٌؽ ٚ ،ٜسب قٔب٘ی و i ٝوٛزىشف یب ٔىبٚی 0ثبٌؽ اؼأٝ
ٔی یبثؽ) ٞف ثبـ و ٝضّم ٝاخفا ٔی ٌٛؼ iیه ٚاضؽ افكایً ٔی یبثؽ)
ٔثبَ 50(1
>"<script type="text/javascript
)for(i=1;i<7;i++
{
;)document.write("<h" + i +">this is heading" + i
;)">" document.write("<h" + i +
}
></script
ضّم ٝی forثبػث ٔی ٌٛؼ ٔمؽاـ ٔشغیف iسب وٛزىشف ٔ ٚىبٚی 6اؼأ ٝدیؽا وٙؽ ٚوذه ٔشٛلف ٌٛؼ
سٛوٗ ؼوشٛـ document.writeأ َٚشٗ غٛؼ ـا ثّٛـر ػٛٙاٖ ثٛخٛؼ آٚـؼیٓ و ٝثبػثً سً
ػٛٙا٘ی اوز وٚ ٝاـؼ ٌؽ ٚ ٜسٛوٗ + i +ایٗ ؼوشٛـ ٞف ثبـ و ٝضّم ٝسىفاـ ٔی ٌٛؼ ػٛٙاٖ ٘یك سغییف دیؽا
ٔی وٙؽ ٚسٛوٗ ٔ +iمؽاـ ٔشغیف iـا ؼـ وٙبـ ٔشٗ غٛؼ و ٝثِٛ ٝـر ػٛٙاٖ ؼـ آٔؽ ٜسىفاـ وفؼیٓ ٚؼـ
٘ document.writeیك ایٗ سً ػٛٙاٖ ـا ثىشیٓ
ًکتِ5سٕبٔی وؽٞبیی و ٝثیٗ ایٗ ؼ ٚؼاثُ وٛر ٚاـؼ ٌؽ ٜا٘ؽ خؽا اق ٔ & iی ثبٌؽ
ًتیجِ:
ضّمٞ ٝب ٔدٕٛػ ٝوؽٞبیی ـا ثفای سؼؽاؼ غبِی یب سب قٔب٘ی و ٝیه ٌف٘ی ثفلفاـ ٌٛؼ اخفا ٔی وٙٙؽ
ایٗ ضّمٕٞ ٝبٖ وبـایی forـا ؼاـؼ ٚاق ٕٞبٖ اخكا سٍىیُ ٌؽ ٚ ٜفمٗ ؼـ سفویت لفاـ ٌففشٗ اخكاء ففق
ؼاـؼ)
ًکتِ :ث ٝخبی =< ٔی سٛاٖ اق ٞف ػٍّٕف ٔمبیى ٝای ؼیٍف اوشفبؼ ٜوفؼ)
ٔثبَ دبییٗ ٕٞب٘ٙؽ ٔثبَ ٞبی ٔ forی ثبٌؽ ٚسفویت لفاـ ٌففشٗ اخكاء ففق ؼاـؼ)
ٔثبَ 50(2
>"<script type="text/javascript
;var i = 0
)while(i <= 6
{
;)document.write("the number is :" + i
;)">document.write("<br /
;i++
}
></script
ؼـ ٔثبَ ثبال ٔؽاـ ٔشغیف iـا +لفاـ ؼاؼ ٜایٓ ٚؼـ غٗ ثؼؽ $ضّم ٝی #whileیه ٔشٗ ـا ثِٛ ٝـر ـٌشٝ
لفاـ ؼاؼ ٜایٓ ٚسٛوٗ ؼوشٛـ & iضّم ٝـا سىفاـ وفؼیٓ سب ؼـ وٙبـ ٞف ٔشٗ ٔب ٌٕبـ ٜای ث ٝسفسیجی و ٝغٛؼٔبٖ
لفاـ ؼاؼ ٜایٓ لفاـ ٌیفؼ ٚؼـ غٗ ثؼؽ ثب اوشفبؼ ٜاق سً < HTML <brخٕالر غٛؼ ـا ؼـ قیف یىؽیٍف لفاـ
ؼاؼ ٜایٓ ٚثؼؽ ؼوشٛـ i++ـا لفاـ ؼاؼ ٜایٓ ٚوذه ؼوشٛـ ـا ثىشیٓ)
حلقِ do...while
ایٗ ضّم ٝیه سفبٚر ثب whileؼاـؼ) ایٗ ضّم ٝاثشؽا ٔدٕٛػ ٝؼوشٛـار ـا یه ثبـ ا٘دبْ ٔی ؼٞؽٚ ،
وذه ضّم ٝـا سب قٔب٘ی وٌ ٝفٖ ثفلفاـ ٌٛؼ سىفاـ ٔی وٙؽ)
ؼـ ثؼٕی ٔٛالغ ٔب ٘یبق ؼاـیٓ و ٝضّم ٝضؽالُ یىجبـ اخفا ٌٛؼ ،ضشی اٌف ٌفٖ ثفلفاـ ٘جبٌؽ! قیفا
ؼوشٛـار لجُ اق ثفـوی ٌفٖ اخفا ٔی ٌ٘ٛؽ)
ؼوشٛـ5
{ do
} دستورات بذًَ حلقَ
;)هقذار پایاًی=<هتغیز (while
ؼـ ٔثبَ قیف اق do...whileاوشفبؼٔ ٜی وٙیٓ) ؼـ ایٗ ٘ٛع ضّمٔ ،ٝدٕٛػ ٝؼوشٛـار ضؽالُ یه ثبـ اخفا
ٔی ٌٛؼ ضشی اٌف ٌفٖ ٘بؼـوز ثبٌؽ ،قیفا ؼوشٛـار لجُ اق ثفـوی ٌفٖ اخفا ٔی ٌ٘ٛؽ)
ٔثبَ 50(3
>"<script type="text/javascript
;var i = 8
do
{
;)"alert("this is a test
;i++
}
;)while(i<=4
></script
ؼـ ٔثبَ ثبال i=8اوز ٌ ٚفٖ ضّم ٝایٗ اوز و i ٝوٛزىشف اق /ثبٌؽ ،أب ثب ایٙىٌ ٝفٖ ثفلفاـ ٘یىز
ِٚی اخفاء ٔی ٌٛؼ ز ٖٛایٗ ٌفٖ ؼـ ا٘شٟبی ضّم ٝثفـوی ٔیٍٛؼ ،ده ضّم ٝیىجبـ اخفا ٌؽ ٚ ٜدیبْ یه ثبـ
ٔیٍٛؼ. ٕ٘بیً ؼاؼٜ
ایٗ ؼوشٛـ ثٙٔ ٝظٛـ ثؽوز آٚـؼٖ غِّٛیبر ٚیب ٔشؽ اٌیبء اوشفبؼٔ ٜی ٌٛؼ ؼـ ِٛـسی و ٝثػٛاٞیٓ
غِّٛیز ٞبی یه ٌی ء ـا ثؽوز آٚـیٓ اق ایٗ ـٚي اوشفبؼٔ ٜی وٙیٓ اِجش ٝث ٝایٗ ٘ىش ٝسٛخ ٝوٙیؽ وٝ
ّٔٙت خّى ٝدیً ؼـ ٔٛـؼ forثىیبـ وبـثفؼی سف اق ایٗ ّٔٙت ٔی ثبٌؽ
ؼوشٛـ5
٘ىش 5ٝؼوشٛـ ٔٛخٛؼ ؼـ ثؽ٘ for...in ٝثفای ٞف غِّٛیز یه ثبـ اخفا ٔی ٌٛؼ)
ٔثبَ 50(4
>"<script type="text/javascript
;}"var person={first:"welcome to",second:"your site
)for(x in person
{
;)" " document.write(person[x] +
}
></script
55 دَلران پی سی
Hadi Ahmadi Learning Java Script
56 دَلران پی سی
Hadi Ahmadi Learning Java Script
رٍیذادّا
ـٚیؽاؼ یه غبِیز اق دیً سؼفیف ٌؽ ٜؼـ خبٚا اوىفیذز اوز و ٝآٖ ـا ؼـ ِفطبر ٚة غٛؼ ثىبـ ٔی
ثفیٓ)
ٔثبَ 51(,
><body
><form
"!<input type="button" value="Click Me
>onclick="window.alert('Hi!';" /
></form
></body
ٔثبَ ثبال ٌبُٔ یه ـٚیؽاؼ ٔی ثبٌؽ و ٝثٔ ٝطٓ فٍفؼٖ ؼوٕ Click Me ٝفؼبَ ٔی ٌٛؼ ٚدیغبْ
! Hiـا ثٕ٘ ٝبیً ٔی ٌؿاـؼ)
ؼـ اؼأ ٝثٔ ٝؼففی ا٘ٛاع ـٚیؽاؼٞب ٔ ٚثبِٟبیی ؼـ ثبـٞ ٜف وؽاْ غٛاٞیٓ دفؼاغز )
ـٚیؽاؼ onabortقٔب٘ی اسفبق ٔی افشؽ و ٝیه وبـثف ِٛؼ ٌؽٖ یه سّٛیف ـا ؼـ ٘یٕ ٝـأ ٜشٛلف وٙؽ)
ثفای ٔثبَ اٌف ٌٕب ثػٛاٞیؽ قٔب٘ی و ٝوبـثف ِٛؼ ٌؽٖ یه ػىه ـا ٔشٛلف ٔی وٙؽ دیبٔی ـا ؼـیبفز
ٕ٘بیؽ ٔی سٛا٘یؽ اق ؼوشٛـ قیف اوشفبؼٕ٘ ٜبییؽ5
ٔثبَ 51(-
)ؽٙ وّیه وٝبی ِفطٞ ٖی یىی اق إِبٚ وبـثف ـٝؽ وٞیؽاؼ قٔب٘ی ـظ ٔی ؼٚایٗ ـ
51(. َٔثب
<body>
<form>
<input type="button" value="Do not Click Here"
onclick="window.alert('I told you not to click me!');">
</form>
</body>
51(/ َٔثب
<body>
<a href="http://none" onclick="window.alert('Hey! You clicked
me!');">
Don't Click Me</a>
</body>
هٙ ِیٝؽ سب ثٙـٌف سالي ٔی وٍٚؽاـ ٔفٞ ؼٛخٚ ثبٝؼ ؼاـؼ ایٗ اوز وٛخٚ ؼـ وؽ ثبالٍٝٔىّی و
ؼ ٌىُ ِطیص وؽٌٛ “ ٔیServer not found” ْ ٕ٘بیً ؼـ آٔؽٖ دیغبٝ ایٗ ثبػث ثٚ ؽٙؼوشیبثی دیؽا و
یؽٙ ٔی وٜؽٞـا ؼـ قیف ٍٔب
51(2 َٔثب
<body>
<a href="http://none"
onclick="window.alert('Hey! You clicked me!'); return false;">
Don't Click Me</a>
</body>
58 دَلران پی سی
Hadi Ahmadi Learning Java Script
)ؽٙ ؼاثُ وّیه وٝبی ِفطٞ ٖی یىی اق إِبٚ وبـثف ـٝؽ وٞیؽاؼ قٔب٘ی ـظ ٔی ؼٚایٗ ـ
51(3 َٔثب
5یؽاؼ اسفبق ٔی افشؽٚی ٔفثغ لفٔك ـً٘ ایٗ ـٚؼـ ٔثبَ قیف ثب ؼاثُ وّیه وفؼٖ ـ
<html>
<head>
<title>Mouse Events Example</title>
<script type="text/javascript">
function handleEvent(oEvent) {
var oTextbox = document.getElementById("txt1");
oTextbox.value += "\n" + oEvent.type;
}
</script>
</head>
<body>
<p>Use your mouse to click and double click the red
square.</p>
<div style="width: 100px; height: 100px; background-color:
red"
ondblclick="handleEvent(event)" id="div1"></div>
<p><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
</html>
هٙؽ یه ِیٙ٘ ٔبٝبی ِفطٟ٘ی یىی اق إِبٚن ـا ـٚ ٌف ٔبٜ وبـثف اٌبـٝؽ وٞیؽاؼ قٔب٘ی ـظ ٔی ؼٚایٗ ـ
5ؽٞضفوز ؼ
51(4 َٔثب
<a href="http://www.dlp.lxb.ir"
onmouseover="window.alert('I told you not to try to click
me!');">
Don't Try Clicking Me!</a>
59 دَلران پی سی
Hadi Ahmadi Learning Java Script
ـؼ ٘ظف غبـج ٕ٘بیؽٛٔ ٜؼٚن ـا اق ٔطؽٚ ٌف ٔبٜ وبـثف اٌبـٝؽ وٞیؽاؼ قٔب٘ی ـظ ٔی ؼٚایٗ ـ
51(2 َٔثب
5<a href="http://www.dlp.lxb.ir"
onmouseout="window.alert('What, you didn\'t like my link?');">
Click Me!</a>
)ؽٞـؼ ٘ظف ـظ ٔی ؼٛٔ ٜؼٚوٗ وبثف ؼـ ٔطؽٛن سٍٚبْ فٍفؼٖ وّیؽ ٔبٙٞ یؽاؼ ؼـٚایٗ ـ
51(3 َٔثب
عٛلٚ ٝیؽاؼ ثٚـ وبُٔ آٖ ـٛ٘ ٍٝبْ دبییٗ ـفشٗ آٖ ثٙٞ ؼـٚ ً٘ی ٔفثغ لفٔك ـٚؼـ ٔثبَ قیف ثب وّیه ثف ـ
)٘ؽؼٛٔی دی
<html>
<head>
<title>Mouse Events Example</title>
<script type="text/javascript">
function handleEvent(oEvent) {
var oTextbox = document.getElementById("txt1");
oTextbox.value += "\n" + oEvent.type;
}
</script>
</head>
<body>
<p>Use your mouse to click and double click the red
square.</p>
<div style="width: 100px; height: 100px; background-color:
red"
onmousedown="handleEvent(event)"
id="div1"></div>
<p><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
</html>
61 دَلران پی سی
Hadi Ahmadi Learning Java Script
)ؽٞـؼ ٘ظف ـظ ٔی ؼٛٔ ٜؼٚوٗ وبثف ؼـ ٔطؽٛ سٜ ٌؽٜن فٍفؼٚب وفؼٖ وّیؽ ٔبٍٞبْ ـٙٞ یؽاؼ ؼـٚایٗ ـ
51(4َٔثب
ع ٔیٛلٚ ٝیؽاؼ ثٚـ وبُٔ ـٛ٘ ٍٝبْ ثبال ـفشٗ آٖ ثٙٞ ؼـٚ ً٘ی ٔفثغ لفٔك ـٚؼـ ٔثبَ قیف ثب وّیه ثف ـ
)٘ؽؼٛدی
<html>
<head>
<title>Mouse Events Example</title>
<script type="text/javascript">
function handleEvent(oEvent) {
var oTextbox = document.getElementById("txt1");
oTextbox.value += "\n" + oEvent.type;
}
</script>
</head>
<body>
<p>Use your mouse to click and double click the red
square.</p>
<div style="width: 100px; height: 100px; background-color:
red"
onmouseup="handleEvent(event)"
id="div1"></div>
<p><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
</html>
)٘ؽؼٛع ٔی دیٛلٚ ٝـؼ ٘ظف ثٛٔ ٜؼٚن ؼـ ٔطؽٚـؼٖ ٍ٘بٍ٘ف ٔبٚ ضفوز ؼـ آٍٝبْ ثٙٞ یؽاؼ ؼـٚایٗ ـ
)٘ؽؼٛع ٔی دیٛلٚ ٝیؽاؼ ثٚیُ لفٔك ـً٘ ـٙ ٔىشٜؼٚ ٔطٓ ضفوز ؼـ ٔطؽٝؼـ ٔثبَ ث
51(,+ َٔثب
<html>
<head>
61 دَلران پی سی
Hadi Ahmadi Learning Java Script
62 دَلران پی سی
Hadi Ahmadi Learning Java Script
51(,, َٔثب
<body>
<input value="onkeydown" type="text"
onkeydown="alert('onkeydown')" />
</body>
ی آٖ ـا سبیخٚف ـٙ وبـاوٚ ٚ ٜ وّیؽ ـا فٍبـ ؼاؼٝبی ِفطٞ وبـثف یىی اق وّیؽٝؽ وٞقٔب٘ی ـظ ٔی ؼ
)ٕ٘بیؽ
51(,- َٔثب
<body>
<input value="onkeypress" type="text" onkeypress="alert('
onkeypress')" />
</body>
<body>
<input value="onkeyup" type="text" onkeyup="alert('onkeyup')"
/>
</body>
63 دَلران پی سی
Hadi Ahmadi Learning Java Script
ثفای ؼـن ثٟشف ـٚیؽاؼٞبی ٔفث ٖٛثِ ٝفط ٝوّیؽ فبیُ htmlی ـا و ٝؼـ ٔٛـؼ ایٗ ٔجطث اوز ٕٔ ٚیٕٝ
وشبة ٔی ثبٌؽ ـا ٔٛـؼ ثفـوی لفاـ ؼٞیؽ)
ایٗ ـٚیؽاؼٞب ٔفث ٖٛث ٝػّٕیبسی اوز و ٝوبـثف ثف ـٚی اخكای ففْ ا٘دبْ ٔی ؼٞؽ) اِجشٕٔ ٝىٗ اوز
ثؼٕی اق ایٗ ـٚیؽاؼٞب ثف ـٚی ٔشٗ ٞبی ػبؼی ؼـ ِفطُ ٝن ػىه اِؼُٕ ٍ٘بٖ ؼٙٞؽ أب ثیٍشفیٗ وبـثفؼ ـا
ؼـ ففْ ٞب ؼاـ٘ؽ)
قٔب٘ی اسفبق ٔی افشؽ و ٝوبـثف ثػٛاٞؽ ـٚی یىی اق اخكای ففْ یب دٙدفٔ ٜشٕفوك ٌٛؼ ٚدیبٔی ـا ٕ٘بیً
ٔی ؼٞؽ یب ػىه اِؼّٕی ـا ٍ٘بٖ ٔی ؼٞؽ)
ٔٙظٛـ اق ٔشٕفوك ٌؽٖ ایٗ اوز و ٝثٛٙـ ٔثبَ ثفای ٌ٘ٛشٗ ـٚی یه text boxوّیه وٙؽ )
ٔثبَ 51(,/
><form
Enter Your Name:
<input type="text" onfocus="window.alert('Don\'t forget to
>capitalize!');" /
></form
ایٗ ـٚیؽاؼ ثفػىه ـٚیؽاؼ focusاوز ٍٙٞ ٚبٔی ـظ ٔی ؼٞؽ و ٝسٛوٗ ٔٛن ٚیب ؼوٕ tab ٝویجٛـؼ ثف
ـٚی یىی اق اخكای ففْ سٕفوك غٛؼ ـا اق ؼوز ثؽٞؽ)
ٔثبَ 51(,0
><form
>Give this box focus:<br /
";)'!<input type="text" onblur="window.alert('Hey! Come back
>/><br /
>then give this box focus to blur the first one:<br /
><input type="text" /
></form
)یٙؽ سغییف ٔشٗ ؼـ وبؼـ ٔشٙ٘ؽ' ٔبٞ وبـثف یىی اق اخكای ففْ ـا سغییف ؼٝؽ وٞیؽاؼ قٔب٘ی ـظ ٔی ؼٚایٗ ـ
51(,1 َٔثب
<form>
Are you cool?<br />
<select onchange="window.alert('Why did you change that?');">
<option selected="selected">Yes</option>
<option>No</option>
<option>Undecided</option>
</select>
</form>
)ؽٞ ـا فٍبـ ؼsubmit ٕٝ وبـثف ؼـ یه ففْ ؼوٝؽ وٍٞبٔی ـظ ٔی ؼٙٞ ٗیؽاؼ فمٚایٗ ـ
51(,2 َٔثب
ٝ ثٚ) ٕ٘بیؽreset ة ـاٚ ٝ ِفطreset ٕٝی ؼوٚ وبـثف ثب وّیه ثف ـٝؽ وٞیؽاؼ قٔب٘ی ـظ ٔی ؼٚایٗ ـ
)ٓ ؼـ ٘ظف ٌففزٞ بٞ ْ ففٖٝ ثٛبی ٔفثٞیؽاؼٚاٖ آٖ ـا اق ـٛػی ٔی سٛ٘
)٘ؽؼٛع ٔی دیٛلٚ ٝیؽاؼ ثٚ ـٚ ؼٌٛ ـیىز ٔیٝ ِفطreset ٕٝی ؼوٚی ـٚؼـ ٔثبَ قیف ثب وّیه ثف
51(,3 َٔثب
<html >
<head>
<title> Form Events Example </title>
</head>
<body>
66 دَلران پی سی
Hadi Ahmadi Learning Java Script
<form onReset="alert('onreset');">
ز اخفإِٙؼ ؼـ ایٗ اٛخٛٔ ؼ اوىفدزٌٛ ؼ ؼاـؼ ا٘شػبةٛ ـا ؼـ غٝبوٌٙ ٗ ایٝ لىٕشی اق ففْ وٝلشی وٚ
)ؼٌٛ ٔی
)ؼٌٛ یؽاؼ اخفا ٔیٚؼ ؼـ وبؼـ ایٗ ـٛخٛٔ ٗؼـ ٔثبَ قیف ثب ا٘شػبة ٔش
51(,4 َٔثب
<html>
<head>
<title> Form Events Example </title></head>
<body>
<textarea cols="20" rows="10"
onselect="alert('onselect')";>Lerning JavaScript
Hadi Ahmadi
http://dlp.lxb.ir</textarea>
</body>
</html>
وشبةٕٝ ٕٔیٚ ـؼ ایٗ ٔجطث اوزٛٔ ؼـٝی ـا وhtml ُ ففْ فبیٖٝ ثٛبی ٔفثٞیؽاؼٚشف ـٟثفای ؼـن ث
)یؽٞـؼ ثفـوی لفاـ ؼٛٔ ٔی ثبٌؽ ـا
67 دَلران پی سی
Hadi Ahmadi Learning Java Script
68 دَلران پی سی
Hadi Ahmadi Learning Java Script
51(-+ َٔثب
ُ وؽ قیف ـا ؼـ یه فبیٝؼٙا٘یؽ اثشؽا لٛـر یه فبیُ غبـخی ثىبـ ثجفیؽٔی سِٛ ٝ آٖ ـا ثٝیؽ وٞاٛاٌف ٔی غ
) یؽٙ وٜ ؾغیفload_alert.js ٔثال.js
51(-, َٔثب
window.onload = function() {
window.alert('I\'m done loading now!');
};
51(-- َٔثب
<body>
<script type="text/javascript" src="load_alert.js"></script>
Text for the body of the page...
</body>
69 دَلران پی سی
Hadi Ahmadi Learning Java Script
51(-. َٔثب
ٚ ـؼ ایٗ ٔجطث اوزٛٔ ؼـٝی ـا وhtml ُـٌف فبیٚ ٔفٜدفٙ دٖٝ ثٛبی ٔفثٞیؽاؼٚشف ـٟثفای ؼـن ث
)یؽٞـؼ ثفـوی لفاـ ؼٛٔ وشبة ٔی ثبٌؽ ـإٕٝٔی
)ؼٌٛ وٗ وبـثف اخفا ٔیٛـٌف سٚ ٔفٜدفٍٙبْ سغییف وبیك دٙٞ یؽاؼ ؼـٚایٗ ـ
51(-. َٔثب
<html>
<head>
<title>OnResize Example</title>
</head>
<body onresize=”alert(„Resizing‟)”>
</body>
</html>
ٝ زٚ ؼیَٕٛ وفؼٖ ػٚ اوىفٝ٘ؽؼ) زٛع ٔی دیٛلٚ ٝ ثٜدفَٙ وفؼٖ یه دٍٚبْ اوىفٙٞ یؽاؼ ؼـٚایٗ ـ
)افمی
51(-/ َٔثب
) ٕ٘بیً ؼـ ٔی آیؽٝ یه دیغبْ ثفای ٌٕب ثَٝ وفؼٖ ِفطٚ ٔطٓ اوىفٝؼـ ٔثبَ قیف ث
<html>
<head>
71 دَلران پی سی
Hadi Ahmadi Learning Java Script
<title>OnScroll Example</title>
</head>
<body onscroll=”alert(„Scrolling‟)”>
<p>Try scrolling this window.</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
51(-0 َٔثب
ٔیٜخؽ ؼـ آٖ ٕ٘بیً ؼاؼٛٔ یٙ ٔشٝبیی ؼـ خؼجٞ ْ دیغبَٝ وفؼٖ ِفطٍٚبْ اوىفٙٞ ؼـ ٔثبَ قیف ؼـ
)ؼٌٛ
<html>
<head>
<title>OnScroll Example</title>
<script type=”text/javascript”>
window.onscroll = function () {
var oTextbox = document.getElementById(“txt1”);
oTextbox.value += “\nscroll is at “ + document.body.scrollLeft
+ “
horizontally and “ + document.body.scrollTop + “ vertically.”;
}
</script>
</head>
<body>
<p>Try scrolling this window.</p>
<p><textarea rows=”15” cols=”50” id=”txt1”></textarea>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
71 دَلران پی سی
Hadi Ahmadi Learning Java Script
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
72 دَلران پی سی
Hadi Ahmadi Learning Java Script
73 دَلران پی سی
Hadi Ahmadi Learning Java Script
خبٚااوىفیذز یه قثبٖ ٌیءٌفاوز) ٘ط ٜٛوبغز اٌیبء ؼـ ثػً دیٍففش ٝؼـ فُّ ٞبی ثؼؽی آٔٛقي
ؼاؼ ٜغٛاٞؽ ٌؽ) ؼـ ایٙدب ٔب اق اٌیبی ؼاغّی خبٚااوىفیذز اوشفبؼ ٜوفؼٍ٘ٚ ٜبٞی ث ٝآٟ٘ب ٔی ا٘ؽاقیٓ ٘ ٚطٜٛ
اوشفبؼ ٜاق آٟ٘ب ـا ٔی آٔٛقیٓ) ؼـ زٙؽ خّى ٝثؼؽی ؤ ٝفث ٖٛث ٝایٗ ثػً اوز ٞف یه اق ایٗ اٌیب ـا ثفـوی
اخٕبِی وفؼٔ ٚ ٜثبَ ٞبی قیبؼی اق آٖ ٞب ـا غٛاٞیٓ ؼیؽ)
اضیاء ()object
ٕٞبٍ٘ ٝ٘ٛو ٝؼـ اسبق ٌٕب اٌیبء ٔػشّفی ٕٞسٔ ٖٛیك ' ِٙؽِی ' سػز ٚغیفٚ ٜخٛؼ ؼاـؼ ؼـ ؼ٘یبی
ٔدبقی ٘یك اٌیبء ٚخٛؼ ؼاـؼ ثٛ٘ ٝـ ٔثبَ ؼوٕٞ ٝبیی و ٝثب htmlوبغش ٝایؽ ' وبؼـٞبیی و ٝثفای ػىه
وبغش ٝایؽ ٚغیف٘ ٜیك ٔفٌ ْٟٛیء ـا ثٔ ٝب ٔی ـوب٘ؽ)
ساختار اضیا
وبغشبـ اٌیب ٚزٍٍ٘ٛی سؼفیف آٟ٘ب ـا ثب سؼفی یه ٔثبَ دی ٔی ٌیفیٓ5
ٔثال ثفای وبغز ٌی carاثشؽا آٖ ـا ثِٛ ٝـر قیف سؼفیف ٔی وٙیٓ 5
ٔثبَ 52(,
{ )function car(seats,engine,theradio
;this.seats=seats
;this.engine=engine
;this.theradio=theradio
}
ؼـ غٗ ا َٚاق لٙؼ ٝوؽ ثبال ٌٕب ٘بْ ٌی ٚدبـأشفٞبی آٖ ـا ٍٔبٞؽٔ ٜی وٙیؽ ٚؼـ غ ٖٛٙثؼؽی ٚثب
اوشفبؼ ٜاق وّٕ this ٝاٟ٘ب ـا ثٔ ٝفٚـٌف ٔی ٌٙبوب٘یٓ)
ضبَ ثفای ٔمؽاـ ؼٞی ث ٝدبـأشفٞب ث٘ ٝفیك قیف ػُٕ ٔی وٙیٓ 5
اثشؽا یه ٔشغیف ـا سؼفیف وفؼ ٚ ٜوذه ثب اوشفبؼ ٜاق وّیؽ ٘ ٚ newبْ ٌی اق دیً سؼفیف ٌؽٔ ٜمبؼیف ـا
ثٌ ٝی ٔٙشىت وفؼ ٚ ٜؼـ ٔشغیف ؾغیفٔ ٜی ٕ٘بییٓ)
ٚثب اوشفبؼ ٜاق لٙؼ ٝوؽ قیف ٌٕب ٔی سٛایؽ ثٞ ٝف وؽاْ اق دبـأشفٞب ؼوشیبثی ؼاٌش ٝثبٌیؽ 5
5 ؽ ٌؽٞاٛٓ سف غٟ لبثُ فٚ ن سفّٕٛٔ ب ثفای ٌٕبٟ٘ اق آٜ اوشفبؼٜٛ ٘طٚ اق ٔثبَ قیف اٌیبٜثب اوشفبؼ
52(- َٔثب
<script type="text/javascript">
function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}
var work_car= new car("cloth","V-6","Tape Deck");
var fun_car= new car("leather","V-8","CD Player");
var engine_type= work_car.engine;
var seat_type= fun_car.seats;
var radio_type= fun_car.theradio;
document.write("I want a car with "+seat_type+" seats.<br
/>");
document.write("It also needs a "+engine_type+" engine.<br
/>");
document.write("Oh, and I would like a "+radio_type+" also.");
</script>
52(. َٔثب
function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}
var work_car= new car("cloth","V-6","Tape Deck");
var fun_car= new car("leather","V-8","CD Player");
work_car.engine="V-4";
var custom_car= new
car(fun_car.seats,work_car.engine,fun_car.theradio);
document.write("I want a car with "+custom_car.seats+"
seats.<br />");
document.write("It also needs a "+custom_car.engine+"
engine.<br />");
document.write("Oh, and I would like a "+custom_car.theradio+"
also.");
76 دَلران پی سی
Hadi Ahmadi Learning Java Script
function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}
var work_car= new car("cloth","V-6","Tape Deck");
var fun_car= new car("leather","V-8","CD Player");
var first_engine=work_car.engine;
work_car.engine="V-4";
var custom_car= new
car(fun_car.seats,work_car.engine,fun_car.theradio);
document.write("At first, I wanted a "+first_engine+"
engine.<br />");
document.write("But after thinking about it a bit:<br />");
document.write("I want a car with "+custom_car.seats+"
seats.<br />");
document.write("It also needs a "+custom_car.engine+"
engine.<br />");
document.write("Oh, and I would like a "+custom_car.theradio+"
also.");
5 یؽٙاـؼ وٚ ـر قیف ؼـ وبغشبـ ٌیِٛ ٝ ٔمبؼیف ـا ثٚ بٞا٘یؽ دبـأشفٛي ٌٕب ٔی سٚؼـ ایٗ ـ
52(0 َٔثب
77 دَلران پی سی
Hadi Ahmadi Learning Java Script
52(1 َٔثب
function get_payment() {
var the_payment=250;
the_payment += (this.seats == "leather") ? 100 : 50;
the_payment += (this.engine == "V-8") ? 150 : 75;
the_payment += (this.theradio == "CD Player") ? 35 : 10;
return the_payment;
}
function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
this.payment=get_payment;
}
var work_car= new car("cloth","V-6","Tape Deck");
var fun_car= new car("leather","V-8","CD Player");
var custom_car= new
car(fun_car.seats,work_car.engine,fun_car.theradio);
var work_car_payment= work_car.payment();
var fun_car_payment= fun_car.payment();
var custom_car_payment= custom_car.payment();
document.write("<h2>The information on the cars you
requested:</h2>");
document.write("<strong>Work Car: </strong>");
document.write(work_car.seats+","+work_car.engine+","+work_car
.theradio);
document.write("<br />");
78 دَلران پی سی
Hadi Ahmadi Learning Java Script
document.write("<strong>Payments:</strong>
$"+work_car_payment);
document.write("<p>");
document.write("<strong >Fun Car: </strong>");
document.write(fun_car.seats+","+fun_car.engine+","+fun_car.th
eradio);
document.write("<br />");
document.write("<strong>Payments:</strong>
$"+fun_car_payment);
document.write("</p>");
document.write("<p>");
document.write("<strong>Custom Car: </strong>");
document.write(custom_car.seats+","+custom_car.engine+",");
document.write(custom_car.theradio);
document.write("<br />");
document.write("<strong>Payments:</strong>
$"+custom_car_payment);
document.write("</p>");
function get_payment() {
var the_payment=250;
the_payment += (this.seats == "leather") ? 100 : 50;
the_payment += (this.engine == "V-8") ? 150 : 75;
the_payment += (this.theradio == "CD Player") ? 35 : 10;
return the_payment;
}
5 ٓیٙیىی ٔی وٛ٘ ثبقif else بیٌٙـر وبغشبـ آِٛ ٝشف' آٖ ـا ثٟ ثفای ؼـن ثٝو
function get_payment() {
var the_payment=250;
if(this.seats == "leather") {
the_payment+=100;
}
else {
the_payment+=50;
if(this.engine == "V-8") {
the_payment+=150;
}
else {
the_payment+=75;
79 دَلران پی سی
Hadi Ahmadi Learning Java Script
}
if(this.theradio == "CD Player") {
the_payment+=35;
}
else {
the_payment+=10;
}
return the_payment;
}
ٖؼٛـر ؼـوز ثِٛ ؼـٝ اوز وٜ٘ی سؼییٗ ٌؽٚ ٔی ٕ٘بییؽ ؼـ سبثغ ثبال ٌفٜؽٞ ٍٔبٝـ وٕٛٙ٘بٞ
اوزٜ ٌؽٝ دیً ففٔی ثفای آٖ ؼـ ٘ظف ٌففشٚ ایٝ ٔمؽاـ دبیٝ وthe_payment ٔشغیفٝٔمؽاـی ـا ث
ٔشغییفٝ ث75$ ب ٔمؽاـٞـٛسٛٔ اعٛ٘ ثفای وبیف اٚ 150$ ٔمؽاـv-8 ـٛسٛٔ ٌفؼؼ ٔثال ثفایٝأبف
)ؼٌٛ ٔیٜ ٔمؽاـ آٖ ثفٌٍز ؼاؼreturn ـٛ اق ؼوشٜ ؼـ آغف ثب اوشفبؼٚ ٌفؼؼٝ أبفthe_payment
)ؽٞب ـا ٔی ؼٟ٘ ؼوشىبـی ٔمبؼیف آٚ ًبی یه ٌی یب ٕ٘بیٞ دبـأشفٝ ؼوشیبثی ثٜ ٌٕب اخبقٝـ ثٛایٗ ؼوش
52(3 َٔثب
ؼوشفوی دیؽاwork_car بی ٌیٞ دبـأشفٝیر وبـ أبفی ثٞ ٖٚ ٌٕب ثؽٝؼ وٌٛ ـ قیف ثبػث ٔیٛؼوش
5 ـیؽٚ ٕ٘بیً ؼـ آٝب ـا ثٟ٘ آٚ یؽٙو
function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}
var work_car= new car("cloth","V-6","Tape Deck");
for (var prop_name in work_car) {
document.write(work_car[prop_name]+"<br />");
}
81 دَلران پی سی
Hadi Ahmadi Learning Java Script
work_car بی ٌیٞ یمٌیٚ ؽاـیٍٟ٘ ٝظیفٚ ٝ اوز وٝالغ یه آـایٚ ؼـwork_car[prop_name]
) ؼاـؼٜؽٟثفای زبح ـا ثفػ
)ؽٙ ـا ـاضز سف ٔی وٝبی آـایٞ دبـأشفٝـ لیُ ؼوشفوی ثٛؽ ؼوشٙ٘ـ ٘یك ٔبٛایٗ ؼوش
52(4 َٔثب
function car(seats,engine,theradio) {
this.seats=seats;
this.engine=engine;
this.theradio=theradio;
}
var work_car= new car("cloth","V-6","Tape Deck");
with (work_car) {
document.write("Seats: "+seats+"<br />");
document.write("Engine: "+engine+"<br />");
document.write("Radio: "+theradio);
}
ٝ ثیٍشف ثٚ شفٟ ٌٕب ثفای ؼوشفوی ثٝ ثٝؼ ؼاـؼ وٛخٚ قیبؼیٜا اوىفیذز اٌیب اق دیً سؼفیف ٌؽٚؼـ خب
)ؽٙ٘ب یبـی ٔی ـوبٞ ٔشؽٚ بٞدبـأشف
ٚ ٖـلٚ 'ْؽ ٘بٙ٘ؽ) ٔبٙٞـٌف وبـثف ـا ٔی ؼٚظیٕبر ٔفٙ سٚ اخكاٝ ؼوشیبثی ثٜ ٌٕب اخبقٝ ثnavigator اٌیب
) ٜغیف
Properties
ـٌف وبـثفٚع ٔفٛ٘ ـؼٛٔ ا٘الػبسی ؼـٝ ٌٕب لبثّیز ؼوشفوی ثٝ ثnavigator object ؼـProperties
)ؽٞـا ٔی ؼ
81 دَلران پی سی
Hadi Ahmadi Learning Java Script
Property Value
appCodeName The code name of the browser
appMinorVersion The name of the browser
appMinorVersion A string representing the minor version of the browser
appMinorVersion
(Internet Explorer only)
appVersion The version of the browser and some other information
browserLanguage The language of the browser being used (Internet Explorer and Opera)
buildID The build identifier of the browser being used (Firefox only)
cookieEnabled Specifies whether or not the browser has cookies enabled
cpuClass A string representing the class of the CPU (Internet Explorer only)
language The language of the browser being used (Firefox and Opera)
mimeTypes An array of MIME types supported by the browser
onLine Specifies whether or not the browser is in “global offline mode”
oscpu A string representing the operating system of the computer (Firefox only)
platform The machine type for which the browser was created
plugins An array of the plugins the browser has installed on it
A string representing the product name of the browser being used
product
(Firefox only)
productSub The build number of the browser being used (Firefox only)
securityPolicy An empty string—returned a value in Netscape 4.7 (Firefox only)
The default language used by the operating system (Internet Explorer
systemLanguage
only)
The natural language of the operating system (Internet Explorer and
userLanguage
Opera)
userAgent The user agent header for the browser
An empty string—returned a string representing the vendor of the
vendor
browser being used (Firefox only)
An empty string—returned the vendor version number of the browser
vendorSub
being used (Firefox only)
ٚ ف ثكـيٌٚشٗ ضفٛ٘ ٜٛ ٘طٝ ثnavigator object ؼـProperties اقٍٜبْ اوشفبؼٙٞ ؼـ5 ٟٓٔ ٝ٘ىش
)یؽٙزه ؼلز وٛو
52(,+ َٔثب
<script type="text/javascript">
window.alert("You are using "+navigator.propertyname);
</script>
82 دَلران پی سی
Hadi Ahmadi Learning Java Script
) وبـ ثجفیؽٝ ثpropertyname خبیٝا٘یؽ ثَٛ ثبال ـا ٔی سٚبی خؽٞ سٕبْ غبِیز5 ٝ٘ىش
52(,, َٔثب
)ؽٞ وبـثف ـا ٕ٘بیً ٔی ؼٜـؼ اوشفبؼٛٔ ـٌفٚ اوٓ ٔفٚ عٛ٘ وؽ قیف
<body>
<script type="text/javascript">
switch (navigator.appName) {
case "Netscape" : window.alert("Firefox/Netscape is cool.");
break;
case "Microsoft Internet Explorer" : window.alert("Internet
Explorer is Cool.");
break;
case "Opera" : window.alert("Opera is cool."); break;
default : window.alert("What browser is this?");
}
</script>
Hi, and welcome!
</body>
هتذّا
بٟ٘بی ٔػشّف اق آٞا٘یؽ ثفای ا٘دبْ وبـٛ ٔی سٝ ؼاـای سؼؽاؼی ٔشؽ ٘یك ٔی ثبٌؽ وNavigator object
) یؽٙ وٜاوشفبؼ
)یؽٙ ٔی وٜؽٞف وؽاْ ـا ٍٔبٞ ٜٔیطی ؼـ ثبـٛ سٚ بَٟ٘ قیف اوبٔی آٚؼـ خؽ
Method Purpose
javaEnabled() Used to test whether or not Java is enabled in the browser
mozIsLocallyAvailable() Checks to see if a file at a certain address is available locally
(Firefox only)
preference() Allows certain browser preferences to be set (requires signed
script)
registerContentHandler() Allows a Web site to set itself as a potential handler of a certain
MIME type
registerProtocolHandler() Allows a Web site to set itself as a potential handler of a certain
protocol
taintEnabled() Returns false— because the method is no longer being in use. It
was used to specify whether or not data tainting was enabled in
the browser
83 دَلران پی سی
Hadi Ahmadi Learning Java Script
52(,- َٔثب
)ب ـا ؼاـؼیب غیفٞ ٕ٘بیً اوىفیذزٜـٌف ٌٕب اخبقٚ آیب ٔفٝؽ وٙوؽ قیف ٍٔػُ ٔی و
ـٌفٚ خبـی ٔفٝ ِفطٝـؼ سبـیػسٛٔ ا٘الػبسی ـا ؼـٚ ٔیجبٌؽwindows ایٗ ٌی ؼـ ضمیمز خكء اٌیب
)ؽٙٓ ٔی وٞففا
52(,. َٔثب
<body>
<script type="text/javascript">
alert("Your current window has viewed "+history.length+"
pages!")
</script>
</body>
5 اوزٜ ٌؽٜـؼٚ ٔشؽ ثفای ایٗ ٌیء آَٝ قیف وٚؼـ خؽ
Method Purpose
Back() Sends the
browser window back one page in the history list
Forward() Sends the browser one page forward in the history list
Go() Sends the browser to a specified page in the history list using an integer
value
84 دَلران پی سی
Hadi Ahmadi Learning Java Script
ٝ لجُ اق ِفطٝ خبـی ؼـ ِیىز سبـیػسٝ لجُ اق ِفطٜ ٌؽٜ ؼیؽٝ آغفیٗ ِفطٝـٌف ـا ثٚایٗ ٔشؽ ٔف
)خبـی ٔی ففوشؽ
52(,/ َٔثب
<body>
<form>
<input type="button" value="Back" id="back_button" />
</form>
<script type="text/javascript" >
var bb = document.getElementById("back_button");
bb.onclick = function() {
history.back();
};
</script>
</body>
) خبـی ٔی ففوشؽٝ ثؼؽ اق ِفطٝ ؼـ ِیىز سبـیػسٜ ٌؽٝ لفاـ ٌففشٝ ِفطٝـٌف ـا ثٚایٗ ٔشؽ ٔف
52(,0 َٔثب
<body>
<form>
<input type="button" value="Back" id="back_button" /><br />
<input type="button" value="Forward" id="forward_button" />
</form>
<script type="text/javascript" >
var bb = document.getElementById("back_button");
var fb = document.getElementById("forward_button");
bb.onclick = function() {
history.back();
};
fb.onclick = function() {
history.forward();
};
</script>
</body>
85 دَلران پی سی
Hadi Ahmadi Learning Java Script
ٔثبَ 52(,1
;)history.go(-2
وؽ ثبال ؼِ ٚفط ٝؼـ ِیىز سبـیػسٔ ٝفٚـٌف ث ٝػمت ثف ٔیٍفؼؼ)
87 دَلران پی سی
Hadi Ahmadi Learning Java Script
ٖ آّٝویٚ ٝ ثٝا اوىفیذز اوز' وٚیىی خبٛ٘ ٝٔٓ سفیٗ خكء ثف٘بٟٔ DOM ٝاٖ ٌفز وٛ خفار ٔی سٝث
خبٝؽ خبثٙ٘یٓ ٔبٙب ـا ؼوشىبـی وٟ٘ ثبٌیٓ ' آٝ ؼوشفوی ؼاٌشٝؼ ؼـ ِفطٛخٛٔ بِفٙ ػٝؼ ثٛیٓ ثٞاٛلبؼـ غ
)بٟ٘ وفؼٖ آٝ أبفٚ ضؿفٚ ٖوفؼ
بییٞ يٚ وذه ـٚ یٓ دفؼاغزٞاٛ غDOMیٓ اثشؽاییٞ سٍفیص ٔفبٝ وؽ قیف اثشؽا ثٝؼٙ اق لٜثب اوشفبؼ
) یٓ ؼاؼٞاٛٔیص غٛ ـا سٜٓ وفؼٞ ـا ففاٝؼ ؼـ ِفطٛخٛٔ بِفٙ ثفای ؼوشىبـی ػٝو
53(, َٔثب
<html>
<head>
<title>DOM </title>
</head>
<body>
<h1> Learning DOM </h1>
<p>
The document object is an object that is created by the
browser for each new HTML page
(document) that is viewed.
For more information visit :
<a href=http://www.dlp.lxb.ir/ rel="external">Dehloran PC</a>
<a href="http://www.quirksmode.org/" rel="external">PPK</a>
and
<a href="http://adactio.com/" rel="external">Jeremy
Keith</a>
</p>
</body>
</html>
88 دَلران پی سی
Hadi Ahmadi Learning Java Script
ؼـ ؼـغز ثبال ٞف ٔىشٙیُ ث ٝػٛٙاٖ یه ٌف ٜؼـ ٘ظف ٌففشٔ ٝی ٌٛؼ ٚؼـ ـیٍ ٝآٖ ٌفdocument ٜ
لفاـ ؼاـؼ وٕٞ ٝیٍ ٝؼـ ثبالسفیٗ وٙص ؼـغز لفاـ ٔی ٌیفؼ)
element
ٌفٞی وٌ ٝبُٔ یه ػّٙف اق ِفط ٝثبٌؽ .ایٗ ٌفٌ ٜبُٔ یه سً آغبقی ٚیه سً دبیب٘ی ٔب٘ٙؽ
> <tag></tagیب > <tag /اوز .ایٗ ٘ٛع ٌف ٜسٟٙب ٘ٛػی اوز ؤ ٝی سٛا٘ؽ ٌبُٔ ففق٘ؽاٖ اق ا٘ٛاع ؼیٍف ثبٌؽ .
text
ایٗ ٘ٛع ٌفٞ ٜب ثٔ ٝشٗ ٔٛخٛؼ ؼـ ؼاغُ یه سً آغبقی ٚسً دبیب٘ی اٌبـ ٜؼاـ٘ؽ .ایٗ ٘ٛع ٌفٞ ٜب ٕ٘ ٓٞی
سٛا٘ٙؽ ففق٘ؽ ؼاٌش ٝثبٌٙؽ .ث ٝایٗ ٘ٛع ٌفٞ ٜب ٌ ،فٔ ٜشٙی ٔی ٌٛیٙؽ .اٌف ٌفٞ ٜبی ٔشٙی ـا ٓٞثٔ ٝثبِی وٝ
ٔب ثٌ ٝىُ قیف سجؽیُ غٛاٞؽ ٌؽ: ثفـوی وفؼیٓ أبف ٝوٙیٓ ؼـغز
attr
ٌف ٜای و ٝث ٝیه ِفز اق یه ػّٙف اٌبـٔ ٜی وٙؽ ٚفبلؽ ففق٘ؽ ٔی ثبٌؽ .ث ٝایٗ ٘ٛع ٌفٞ ٜب ٌ ،فٜ
ِفشی ٌفشٔ ٝی ٌٛؼ) ؼـ ؼـغز ٔ DOMؼٕٛال ایٗ ٌفٞ ٜب ـا ثِٛ ٝـر ؼایف ٜای ٔ ٚشُّ ثٌ ٝفٞ ٜبی
ػّٙفی ٕ٘بیً ٔی ؼٙٞؽ .ث ٝػٛٙاٖ ٔثبَ ٞف یه اق ػٙبِف ِیٙىی و ٝؼـ ٔثبَ ثبال ٍٔبٞؽٔ ٜی ٌٛؼ ؼاـای
ِفز ٞبی relو ٞ hrefىشٙؽ ؤ ٝی سٛاٖ آٖ ٞب ـا ثِٛ ٝـر قیف ٕ٘بیً ؼاؼ:
comment
ثٌ ٝفٞ ٜبی سٔٛیطی اٌبـٔ ٜی وٙؽ ٚفبلؽ ففق٘ؽ اوز .غبِجب ٌفٞی اِّی ث ٝػٛٙاٖ ـان ایٗ ؼـغز
ٚخٛؼ ؼاـؼ وٕٞ ٝبٖ documentاوز.
DOMایٗ اخبق ٜـا ث ٝخبٚا اوىفیذز ٔی ؼٞؽ سب ث ٝوبغشبـ وٙؽ ؼـ ٔفٚـٌف ؼوشفوی دیؽا وٙؽ) ٚایٗ
وبـ ـا ثب اوشفبؼ ٜاق ٚیمٌی ٞبیی ا٘دبْ ٔی ؼٞؽ و ٝؼـ خؽَ قیف آٚـؼٌ ٜؽ ٜا٘ؽ)
Property Description
activeElement Returns a string holding the value of the active element in the document
alinkColor Returns the hexadecimal value of the active link color of the document
anchors An array of all the named anchors in the document
async Used to tell the browser whether to load a document with an
asynchronous request or a synchronous request
applets An array of all the Java applets in a document
bgColor Returns the hexadecimal value of the background color of the document
body Returns the body or frameset element of the document
characterSet Returns a string value that represents the character set used to encode
the document
charset Returns a string value that represents the character set used to encode
the document
childNodes An array of all of the child nodes of the document
compatMode Returns the string “BackCompat” if the document is rendered in Quirks
modor the string “CSS1Compat” if the document is rendered in Strict
mode
contentType Returns a string for the Content-Type from the MIME header of the
document
cookie Used to set JavaScript cookies in a document
defaultCharset Returns a string value that represents the default character set used to
encode the documen
defaultView References the window object for the document
designMode Returns a string value that provides information on whether or not the
document can be edited
dir Returns a string value that represents the reading direction of the
document
doctype Returns the doctype declaration associated with the document
documentElement Returns a string representing the root node of the document
documentURIObje Returns an object representing the URI of the document (only available
ct to privileged JavaScript code)
domain Returns the domain name of the server for the document
embeds An array of all the embed tags in the document
expando Returns a Boolean value based on whether or not arbitrary variables can
be created within the document
fgColor Returns the hexadecimal value of the default text color of the document
fileCreatedDate Returns the date the document was created
fileModifiedDate Returns the date the document was last modified
formName Not a property itself, but creates a new property with each named form
placedin the document
forms An array of all the form tags in a document
frames An array of all of the frames used in the document
height Returns the height, in pixels, of the body element of the document
images An array of all the image (img) tags in the documen
implementation Returns a string value representing the implementation object of the document
91 دَلران پی سی
Hadi Ahmadi Learning Java Script
بؼ ٔیٍٟٙ دیٝؼ) اِجشٚ غیفع ثىبـ ٔی ـٚ بٟىٙیٗ ـً٘ ِیٕٙسٞ ٚ ٝٙایٗ غبِیز ثفای ـً٘ ده*دیً قٔی
)یؽٞ یب سغییف ؼٚ اػٕبَ ٕ٘بییؽCSS ب ـا ؼـٞ یمٌیٚ ٗ ایٝؼ وٌٛ
)( ـا ثفٔی ٌفؼا٘ؽsuch as <a name=“top”>) ؽٙ٘ؽ ٔبٙؼ ؼـ وٛخٛٔ anchor بیٞ ْ ای اق سٕبْ ٘بٝآـای
5 3(- َٔثب
<body>
<h1>My Page</h1>
<a name="sec1"></a>
<h2>Section 1</h2>
92 دَلران پی سی
Hadi Ahmadi Learning Java Script
)ؽ آٔؽٞاٛ ٕ٘بیً ؼـ غٝ ثanchor بیٍٟ سؼؽاؼ سٝیؽ وفؼ وٞاٛ غٜیؽٞق ٍٔبٛثب اخفای وؽ ف
1
The cookie Property
ٔیٜ ٌىُ قیف اوشفبؼٝ ثdocument ٌی.cookie ااوىفیذز اق غبِیزٚب ؼـ خبٟویٛثفای ایدبؼ و
:ٓیٙو
:یٓ وفؼٞاٛ ٌىُ قیف ػُٕ غٝ ثٜبی اق لجُ ایدبؼ ٌؽٞ ویٛ ثفای ثبقیبثی سٕبٔی وٚ
var x = document.cookie;
. مطالة مثحث کًکی عیىا از فصلی تا َمیه وام از کتاب آمًزش کارتردی جايا اسکریپت وًشتٍ آقای احمذ تادپی آيردٌ شذٌ اوذ: ٍوکت .1
93 دَلران پی سی
Hadi Ahmadi Learning Java Script
ٕٞب٘ٛٙـ و ٝؼـ ؼوشٛـ اثشؽایی ٔی ثیٙیؽ ثفای ایدبؼ وٛوی ٔی ثبیىز ـٌش ٝای ضبٚی یىىفی غٛاَ
ٔ ٚمبؼیفٌبٖ ـا ؼـ لبِت خفز ٞبی name=valueث ٝغبِیز ٘ .cookieىجز ؼٞیٓ .ؼـ خؽ َٚقیف ٞف
یه اق ایٗ لىٕز ٞب ـا ٌفش ٔی ؼٞیٓ.
ٔثبَ 5 3(.
ؼـ ٔثبَ قیف یه وٛوی ثب ٘بْ ٚ usernameثب ٔمؽاـ aliو ٝؼـ سبـیع 15/02/2010اق ثیٗ ٔی ـٚؼ
ایدبؼ ٔی ٌٛؼ:
ٔثبَ 5 3(/
ؼـ ٔثبَ قیف یه وٛوی ثب ٘بْ ٚ myCookieثب ٔمؽاـ this is my cookieایدبؼ ٌؽ ٜاوز:
٘ىش: ٝؼـ وؽ فٛق سبثغ )(.escapeیه ـٌش ٝـا ؼـیبفز وفؼ ٚ ٜسٕبٔی وبـاوشفٞبی ٘بٔؼشجف آٖ ـا ث ٝوؽ
ٔؼبؼًِ سجؽیُ ٔی وٙؽ .لجُ اق وؽ ٔؼبؼَ یه ػالٔز %لفاـ ٔی ٌیفؼ .ث ٝػٛٙاٖ ٔثبَ ایٗ سبثغ وبـاوشف
spaceـا ث ٝوؽ 20%سجؽیُ ٔی وٙؽ .ایٗ سبثغ ٔؼبؼَ سبثغ )(.encodeURIComponentاوز.
ثفای ضؿف یه وٛوی ٔی سٛاٖ اق سبثؼی و ٝقٔبٖ ا٘مٕبی وٛوی ـا ث ٝیه ثب٘ی ٝلجُ سٙظیٓ ٔی وٙؽ
اوشفبؼ ٜوٙیٓ .ایٗ سبثغ ثِٛ ٝـر قیف اوز:
ٔثبَ 5 3(0
ضبَ وبفی اوز ثفای ضؿف یه وٛوی ٘بْ آٖ ـا ثفای سبثغ فٛق ثففوشیٓ .ؼوشٛـ قیف وٛوی ثب ٘بْ
usernameـا ضؿف ٔی وٙؽ:
; )"delete_cookie ("username
ضبَ و ٝثب ایدبؼ ٚضؿف وفؼٖ وٛوی ٞب آٌٙب ٌؽیٓ ٘ط ٜٛثبقیبثی ٚؼوشفوی ث ٝآٟ٘ب ـا ثیبٖ ٔی وٙیٓ .
ثفای ثبقیبثی وٛوی ٞبیی و ٝلجال ایدبؼ ٌؽ ٜا٘ؽ ثبق ٓٞاق غبِیز ٌ .cookieی documentثِٛ ٝـر
قیف اوشفبؼٔ ٜی وٙیٓ:
;var x = document.cookie
ایٗ ؼوشٛـ ِیىشی اق خفز ٞبی name=valueسٕبٔی وٛویٟبی لبثُ ؼوشفن ثفای وٙؽ خبـی ـا وٝ
ثب ; اق ٓٞخؽا ٌؽ ٜا٘ؽ ثفٔی ٌفؼا٘ؽ ) ث ٝػٛٙاٖ ٔثبَ ٔشغیف ٔ xی سٛا٘یؽ ضبٚی ـٌش ٝای ثِٛ ٝـر قیف
ثبٌؽ:
ٔثبَ 5 3(1
ؼـ ایٗ ٔثبَ ؼ ٚوٛوی اق لجُ ایدبؼ ٌؽ ٜاوز :یىی ثب ٘بْ ٔ ٚ usernameمؽاـ ٚ aliؼٔٚی ثب ٘بْ
passwordثب ٔمؽاـ .abc123
او x ٖٛٙیه ٔشغیف ـٌش ٝای وبؼ ٜاوز ؤ ٝی سٛا٘یٓ ثفای ؼوشفوی ثٞ ٝف یه اق وٛوی ٞب ٚ
ٔمؽاـٌبٖ اثشؽا xـا ثٛویّٔ ٝشؽ ٌ splitی stringث ٝآـای ٝای سجؽیُ وفؼ ٚ ٜثٛویّٔ ٝشؽٞبی غبَ آـایٝ
ث ٝآٖ ٞب ؼوشفوی ؼاٌش ٝثبٌیٓ .ث ٝػٛٙاٖ ٔثبَ ثفای زبح ٔمؽاـ وٛوی ٞبی فٛق ٔی سٛاٖ ثِٛ ٝـر قیف
ػُٕ وفؼ:
ٔثبَ 5 3(2
<body>
<script type="text/javascript">
window.alert("You have reached the "+document.domain+"
domain!");
</script>
</body>
ٍؽاـ قیف ـا ثف ٔیٞ ثبٌؽٝ لفاـ ٌففشdlp.lxb.ir 5 فیه اق ِفطبر یه وبیز ٔثالٞ اٌف ایٗ وؽ ؼـ
5ٌفؼا٘ؽ
وبـ ٔی آیؽٝ قٔب٘ی ثٝؼ یه غبِیز ٘یىز) ثّىٛؼی غٛ غٝالغ ثٚ ؼـThe formname Property
ٚ ٜ ٔثبَ قیف ؼلز وفؼّٝت ثٙٔ ٖ ٌؽٝخٛ ثبٌیؽ ثفای ٔشٝ سجغ آٖ ٘بْ یه ففْ ـا ؼاٌشٝ ثٚ ْ ٌٕب یه ففٝو
5 آٖ ـا اخفا ٕ٘بییؽ
5 3(4 َٔثب
<body>
<form name="funform">
<input type="button" name="funb" value="You can click me I
suppose"
onclick="document.funform.funb.value='Thanks, you clicked
me!';" />
</form>
</body>
97 دَلران پی سی
Hadi Ahmadi Learning Java Script
5 3(,+ َٔثب
<body>
<h1>My Always Updated Web Page!</H1>
<script type="text/javascript">
document.write("Last Updated: "+document.lastModified);
</script>
</body>
5 3(,, َٔثب
if (document.layers) {
window.alert("You have Netscape Navigator 4!");
}
5 3(,- َٔثب
if (document.all) {
window.alert("You have Internet Explorer 4 or better!");
}
98 دَلران پی سی
Hadi Ahmadi Learning Java Script
)یٙبوبیی وٌٙ ـاٝؼ ؼـ ِفطٛخٛٔ بیٞ هٙا٘یؽ سؼؽاؼ ِیٛ اق ایٗ غبِیز ٌٕب ٔی سٜثب اوشفبؼ
5 3(,. َٔثب
<body>
<a href="http://www.dlp.lxb.ir" >
<a href="http://www.dlp.loxblog.com">
<script type="text/javascript">
document.write(document.links.length);
</script>
</body>
5 3(,/ َٔثب
<head>
<title>JavaScript!</title>
</head>
<body>
<script type="text/javascript">
document.write("<h1>" + document.title + "</h1>");
</script>
Learning javascript with us!
</body>
<body>
<h1>Buy Something!</h1>
If you don't buy something I will be really upset so you had
better...
99 دَلران پی سی
Hadi Ahmadi Learning Java Script
<br/><br/>
<script type="text/javascript">
document.write("You are at: "+document.URL);
</script>
</body>
#ٖ وفؼencode$ ـٔكٌٍبییٌٝ٘ٛفٞ ٖٚ خبـی ـا ثفٔی ٌفؼا٘ؽ' ثؽٝآؼـن وبُٔ ِفط
ؼ ؼـ ضبِیٌٛ ٜ ٕ٘بیً ؼاؼ%20 ـرِٛ ٝ فٕبی غبِی ثٝ ٕٔىٗ اوز وURL Property ـ ٔثبَ ؼـٛ٘ ٝث
)ؼٌٛ ٌی ٔیٛ اق ٕ٘بیً آٖ زٍٓ دURLUnencoded Property ؼـٝو
ٗ اق ایٜ اوز) ثب اوشفبؼٜ ٌؽٜـؼٚب ـا آٟ٘ـؼ آٛٔ ٔیطبسی ؼـٛ ٔشٚ بٞفیه اق ایٗ ٔشؽٞ َْ قیف ٘بٚؼـ خؽ
)یؽٙاـؼ وٚ ؼٛبؼ غٙ اوٝبی خؽیؽی ـا ثٞ لبثّیزٚ یمٌیٚ ا٘یؽٛب ٌٕب ٔی سٞٔشؽ
Method Description
Attaches a function to an event, so that the function runs when
attachEvent()
the eventoccurs (Internet Explorer only)
createAttribute() Creates an attribute with a name that is sent to it as a parameter
createAttributeNS() Creates a new attribute in a particular namespace
createCDATASection() Creates a new CDATA section
Creates a comment with the value that is sent to it as a
createComment()
parameter
createDocumentFragment() Creates a new document fragment
createElement() Creates an element of the type sent to it as a parameter
Creates an element in a particular URI and a particular type sent
createElementNS()
to it as parameters
createEntityReference() Creates a new entity reference
createEvent() Creates an event
Creates an event object for the purpose of passing event
createEventObject()
information
createNodeIterator() Creates a node iterator object
createNSResolver() Creates a namespace resolver
createProcessingInstruction() Creates a processing instruction
createRange() Creates a range object
Creates a style sheet for the document to use (Internet Explorer
createStyleSheet()
only)
111 دَلران پی سی
Hadi Ahmadi Learning Java Script
getElementById() هتذ
آیؽی آٖ ؼوشفوی دیؽاّٝویٚ ٝ ثٝبی یه خكء اق ِفطٟ غبِیشٝ ثٝؽ وٞ ٔی ؼٜ ٌٕب اخبقٝایٗ ٔشؽ ث
)یؽٙو
5 3(,1 َٔثب
getElementByName() هتذ
)ؼٌٛ ٔیٜب ثفاثف ٔمؽاـی غبَ اوز اوشفبؼٟ٘ آname ِفزٝ وٝبِفی اق ِفطٙ ػٝثفای ؼوشفوی ث
5 3(,2 َٔثب
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<form method=”post” action=”dosomething.php”>
<fieldset>
<legend>What color do you like?</legend>
<input type=”radio” name=”radColor” value=”red” /> Red<br />
<input type=”radio” name=”radColor” value=”green” /> Green<br
/>
<input type=”radio” name=”radColor” value=”blue” /> Blue<br />
</fieldset>
<input type=”submit” value=”Submit” />
</form>
</body>
</html>
ایٗ ِفط ٝـً٘ ٔٛـؼ ػالل ٝوبـثف ـا وٛاَ ٔی وٙؽ)ٞ radiobuttonب اوٓ یىىب٘ی ؼاـ٘ؽ .أب ٔی
غٛاٞیٓ فمٗ ٔمؽاـradiobuttonی ـا و ٝا٘شػبة ٌؽ ٜاوز ـا دیؽا وٙیٓ)ثفای ایٗ أف اق وؽ قیف اوشفبؼٜ
ٔی وٙیٓ5
ضبَ ٔی سٛا٘یؽ اق ٕٞبٖ ـٚي لجّی ثفای ث ٝؼوز آٚـؼٖ ٔمؽاـ ٞف اق ٞradiobuttonب ث ٝـٚي قیف
ػُٕ وٙیؽ:
ٕٞب٘ٙؽ ٔشؽٞبی ثبال ػُٕ ٔی وٙؽ ثب ایٗ سفبٚر و ٝاق ٘بْ والوی و ٝث ٝػٙبِف سػّیّی یبفش ٝاوز ثفای
ؼوشفوی ث ٝآٟ٘ب اوشفبؼٔ ٜی وٙؽ)
ٔثبَ 5 3(,4
ففْ وٙیؽ ٔی غٛاٞیٓ ث ٝسٕبْ اخكایی و ٝوالن آٟ٘ب number_oneاوز ـا ؼوشفوی دیؽا وٙیٓ
ثؽیٗ ٔٙظٛـ اق لٙؼ ٝوؽ قیف اوشفبؼٔ ٜی وٙیٓ5
هتذ )(getElementsByTagName
ایٗ ٔشؽ ثٌٕ ٝب اخبق ٜؼوشیبثی ث ٝآـای ٝای اق ػٙبِفی ـا ٔی ؼٞؽ و ٝؼاـای سً ٍٔػُ ٞىشٙؽ)
ٔثبَ 5 3(-+
ـ اقٛظٙٔ ٗیٓ ثؽیٙؽ ؼوشفوی دیؽا وٙىشٞ img ً ؼاـای سٝبِفی وٙ سٕبْ ػٝیٓ ثٞاٛیؽ ٔی غٙففْ و
5ٓیٙ ٔی وٜوؽ قیف اوشفبؼ
ثفای دبیبٖ آٖ اق ٔشؽٚ ؽٌٞشٗ ؼـ آٖ ـا ٔی ؼٛ٘ ٚ خؽیؽٝ وبغز یه ِفطٜ ٌٕب اخبقٝ ثopen() ٔشؽ
) ٔی ٌفؼؼٜ اوشفبؼclose()
5 3(-, َٔثب
ٜ خؽیؽ ثبق ٌؽٝ وفؼٖ آٖ یه ِفطsubmit ؽ وذه ثبٙاـؼ ٔی وٚ ؼ ـاٛؼـ ٔثبَ قیف وبـثف اثشؽا ٘بْ غ
)یؽٌٛ ي آٔؽ ٔیٛ غٜاـؼ وفؼٚ ؼـ اثشؽاٝؼ وبـثف ـا ثب ؾوف ٘بٔی وٚـٚ ٝو
<body>
<strong>Enter your name in the box below, then click
the button to see a personalized page!</strong>
<br />
<form id="newp" onsubmit="newpage();">
Name: <input type="text" id="yourname" size="25">
<br/><br/>
<input type="submit" value="Submit">
</form>
<script type="text/javascript" >
function newpage() {
var thename = document.getElementById("yourname").value;
document.open();
document.write("<h1>Welcome!</h1>");
document.write("Hello, "+thename+", and welcome to my page!");
document.close();
}
</script>
</body>
Property Description
attributes An array of all of the attributes in the specified node; the name and
value properties of this property can be used to access the attribute
name or attribute value for each member of the array
childNodes An array of all the child nodes of the specified node
className Returns the value of the class attribute of the specified node
clientHeight Returns the height, in pixels, of the specified node
clientWidth Returns the width, in pixels, of the specified node
dir Returns the value of the direction of the text in the specified node (ltr or
rtl)
firstChild Returns the first child node of the specified node
id Returns the value of the id of the specified node
innerHTML Returns the HTML code (text, image code, tags, etc.) within the
specified node, such as all of the HTML code within a div element
lang Returns the language value of the specified node
lastChild Returns the last child node of the specified node
nextSibling Returns the node following the specified node
nodeName Returns the name of the specified node (such as div for a div element)
nodeType Returns the type of the specified node
nodeValue Returns the value of the specified node (such as the text within a div
element or the value of an attribute)
offsetHeight Returns the offset height of the specified node
offsetWidth Returns the offset width of the specified node
ownerDocument Returns the document object that contains the specified node
parentNode Returns the parent node of the specified node
previousSibling Returns the node before the specified node
scrollLeft Returns the difference between the left edge and the left edge in view of
the specified node
scrollTop Returns the difference between the top edge and the top edge in view of
the specified node
scrollHeight Returns the entire height (including anything hidden and viewable via a
scroll bar) of the specified node
scrollWidth Returns the entire width (including anything hidden and viewable via a
scroll bar) of the specified node
style Returns the style object of the specified node
tabIndex Returns the tab index of the specified node
tagName Returns the tag name (in uppercase) of the specified node
title Returns the value of the title attribute of the specified node
5 3(-- َٔثب
<body>
<div id="div1" title="All about me!">
This page is about me, me, and... me!
</div>
<script type="text/javascript">
var me_div = document.getElementById("div1");
var me_title = me_div.title;
window.alert("The title of the div element is" + me_title);
</script>
</body>
Method Description
addEventListener() Adds an event listener to the specified node to run a function
on the
event sent to it as a parameter
appendChild() Appends a node as the last child of the specified node
attachEvent() Attaches an event to the specified node to run a function on
the eventsent to it as a parameter
blur() Removes focus from the specified node
click() Executes the click event on the specified node
cloneNode() Creates a clone of the specified node
detachEvent() Detaches an event from the specified node
dispatchEvent() Executes an event on the specified node
focus() Gives focus to the specified node
getAttribute() Returns the value of the attribute name sent to it as a
parameter on the specified node
getAttributeNS() Returns the value of the attribute name and namespace sent
to it as a parameter on the specified node
getAttributeNode() Returns the attribute node of the attribute name sent to it as a
parameter for the specified node
getAttributeNodeNS() Returns the attribute node of the attribute name and
namespace sent to it as parameters for the specified node
getElementsByTagName() An array of all the child element nodes with the tag name
sent to it as a parameter in the specified node
116 دَلران پی سی
Hadi Ahmadi Learning Java Script
getElementsByTagNameNS() An array of all the child element nodes with the tag name and
namespace sent to it as parameters in the specified node
hasAttribute() Returns true if the attribute name sent to it as a parameter
exists on the specified node, or false if not
hasAttributeNS() Returns true if the attribute name and namespace sent to it as
parameters exist on the specified node, or false if no
hasAttributes() Returns true if the specified node has any attribute nodes
defined, or false if not
hasChildNodes() Returns true if the specified node has any child nodes. or
false if not
insertBefore() Inserts a node sent to it as a parameter before the node sent to
it as a second parameter inside the specified node
normalize() Normalizes the specified node
removeAttribute() Removes the attribute node for the attribute name sent to it as
a parameter from the specified node
removeAttributeNode() Removes the attribute node for the attribute node object
reference sent to it as a parameter from the specified node
removeAttributeNS() Removes the attribute node for the attribute name sent to it as
a parameter with the namespace sent to it as a parameter
from the specified node
removeChild() Removes the child node sent to it as a parameter from the
specified node
removeEventListener() Removes an event listener from the specified node
replaceChild() Replaces the child node sent to it as the second parameter
with the child node sent to it as the first parameter in the
specified node
scrollIntoView() Scrolls the specified node into view in the browser window
setAttribute() Sets an attribute node’s name (first parameter) and value
(second parameter) for the specified node
setAttributeNode() Sets an attribute node as the attribute node object sent to it as
a parameter for the specified node
setAttributeNodeNS() Sets an attribute node as the attribute node object sent to it as
a parameter with the namespace sent to it as a parameter for
the specified node
setAttributeNS() Sets an attribute node’s namespace (first parameter), name
(secondparameter), and value (third parameter) for the specified node
5 3(-. َٔثب
<body>
<div id="div1" title="All about me!">
This page is about me, me, and... me!
</div>
</body>
ٖ آٝبی ؼیٍفی ـا ثٞ ٜیؽ ٌفٞاٛ اوز) اٌف ٌٕب ثػtext ففق٘ؽٜ یه ٌفٚ div ٜایٗ وؽ ؼاـای یه ٌف
ٜ ٔشؽ ٌفٚ ' document.createTextNode()'document.createElement() یؽ ثبیؽ اقٙ وٝأبف
)یؽٙ وٜ اوشفبؼappendChild() 'DOM
5یؽٙ ؼوشفوی دیؽا وdiv ٝ اق وؽ قیف ثٜ ثب اوشفبؼٚ ٝا اوىفیذز ـفشٚ وؽ خبٝاثشؽا ث
5 3(-/ َٔثب
inner_div ٜاٖ یىی اق ففق٘ؽاٖ ٌفٛٙ ػٝ ـا ثٜ ایٗ ٌفappendChild() سپس با استفادٍ اس هتذ
)ا٘شػبة ٕ٘بییؽ
inner_div.appendChild(inner_div_text);
me_div.appendChild(inner_div);
ؼـ ِفطبرCSS ثیٍشف اقٚ شفٟ ثٜبیی ثفای اوشفبؼٞ يٚ ـٚ بٞ یهٙؼ سىٌٛ ؼـ ایٗ ثػً وؼی ٔی
)ؼٌٛ ٜقي ؼاؼٛٔؼ آٛغ
ٗ ٕٔىٝؼ ٔی ثبٌؽ اٌفزٛخٛٔ css ؼـٝبیی اوز وٟیمٌیٚ ٚ َاٛ ٌبُٔ سٕبْ غstyle object ٜقٚأف
ؼـbackground-color َـ ٔثبٛ٘ ٝ ثبٌؽ ثٝؼ ؼاٌشٛخٚ رٚب سفبٞ یمٌیٚ ٗاوز وٕی ؼـ ٘بٍٔؿاـی ای
)ؼٚ وبـ ٔی ـٝ ثstyle.backgroundColor ـرِٛ ٝ ثCSS
)یؽٙ ٔی وٜؽٞ ـا ٍٔبDOM ب ؼـٟ٘ ٔؼبؼَ آٚ CSS ؼ ؼـٛخٛٔ بیٞ یمٌیٚ َ قیفٚؼـ خؽ
5 3(-0 َٔثب
5 ٓیٙ سؼفیف ٔی وobject property ٜ ثب اوشفبؼٚ بی قیفٟیمٌیٚ ثبborder ؼـ ٔثبَ قیف یه
5 3(-1 َٔثب
<html>
<head>
<title>Style Example</title>
<script type=”text/javascript”>
function sayStyle() {
var oDiv = document.getElementById(“div1”);
alert(oDiv.style.backgroundColor);
}
</script>
</head>
<body>
<div id=”div1” style=”background-color: red; height: 50px;
width:
50px”></div><br />
<input type=”button” value=”Get Background Color”
onclick=”sayStyle()” />
</body>
</html>
5 3(-2 َٔثب
آثیٝ آٖ ثٜؼٚ ٔطؽٝن ثٚؼ ٍ٘بٍ٘ف ٔبٚـٚ ثبٝ ٕ٘بیً ؼـ ٔی آیؽ وٝ اق وؽ قیف یه ٔفثغ لفٔك ثٜثب اوشفبؼ
)ؼ ثبق ٔی ٌفؼؼٛ ـً٘ لجّی غٝ ٔفثغ ثٜؼٚن اق ٔطؽٚج ٍ٘بٍ٘ف ٔبٚ ثب غفٚ ؽٞسغییف ـً٘ ٔی ؼ
<html>
<head>
<title>Style Example</title>
</head>
<body>
<div id=”div1”
style=”background-color: red; height: 50px; width: 50px”
onmouseover=”this.style.backgroundColor = „blue‟”
onmouseout=”this.style.backgroundColor = „red‟”></div>
</body>
</html>
5 3(-3 َٔثب
وؽٝؼٙ اق لٜیؽ ثب اوشفبؼٙی ٔفثغ لفٔك ـً٘ وّیه ٔی وٚ ـٍٝبٔی وٙٞ ؼـ ٔثبَ قیف
)ؽٙ ٕ٘بیً ؼـ ٔی آیٝبی ٔفثغ ثٟیمٌیٚ this.style.cssText
<html>
<head>
<title>Style Example</title>
</head>
<body>
<div id=”div1”
style=”background-color: red; height: 50px; width: 50px”
111 دَلران پی سی
Hadi Ahmadi Learning Java Script
onclick=”alert(this.style.cssText)”></div>
</body>
</html>
Css ٚ DOM ٗ ػُٕ سجؽیُ ٔشمبثُ ـا ثیٝبیی اوز وٞـؼ ٔشؽٛٔ ؼـDOM style methods
)ٓیٌٙٗ سف ٔی وٚ ایٗ ٔجطث ـا ـٚ بی قیفٞ َ ٔثبٚ َٚ اق خؽٜؽ )ثب اوشفبؼٙٞا٘دبْ ٔی ؼ
5 3(-4 َٔثب
<html>
<head>
<title>Style Example</title>
<script type=”text/javascript”>
function useMethods() {
var oDiv = document.getElementById(“div1”);
alert(oDiv.style.item(0)); //outputs “background-
color”
alert(oDiv.style.getPropertyValue(“background-color”));
}
</script>
</head>
<body>
background- ٖٕٕٛٔ ٍؽاـ ثبٞ ْؼ اثشؽا یه دیبٌٛ ٔیٜ فٍفؼٝؼ ؼـ ِفطٛخٛٔ ٕٝ ؼوٝقٔب٘ی و
ؼـٝ وdiv یٖٝٙ ٘بْ ـً٘ قٔیٕٕٛٔ ٍؽاـ ؼیٍفی ثبٞ ْ وذه ثب سبییؽ آٖ دیبٚ ؼٌٛ ٔیٜ ٕ٘بیً ؼاؼcolor
)ؼٌٛ ٔیٜ اوز ٕ٘بیً ؼاؼred دبٙای
Custom tooltips
ٔیطبسیٛالغ سٚ بوز ایٗ اثكاـ ؼـٞtooltip اقٜ اوشفبؼstyle object خؿاةٜاـؼ اوشفبؼٛٔ یىی ؼیٍف اق
)٘ؽٌٛ ٔیٜ لفاـ ٔی ٌیفؼ ٕ٘بیً ؼاؼٝی یىی اق اٌیبی ِفطٚن ـٚ ٍ٘بٍ٘ف ٔبٝ قٔب٘ی وٝؽ وٙىشٞ
5 3(.+ َٔثب
<html>
<head>
<title>Style Example</title>
<script type=”text/javascript”>
function showTip(oEvent) {
var oDiv = document.getElementById(“divTip1”);
oDiv.style.visibility = “visible”;
oDiv.style.left = oEvent.clientX + 5;
oDiv.style.top = oEvent.clientY + 5;
}
function hideTip(oEvent) {
var oDiv = document.getElementById(“divTip1”);
oDiv.style.visibility = “hidden”;
}
</script>
</head>
<body>
<p>Move your mouse over the red square.</p>
<div id=”div1”
style=”background-color: red; height: 50px; width: 50px”
onmouseover=”showTip(event)”
onmouseout=”hideTip(event)”></div>
<div id=”divTip1”
style=”background-color: yellow; position: absolute;
visibility:
hidden; padding: 5px”>
<span style=”font-weight: bold”>Custom Tooltip</span><br />
More details can go here.
</div>
</body>
</html>
ی آٖ لفاـ ٔیٚن ـٚ ٍ٘بٍ٘ف ٔبٝ قٔب٘یىٝ ٕ٘بیً ؼـ ٔی آیؽ وٝثب اخفا ٌؽٖ وؽ ثبال یه ٔفثغ لفٔك ـً٘ ث
More ٖٕٕٛٔ ٚ ً٘ ی قـؼ ـٝٙن دیبٔی ثب ده قٔیٚ وٕز زخ ٍ٘بٍ٘ف ٔبٚ ٌٗیفؼ ؼـ لىٕز دبیی
) ٕ٘بیً ؼـ ٔی آیؽٝ ثdetails can go here.
Collapsible sections
Collapsible section اقٜؼ اوشفبؼٚة ثىبـ ٔی ـٚ ثفای خؿاة ٌؽٖ ِفطبرٝیىی ؼیٍف اق اثكاـی و
یؽ سبٞ ـا ثؽٜ وبـثف ایٗ اخبقٝ ثٝـؼ ایٗ اوز وٛٔ ٗ ی اِّی ؼـ ایٜ ای ٔی ثبٌؽ)ایؽٜبی وفوفٞٛٙٔ ب یبٞ
)ب ٘ؽاـؼ ـا ٔػفی ٕ٘بیؽٟ٘ ؼیؽٖ آٝ سٕبیُ ثٝ ـا وٝبیی اق ِفطٞ لىٕز
5 3(., َٔثب
<html>
<head>
<title>Style Example</title>
<script type=”text/javascript”>
function toggle(sDivId) {
var oDiv = document.getElementById(sDivId);
oDiv.style.display = (oDiv.style.display == “none”) ? “block”
:
“none”;
}
</script>
</head>
<body>
<div style=”background-color: blue; color: white; font-weight:
bold;
padding: 10px; cursor: pointer”
onclick=”toggle(„divContent1‟)”>Click Here</div>
<div style=”border: 3px solid blue; height: 100px; padding:
10px”
id=”divContent1”>This is some content
to show and hide.</div>
<p> </p>
<div style=”background-color: blue; color: white; font-weight:
bold;
padding: 10px; cursor: pointer”
onclick=”toggle(„divContent2‟)”>Click Here</div>
<div style=”border: 3px solid blue; height: 100px; padding:
10px”
id=”divContent2”>This is some content
to show and hide.</div>
</body>
</html>
) اوزٜ سً ٘فاضی ٌؽٚ ثفای سغییف یب اِالش ٔشٗ ثیٗ ؼinnerText property
5 3(.- َٔثب
<div>New text for ٝیؽ آٖ ـا ثٞاٛ ٔی غٝ< ؼاـیؽ وdiv/> یؽ ٌٕب یه سً غبِیٙففْ و
5 ـر قیف ا٘دبْ ٔی ٌیفؼِٛ ٝ ایٗ ػُٕ ثDOM اقٜیؽ ثب اوشفبؼٞ سغیف ؼthediv.</div>
innerText اقٜ ٔی ثبٌؽثب اوشفبؼٜؽٙٙ وٝالؼی غىشٛٔ ؼـٚ ال٘یٛ٘ ِیٚ ایٗ وؽ قیبؼ ٍٔىُ ٘یىز
5 ـر قیف ا٘دبْ ؼیؽِٛ ٝا٘یؽ آٖ ـا ثٛٔی س
5 3(.. َٔثب
ٜ ٌؽٜ ٍ٘بٖ ؼاؼinnerHTML ٚ innerText رٚ اق یه وؽ ٍٔػُ سفبٜ ثب اوشفبؼٚ َ قیفٚؼـ خؽ
)اوز
بیٞ وؽinnerHTML ٌٝشبـی ـا ثف ٔی ٌفؼا٘ؽ ؼـ ضبِیىٛ٘ بیٞ ً فمٗ ثػinnerText الغٚ ؼـ
)ب ـا ثفٔی ٌفؼا٘ؽٞ ٗ ٔشٚ ' سٕبْ اخكاٖٝ ثٛ ٔفثHTML
)ؽٙ خب ٔی وٝی خبثٙ ٔشٜ ـا ثب یه ٌفٜ ٌفٝر وٚ اوز ثب ایٓ سبinnerText ؽٕٙ٘بٞ
5 3(./ َٔثب
5 3(.0 َٔثب
var oP = document.createElement(“p”);
oP.appendChild(document.createTextNode(“This is a paragraph.
“));
var oDivParent = oDiv.parentNode;
oDivParent.replaceChild(oP, oDiv);
) اوزٜ ٌؽٜ ٍ٘بٖ ؼاؼouterHTML ٚ outerText رٚ اق یه وؽ ٍٔػُ سفبٜ ثب اوشفبؼٚ َ قیفٚؼـ خؽ
5 3(.1 َٔثب
یؽٞؼ ؼـ ٔثبَ قیف ـا فٍبـ ٔی ؼٛخٛٔ ٕٝ ٌٕب ؼوٝ< قٔب٘ی وdiv/> ُٔی ٌبٙ ٔشٜ ثب یه ٌفThis is my
ؼٌٛ خبیٍكیٗ ٔیoriginal text.
<html>
<head>
<title>OuterText Example</title>
<style type=”text/css”>
div.special {
background-color: red;
padding: 10px;
}
</style>
<script type=”text/javascript”>
function useOuterText() {
var oDiv = document.getElementById(“div1”);
oDiv.outerText = oDiv.outerText;
alert(document.getElementById(“div1”));
}
</script>
</head>
<body>
<div id=”div1” class=”special”>This is my original text</div>
<input type=”button” value=”Use OuterText”
onclick=”useOuterText()” />
</body>
</html>
ُاضیای پٌجز
ؼـٝـ وٕٛٙ٘بٞ ٚ )٘ؽٌٛ ٔیٝاٖ ٌی ؼـ ٘ظف ٌففشٛٙ ػٝ ثٜ غیفٚ بٞ ٓب' ففیٞ ٜدفٙا اوىفیذز دٚؼـ خب
بٞـؼ ٔشؽٛٔ سفّیُ ؼـِٝیبسی ٘یبق ؼاـیٓ) ؼـ یٗ فُّ ثّٛ غٚ بٞ ٔشؽَٝ لجُ ؼیؽیٓ' ثفای وبـ ثب اٌیب ثّٛف
ٜ وفؼٜب آٔبؼٞ ٜدفٙـ اغشّبِی ثفای وبـ ثب دٛ٘ ٝااوىفیذز ثٚ خبٝیٓ ٌفز وٞاِٛیبسی وػٗ غّٛ غٚ
)اوز
Properties_خصَصیات
)ٓیٌٛ بٌِٙیبر ایٗ ٌی آّٛیؽ سب اثشؽا ثب غٞ ؼٜ اخبقٜدفٙبی ٌی دٞع وبـ ثب ٔشؽٚثفای ٌف
)ف وؽاْ اوزٞ ـؼٛٔ ٔیطبسی ؼـٛ سٜٕفاٞ ِٝیبر ثَّٛ قیف ٌبُٔ ِیىشی اق ایٗ غٚخؽ
Property Description
closed Holds the value based on whether or not a window has been closed
defaultStatus Defines the default message displayed in the status bar
document A reference to the document object of the window
frames An array that represents all of the frames in a given window
history Provides information on the browser history of the current window
innerHeight Returns the height, in pixels, of the viewable area within the window
innerWidth Returns the width, in pixels, of the viewable area within the window
length Holds a value equal to the number of frames in a window
location Holds the value of the current URL of the window
name Enables a window to be named
opener Refers to the window that opened another window
outerHeight Returns the width, in pixels, of the entire browser window
outerWidth Returns the width, in pixels, of the entire browser window
parent Refers to the frame set that contains the current frame
screen.availWidth Returns the available width of the browser window in pixels
screen.availHeight Returns the available height of the browser window in pixels
screen.colorDepth Returns the color depth of the screen in bits per pixel
screen.height Returns the total height of the screen in pixels
screen.pixelDepth Returns the bit depth of the screen in bits per pixel
screen.width Returns the total width of the screen in pixels
self Provides another way to reference the current window
status Enables a message to be placed in the status bar; overrides defaultStatus
top A reference to the top window containing a frame, frame set, or nested
frame set
window A reference to the current window
ٚ ' اوز یب غیف؟ٜ ٌؽٝ ای ثب ٘بْ ٍٔػُ ثىشٜدفٙ آیب دٝؽ وٙ زه ٔی وٝىز وٙ ایٗ غبِیز ایٝظیفٚ
ٜدفٙ٘ؽ) ایٗ غبِیز ػُٕ ٔفؼ ٘ظفي ـا ثف اوبن ٘بْ دٌٛ بی ؼاغُ آٖ اخفا ٔیٞؼٖ وؽٛ ثٝـر ثىشِٛ ؼـ
)ؽٞب ا٘دبْ ٔی ؼٞ
if (windowname.closed) {
JavaScript Statements
}
innerHeight Property
ٗؽ' ایٙؽاـی ٔی وٍٟ٘ ؽ ـاٙ ٔی وٜؽٞ وبـثف ٍٔبٝ وٜدفٙ ای اق دٜؼٚایٗ غبِیز ٔمؽاـ اـسفبع ٔطؽ
ِیز ؼـّٛؼ) ایٗ غٌٛ ـٌف ٕ٘یِٚیبر ٔفّٛ ؼیٍف غٚ ِجبـٛثبـ' سٛٙٔ 'َ ثبـٚغبِیز ٌبُٔ اوىف
)ــ ثف ٕ٘ی ٌفؼا٘ؽّٛشف٘ز اوىذٙ ای ـا ؼـ ایِٝی ٘شیدٚ ؽٙ فبیففبوه وبـ ٔی وٚ بی ادفاٞـٌفٚٔف
innerWidth Property
ٗؽ' ایٙؽاـی ٔی وٍٟ٘ ؽ ـاٙ ٔی وٜؽٞ وبـثف ٍٔبٝ وٜدفٙ ای اق دٜؼٚایٗ غبِیز ٔمؽاـ ػفْ ٔطؽ
ِیز ؼـّٛؼ) ایٗ غٌٛ ـٌف ٕ٘یِٚیبر ٔفّٛ ؼیٍف غٚ ِجبـٛثبـ' سٛٙٔ 'َ ثبـٚغبِیز ٌبُٔ اوىف
)ــ ثف ٕ٘ی ٌفؼا٘ؽّٛشف٘ز اوىذٙ ای ـا ؼـ ایِٝی ٘شیدٚ ؽٙ فبیففبوه وبـ ٔی وٚ بی ادفاٞـٌفٚٔف
5 4(, َٔثب
ؽ ٔشؽٕٙ٘بٞ خؽ ٔی ثبٌؽ) ؼـوزٛٔ ٜدفٙ سؼؽاؼ ففیٓ ؼـ دٝ زٝیؽ وٌٛ ٌٕب ٔیٝایٗ غبِیز ث
) window.frames.length
هٙ ِیٕٝیمٌی ثفای ایدبؼ ؼوٚ ٗ٘یؽ اق ایٛ ٔی ؼاـ ؼٌٕب ٔی سٍٝ٘ خبـی ـاٝ ِفطURL ایٗ ٔشؽ
) ٕ٘بییؽٜاوشفبؼ
5 4(- َٔثب
<head>
<title>Page has moved</title>
<script type="text/javascript">
window.location="page2.html";
</script>
</head>
<body>
Lacking JavaScript? Click the link below for the new page
then!
<br />
<a href="page2.html">New Page</a>
</body>
ٝؽ سب یه ٘بْ ـا ثٞ ٌٕب ایٗ أىبٖ ٔی ؼٝ ثٚ ؼٚ وبـ ٔی ـٝ خبـی ثٝؽاـی ٘بْ ِفطٍٟ٘ ایٗ ٔشؽ ثفای
)یؽٞ ای اغشّبَ ؼِٝفط
5 4(. َٔثب
<body>
<script type="text/javascript">
window.name="cool_window";
document.write("This window is named "+window.name);
</script>
</body>
) ٕ٘بیً ٔی ٌؿاـؼٝ ثٝؼ ِفطٚ ؽ وذه آٖ ـا ؼـٞ اغشّبَ ٔی ؼٝ ِفطٝایٗ ٔثبَ یه ٘بْ ـا ث
ٝ قٔب٘ی اسفبق ٔی افشؽ وٚ ) اوزٜ ای ؼیٍف ـا ثبق وفؼٜدفٙ دٝ ای اوز وٜدفٙ دٝ ثٝایٗ ٔشؽ ثفای ٔفاخؼ
)ؼٌٛ ثبقopen() ٔشؽّٝویٚ ٝ خؽیؽ ثٜدفٙیه د
ٌٕب ایٗ أىبٖ ـاٝ ثٚ ؽٌٙ ثبٝؼ ؼاٌشٛخٚ ٝؽ ففیٓ ؼـ ِفطٙ زٝؼ وٚ وبـ ٔی ـٍٝبٔی ثٙٞ ٗایٗ ٔشؽ فم
)یؽٙ ؼوشفوی دیؽا وparent frame set ٝؽ سب ثٞٔی ؼ
ٜدفٙؽ یه ٌی دٙ٘ااوىفیذز) ایٗ ٔشؽ ٔبٚ خبـی ؼـ خبٝی اوز ثفای ِؽا وفؼٖ ِفطٞایٗ ٔشؽی ـا
)ؽ ٌفؼؼٙٔ ٜفٟ خبـی ثٜدفٙبی ٌی دٞ ا٘ؽ اق سٕبْ غبِیزٛ ٔی سٚ ؼٌٛ ٔیٜثىبـ ثفؼ
هتذّا
بی ایٗ ٌیٞ سب ثب ٔشؽٜثز آٖ ـویؽٛ٘ ایؽٜب ٌؽٌٙ آٜدفٙبی ٌی دٞ یمٌیٚ ٌٕب ثب سؼؽاؼی اقٝضبَ و
)یؽٌٛ بٌٙ٘یك آ
Method Description
alert() Pops up an alert to the viewer, who must then click OK to proceed
back() Takes the window back one item in its history list
blur() Removes the focus from a window
clearInterval() Cancels the action of a setInterval() method call
clearTimeout() Cancels the action of a setTimeout() method call
close() Closes a browser window
Displays a confirmation dialog box to the viewer, who must then click
confirm()
OK or Cancel to proceed
escape() Converts special characters in a string to hexadecimal characters
Enables the viewer to launch the Find utility in the browser to find text
find()
on a page
focus() Gives the focus to a window
forward() Takes the window one item forward in its history list
Sends the viewer to the home page the viewer has set in the Web
home()
browser settings
moveBy() Moves a window by certain pixel values that are sent as parameters
Moves the top-left corner of the window to the coordinates sent as
moveTo()
parameters
open() Opens a new browser window
print() Prints the contents of the window
prompt() Pops up a prompt dialog box asking the viewer to input information
Resizes a window by moving the bottom-right corner by certain pixel
resizeBy()
values that are sent as parameters
Resizes an entire window to the height and width that are sent as
resizeTo()
parameters
Scrolls the viewing area of a window by certain pixel values that are
scrollBy()
sent as parameters
Scrolls the viewing area of the window to the specified coordinates that
scrollTo()
are sent as parameters
setInterval() Calls a function each time a certain amount of time passes
setTimeout() Calls a function once after a certain amount of time has passed
stop() Stops the window from loading its content
unescape() Converts an escaped string back to its normal characters
ٝ ثٝـؼ وٚؼ ٔی آٛخٚ ٝ ایؽ) ایٗ ٔشؽ یه دبح آح ثَٜ لجُ ؼیؽّٛ اق ایٗ ٔشؽ ـا ؼـ فٜ اوشفبؼٝ٘فیم
)ٓیٌٙكؼ ٔی وٌٛ وبـثفٝ آٖ دیبٔی ـا ثّٝویٚ
5 4(/ َٔثب
window.alert("Hi there!");
ٍؽاـٞ ٖاِی اق وبـة یب ؼاؼٛ ثف دفویؽٖ وٜٚ ؼـ آٖ ػالٝؽ وٙ ثبق ٔی وpop up ٜدفٙایٗ ٔشؽ ٘یك یه د
)ؼ ؼاـؼٛخٚ cancel ٚ ok ٝٙ ٌكیٚ ؼٜ یب غیفٚ
5 4(0 َٔثب
5 4(1 َٔثب
<body>
<a href="http://www.dlp.lxb.ir" id="myweb">My Weblog</a>
<script type="text/javascript">
var s_link = document.getElementById("myweb");
s_link.onclick = function() {
var is_sure = window.confirm("Are you sure you want to
leave?");
if (!is_sure) {
window.alert("OK. You can stay here.");
return false;
}
};
</script>
</body>
آیب اقٝىٙی ثف ایٙؼ ٔجٌٛ ٔیٜ وبـثف ٕ٘بیً ؼاؼٝه دیغبٔی ثٙی ِیٚ ٔطٓ وّیه وفؼٖ ـٝؼـ ٔثبَ ثبال ث
بٖ ؼاـیؽ ؟ٙ إ٘یٝسفن ِفط
ٝیؽ ؼـ ِفطٙ وّی وcancel یِٚی اٌف ـٚ یؽٌٛ غبـج ٔیٝ اق ِفطok یٚـر وّیه وفؼٖ ـِٛ ؼـ
)ثبلی ٔی ٔب٘یؽ
ای غبَ ؼـ ِفطبرّٕٝی ِغز یب خٛ خٚ وبـثف اخبق خىزٝا٘یؽ ثب ثىبـ ثفؼٖ ایٗ ٔشؽ ثٌٕٛب ٔی س
)یؽٞؼ ـا ثؽٛغ
5َٔثب
ثفای وبـثفٛ خٚ خىزٜدفٙ یه دClick to Find Text ٕٝی ؼوٚؼـ ٔثبَ قیف ثب وّیه وفؼٖ ثف ـ
)بی ٔػشّف آٖ ـا ا٘شػبة ٕ٘بثؽٞ ٍٗ آدٚ ؼ ـا ؼـ آٖ سبیخ ٕ٘بیؽٛـؼ ٘ظف غٛٔ ا٘ؽ ِغزٛ ٔی سٝؼ وٌٛ ثبق ٔی
5 4(2 َٔثب
<form>
Web _ـٌفٚة ٔفٚ ظیٕبرٙ ؼـ سٝ اِّی وٝ ِفطٝؼ سب وبـثف ـا ثٌٛ ٔیٜایٗ ٔشؽ ثفای ایٗ اوشفبؼ
ؼـٚ ؽٙ فبیففبوه وبـ ٔی وٚ بی ادفاٞـٌفٚ ثفٌفؼا٘ؽ) ایٗ ٔشؽ ؼـ ٔفٜؼ سبیخ وفؼٛ_ غbrowser settings
)ٖٝ ٘شیدٚــ ثؽّٛشف٘ز اوىذٙـٌف ایٚٔف
5 4(3 َٔثب
<form>
<input type="button" value="Go Home!"
onclick="window.home();">
</form>
) ؽٙز اق یه وٙ اق ٘بْ آٖ ثف ٔی آیؽ وبـثف ـا لبؼـ ٔی وبقؼ ثفای ٌففشٗ دفیٝـ وٕٛٙ٘بٞ
5 4(4 َٔثب
<form>
<input type="button" value="Click to Print Page"
onclick="window.print();">
</form>
اـؼ وفؼٖ ا٘الػبر ؼـ آٖ ـا ٔیٚ ا٘بییٛ وبـثف سٝ ثٝؽ وٙ دبح آح ایدبؼ ٔی وٜدفٙایٗ ٔشؽ ٘یك یه د
)ؽٞؼ
)یؽٞؼ اغشّبَ ؼٛ دبح آح غٜدفٙ دٝ خبی آٖ یه ٘بْ ثٝ ثvarname
)یىیؽٛ٘ دب ٔیٙ ٕ٘بیؽ ـا ؼـ ایٜؽٞىشیؽ سب وبـثف ٍٔبٞ ُ ٔبیٝ دیبٔی وYour Text
ٖ وبـثف ٍ٘بٝـر دیً ففْ ثِٛ ٝ ثٚ ؼیٚـٚ ُ ٌٕب ـا لبؼـ ٔی ٌبقؼ سب دیبٔی ـا ؼـ ٔؽغDefault Entry
)یؽٞؼ
5 4(,+ َٔثب
<script type="text/javascript">
var thename = window.prompt("What's your name?","");
if (thename.length < 1) {
thename = "Anonymous Visitor";
}
document.write("Hello "+thename+"!");
</script>
شفٙ قؼٖ وّیؽ ایٚ ْاـؼ وفؼٖ ٘بٚ ـرِٛ اـؼ ٕ٘بییؽ ؼـٚ ؼ ٘بٔی ـاٌٛ اوز ٔیٛؼـ ٔثبَ ثبال اثشؽا اق ٌٕب ؼـغ
ْیؽ دیبٞشف ـا فٍبـ ؼٙ ایٚ ٕبییؽٙ٘ ِی اٌف زیكی ـا ؼـ وبؼـ سبیخٚ ؼٌٛ ٔیٜ ٕ٘بیً ؼاؼٝ٘بْ ٌٕب ؼـ ِفط
)ؼٌٛ زبح ٔیٝ ؼـ ِفطAnonymous Visitor
5 4(,, َٔثب
<body>
<div id="greeting">
<h1>Hello! Welcome!</h1>
</div>
<div id="content">
This page talks about what I think about...
</div>
<script type="text/javascript">
var greet = document.getElementById("greeting");
var thename = window.prompt("What's your name?","");
if (thename.length < 1) {
thename = "Anonymous Visitor";
}
greet.innerHTML = "<h1>Hello " + thename + "! Welcome!</h1>";
vf
</script>
128 دَلران پی سی
Hadi Ahmadi Learning Java Script
></body
;)"window.open("URL","name","attribute1=value,attribute2=value
URL
ث ٝخبی ایٗ دبـأشف آؼـن ِفط ٝیب وبیز ٔٛـؼ ٘ظف لفاـ ٔی ٌیفؼ)
name
ث ٝخبی ایٗ دبـأشف ٘بٔی ـا ؤ ٝی غٛاٞیؽ ثِ ٝفط ٝخؽیؽ ا٘الق ٌٛؼ ـا ٔی ٘ٛیىیؽ)
attribute1=value,attribute2=value
ث ٝخبی ایٗ دبـأشفٞب ٔمبؼیف ػؽؼی ٚیب ٌكیٞ ٝٙبی “yes”, “no”,لفاـ ٔی ٌیفؼ) اٌف اق ایٗ دبـأشفٞب
اوشفبؼٕٙ٘ ٜبییؽ اق ٌكیٞ ٝٙبی دیً ففْ دٙدف ٜخبـی اوشفبؼٔ ٜی ٌٛؼ)
ٔثبَ 5 4(,-
;)"window.open("http://www.dlp.lxb.ir","my_blog
ٔثبَ 5 4(,.
window.open("http://www.dlp.lxb.ir","my_blog","width=400,heigh
;)"t=300
Standard Attributes
بٌٙاَ ایٗ ٔشؽ آٛ ٕ٘بییؽ ثبیؽ ثب ثفغی اق غٜ اوشفبؼopen() بی ٔشؽٞ یمٌیٚ اقٝیؽ وٞاٛاٌف ٔی غ
ٕٝیز ؼاـؼ وٞز اٟاَ اوشب٘ؽاـؼ اق آٖ خٛ غٝب ثٟ٘ؽ ٘بٍٔؿاـی آٙیٌٛ اَ اوشب٘ؽاـؼ ٔیٛب غٟ٘ آٝ ثٝیؽ وٌٛ
)یٓ وفؼٞاٛب ِطجز غٟ٘ـؼ آٛٔ ؼـ ثػً ثؼؽ ؼـٝؼ ؼاـ٘ؽ وٛخٚ ٜدفٙ دٖٝ ثٛاَ ٔفثٛ ای ؼیٍف اق غٝؼوش
)یؽَٙ قیف ٔی ثیٚاَ اوشب٘ؽاـؼ ـا ؼـ خؽِٛیىشی اق غ
5 4(,/ َٔثب
window.open("http://dlp.lxb.ir","dehloran
pc","width=300,height=200,menubar=yes");
5 4(,0 َٔثب
window.open("http://dlp.lxb.ir","dehloran pc
","width=300,height=200,directories=yes,
location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=y
es,
toolbar=yes");
Extended Attributes
ب ٔیٟ٘ ٔؼففی آٝ ؼـ قیف ثٝؼ ؼاـؼ وٛخٚ خؽیؽٜدفٙ ٔشؽ ایدبؼ دٖٝ ثٛاَ ٔفثٛ ای ؼیٍف اق غٝؼوش
ٕٝٞ ؼـٝب٘ؽ ٕٔىٗ اوز وٞ ایدبؼ ٌؽnew window ٔشؽٝوؼٛ ثفای سٝب وٞ یمٌیٚ ٗدفؼاقیٓ) ای
) ا٘ؽٜاَ اوشب٘ؽاـؼ لفاـ ٘ؽاؼٛب ـا خكء غٟ٘ إٓٝیٗ ؼِیُ اوز وٞ ٝؽ ثٙٙثی ػُٕ ٘ىٛ غٝب ثٞـٌفٚٔف
5 4(,1 َٔثب
window.open("http://dlp.lxb.ir","dehloran pc"
,"width=400,height=300,status=yes,screenX=0,left=0,screenY=0,t
op=0");
ٜدفٙ وٕز زخ دٚ ثبالٌٌٝٛ دیىىُ' ؼـ.++ دیىىُ ' اـسفبع/++ ْ ثب ػفٜدفٙؼـ ٔثبَ ثبال یه د
)ؼٌٛ خبـی ایدبؼ ٔی
یٚؼ) ایٗ ٔشؽ ٕٔىٗ اوز ـٚ وبـ ٔی ـٝ ایؽ ثٜااوىفیذز ثبق وفؼٚ ثب خبٝ ای وٜدفٙایٗ ٔشؽ ثفای ثىشٗ د
)ؽٙب وبـ ٘ىٞـٌفٚثؼٕی اق ٔف
<body>
I am a new window! I am newer than that old window
that opened me, so I am special. Ha, ha!
<form>
<input type="button" value="Close Window"
onclick="window.close();" />
</form>
</body>
ُٕ ایٗ ػٚ)ؼٚ ٕ٘بیٍٍف ثىبـ ـٝ یه ٔىبٖ اق ِفطٝ خؽیؽ ثٜدفٙ خبیی یه دٝا٘ؽ ثفای خب ثٛایٗ ٔشؽ ٔی س
ٝیٓ) ٌىُ وّی آٖ ثٞ آٖ ٔی ؼٝ ثف ضىت دیىىُ ثٝؽ وٞبی ػؽؼی ا٘دبْ ٔی ؼٞ اق دبـأشفٜـا ثب اوشفبؼ
5ـر قیف اوزِٛ
window.moveBy(x-pixels,y-pixels);
5 4(,2 َٔثب
<body>
I am a new window! I am newer than that old window
that opened me, so I am special. Ha, ha!
<form>
<input type="button" value="Move Window"
onclick="window.moveBy(50,50);" />
<br /><br />
<input type="button" value="Close Window"
onclick="window.close();" />
</form>
</body>
ٔثبَ ثبال ٌٕب ؼ ٚؼؤٕ ٝی ثبٌؽ یىی اق آٟ٘ب ثب ٘بْ Move Windowوٚ ٝظیف ٝخبث ٝخبیی ث ٝا٘ؽاقٜ
0+دیىىُ ؼـ ـاوشبی ٔطٛـٞبی X_Yـا ثفػٟؽ ٜؼاـؼ ٚؼیٍفی ثب ٘بْ ٚ Close Windowظیف ٝثىشٗ
دٙدف )ٜایٗ ٔشؽ ٕٔىٗ اوز ـٚی ثؼٕی اق ٔفٚـٌفٞب وبـ ٘ىٙؽ)
ایٗ ٔشؽ ٔی سٛا٘ؽ ثفای ا٘شمبَ یه دٙدف ٜخؽیؽ ث ٝیه ٔىبٖ اق ِفطٕ٘ ٝبیٍٍف ثىبـ ـٚؼ) ٚایٗ ػُٕ ـا ثب
اوشفبؼ ٜاق دبـأشفٞبی ػؽؼی ا٘دبْ ٔی ؼٞؽ و ٝثف ضىت دیىىُ ث ٝآٖ ٔی ؼٞیٓ) ففق ایٗ ٔشؽ ثب ٔشؽ ثبال
ؼـ ایٗ اوز و ٝایٗ ٔشؽ سٟٙب یه ثبـ اخفا ٔی ٌٛؼ ٌىُ وّی آٖ ثِٛ ٝـر قیف اوز5
;)window.moveTo(x-value,y-value
ٔثبَ 5 4(,3
><body
I am a new window! I am newer than that old window
!that opened me, so I am special. Ha, ha
><form
"<input type="button" value="Move Window
>onclick="window.moveTo(50,50);" /
><br /><br /
"<input type="button" value="Close Window
>onclick="window.close();" /
></form
></body
window.resizeBy(x-value,y-value);
5 4(,4 َٔثب
<body>
I am a new window! I am newer than that old window
that opened me, so I am special. Ha, ha!
<form>
<input type="button" value="Resize Window"
onclick="window.resizeBy(50,50);" />
<br /><br />
<input type="button" value="Close Window"
onclick="window.close();" />
</form>
</body>
ٖ ػفْ آٚ َٛ٘ ٝ دیىىُ ث0+ ٜ ا٘ؽاقٝ ثResize Window ٕٝی ؼوٚفثبـ وّیه ثف ـٞ ؼـ ٔثبَ ثبال ثب
)ؼٌٛ ٔیٜؼٚافك
ؽ ٔشؽٕٙ٘بٞ ٖ ؼـیبفز دبـأشف آٚ ػّٕىفؼٜٛ ٘طٚ ؼٚ ثىبـ ٔی ـٜدفٙ یه دٜایٗ ٔشؽ ثفای سغییف ا٘ؽاق
)ـر قیف ٔی ثبٌؽّٛ ٔی ثبٌؽ)ٌىُ وّی آٖ ثmoveTo()
window.resizeTo(x-value,y-value);
ٝ ثف ضىت ٔیّی ثب٘یٝ دبـأشف قٔب٘ی وٝ ثٝخٛ ثب سٝؽ وٞ ـا ا٘دبْ ٔی ؼٝایٗ ٔشؽ ؼـ ضمیمز وبـ یه ضّم
ُ ٕ٘بییٓ)ٌىٜ اوشفبؼclearInterval() اق ٔشؽٝؼ سب قٔب٘ی وٌٛ ـر ٔىفـ اخفا ٔیِٛ ٝیٓ ثٞ آٖ ٔیؽٝث
5ـر قیف اوزِٛ ٝوّی ایٗ ٔشؽ ث
window.setInterval("function()",time);
5 4(-+ َٔثب
<body>
<script type="text/javascript">
function annoy_alert() {
window.alert("Am I bothering you yet?");
}
window.setInterval("annoy_alert()",10000);
</script>
</body>
) ٕ٘بیً ؼـ ٔی آیؽٍٝؽاـی ثفای وبـثف ثٞ ْ یه ثبـ دیبٝ ثب٘یٜف ؼٞ ؼـ ٔثبَ ثبال
ـر قیف ٔیِٛ ٝؼ)ٌىُ وّی آٖ ثٌٛ ٔیٜ اوشفبؼsetInterval() اق ایٗ ٔشؽ ثفای اق وبـ ا٘ؽاغشٗ ٔشؽ
5ثبٌؽ
window.clearInterval(name);
) اوزٜ ٌؽٜ ٘ىجز ؼاؼsetInterval() ٝ ثٝیؽ وٙاـؼ وٚ ٘بْ ٔشؽ یب ٔشغییف ـاname خبیٌٕٝب ثبیؽ ث
5 4(-, َٔثب
<body>
<script type="text/javascript">
function annoy_alert() {
window.alert("Am I bothering you yet?");
}
var madness = window.setInterval("annoy_alert()",10000);
</script>
Click the button below to end the endless barrage of
alerts.<br />
<form>
<input type="button" value="Stop the Madness!"
onclick="window.clearInterval(madness);" />
</form>
</body>
)ؼٌٛ لف ٔیٛ ٔشsetInterval() ٔشؽStop the Madness ٕٝ ٔطٓ قؼٖ ؼوٝؼـ ٔثبَ ثبال ث
5 4(-- َٔثب
<body>
<script type="text/javascript">
function annoy_alert() {
window.alert("Sign my guest book NOW!");
}
var theguest = window.setTimeout("annoy_alert()",10000);
</script>
</body>
)ؼٌٛ ٔیٜ اوشفبؼsetTimeout() اق ایٗ ٔشؽ ثفای اق وبـ ا٘ؽاغشٗ ٔشؽ
5 4(-. َٔثب
<body>
<script type="text/javascript">
function annoy_alert() {
window.alert("Sign my guest book NOW!");
}
var theguest = window.setTimeout("annoy_alert()",10000);
</script>
Click the button below within 10 seconds to avoid an alert
message.<br />
<form>
<input type="button" value="No Alert for Me!"
onclick="window.clearTimeout(theguest);" />
</form>
</body>
آرایِ
یه ـا ٜؾغیف ٜوبقی ػٙبِف اق یه ٘ٛع ثفای ؼوشفوی وفیغ ٚآوبٖ ؼـ ٔٛالغ ٔػشّف آـای ٝاوز) ؼـ
خبٚا اوىفیذز ـٚي ٞبی ٔػشّفی ثفای ایدبؼ آـایٞ ٝب ٕٞ ٚسٙیٗ ؼوشفوی ث ٝآٟ٘ب ٚخٛؼ ؼاـؼ) وبؼ ٜسفیٗ
٘ٛع یه آـای ' ٝآـای ٝی یه ثؼؽی ٔی ثبٌؽ و ٝؼوشفوی ث ٝػٙبِف آٖ ث ٝـاضشی ٚثب اوشفبؼ ٜاق ایٙؽوه آٟ٘ب
ِٛـر ٔی ٌیفؼ)
ًاهگذاری یک آرایِ
٘بٍٔؿاـی آـایٞ ٝب ٕٞب٘ٙؽ ٘بٍٔؿاـی ٔشغیفٞب ' اٌیب ٚسٛاثغ ٔی ثبٌؽ ٚاق لٛا٘یٗ ٘بٍٔؿاـی ٌفشٌ ٝؽ ٜؼـ
ٔٛـؼ آٟ٘ب ٘یك دیفٚی ٔی وٙؽ)ٔب٘ٙؽ اخشٙبة اق آٚـؼٖ اػؽاؼ ؼـ اثشؽای ٘بْ آـای ٝیب اوشفبؼ ٜاق فٕبی غبِی ٚ
وّٕبر ـقـٌ ٚؽ)ٜ
تؼزیف یک آرایِ
سؼفیف یه آـای ٝث ٝوبؼٌی سؼفیف ٞف ٔشغیف ؼیٍفی ِٛـر ٔی ٌیفؼ) ٔب٘ٙؽ 5
ٔثبَ 5,+(,
ؼـ ٔثبَ ثبال یه آـای ٝث٘ ٝبْ ٚ s_listثب زٟبـ ػّٙف سؼفیف وفؼ ٜایٓ)
ثفای ؼوشفوی ث ٝػٙبِف یه آـای ٝیه ثؼؽی ٔی سٛاٖ اق ایٙؽوه آٟ٘ب اوشفبؼ ٜوفؼ ٔب٘ٙؽ5
5,+(- َٔثب
<body>
<script type="text/javascript">
var s_list = new Array("Thomas","Roger","Amber","Jennifer");
var tall_student = s_list[0];
document.write("The tallest student in class is
"+tall_student);
</script>
</body>
وذه آٖ ـا ؼـٚ ٓیٙ ؼوشفوی دیؽا ٔی وَٝ آـایٚ اٝ٘ غبٝ ثs_list[0] اق وؽٜ ثب اوشفبؼٚ ؼـ ٔثبَ ثبال
)ٓیٞخی ٕ٘بیً ٔی ؼٚبیز آٖ ـا ؼـ غفٟ٘ ؼـٚ ٓ ٔی ٕ٘بییٜ ؾغیفtall_student
5,+(. َٔثب
<body>
<script type="text/javascript">
var s_list = new Array("Thomas","Roger","Amber","Jennifer");
document.write("The tallest student in class is "+s_list[0]);
</script>
</body>
ًخی ٕ٘بیٚ ؼـ یه ٔشغییف غبـخی آٖ ـا ٔىشمیٕب ؼـ غفs_list[0] ٖ وفؼٜٖ ؾغیفٚؼـ ٔثبَ ثبال ثؽ
یٓ وذه ؼـٞ آٖ اغشّبَ ؼٝـؼ ٘یبق ـا ثٛٔ اثشؽا فٕبیٝ ایٗ اوز وٝبی سؼفیف آـایٞ ٜیىی ؼیٍف اق ـا
)ٖٓ ٕ٘بییٛ آٖ ٔفثٝلغ ٘یبق ٔمبؼیف ـا ثٛٔ
5,+(/ َٔثب
ؼـ ٔثبَ ثبال یه آـای ٝث٘ ٝبْ ٚ s_listزٟبـ غب٘ ٝثفای آٖ ایدبؼ ٕ٘ٛؼ ٜایٓ)
ضبَ ٔی سٛا٘یٓ ثٞ ٝفیه اق غب٘ٞ ٝبی آٖ ٔمؽاـی ـا ٔٙشّت ٕ٘بییٓ ٚضشٕب القْ ٘یىز و ٝایٗ اسّبة
ٞب ث ٝسفسیت ثبٌٙؽ)
;"s_list[2] = "Amber
;"s_list[4] = "Pat
ـا ٜو ْٛثفای سؼفیف آـای ٝایٙىز و ٝاثشؽا فٕبی ٔٙبوت ـا ث ٝآٖ اغشّبَ ؼٞیٓ ٚثالفبِّٔ ٝمبؼیف ـا
ث ٝآٖ اسّبَ ؼٞیٓ)
ٔثبَ 5,+(0
ایدبؼ یه آـای ٝفمٗ ثب اوشفبؼ ٜاق اغشّبَ یه ٘بْ ٚثؽ ٖٚؼـ ٘ظف ٌففشٗ فٕب ثفای ؾغیف ٜوبقی ػٙبِف
آٖ ٘یك یىی اق ـاٞ ٜبی سؼفیف آـایٔ ٝی ثبٌؽ)
ٔثبَ 5,+(1
ؼـ ایٗ ـٚي ٔی سٛا٘یٓ ثؼؽا ٚؼـ ٌفایٗ ٔػشّف ٔمبؼیف ٔشفبٚسی ـا ث ٝآٖ اغشّبَ ؼٞیٓ)
5,+(2 َٔثب
5,+(3 َٔثب
Property Description
constructor Refers to the constructor function used to create an instance of an
object
index Used when an array is created by a regular expression match
input Used when an array is created by a regular expression match
length Contains a numeric value equal to the number of elements in an
array
prototype Allows the addition of properties and methods to objects such as
the JavaScript
Array object
The constructor
) ـا ٔی وبق٘ؽٝ یه آـایٝبیی اوز وٞایٗ غبِیز ٌبُٔ سٕبْ وؽ
وّیؽیّٕٝ ثؼؽ اق آٖ وٚ ٝٙ وذه ٘مٚ ٝ ایٗ غبِیز ٌٕب ثبیؽ اق ٘بْ آـایٝثفای ؼوشفوی ث
) ٕ٘بییؽٜ اوشفبؼconstructor
5,+(4 َٔثب
The length
)اٖ ٔمؽاـ ثف ٔی ٌفؼا٘ؽٛٙ ػٝ ـا ثٝایٗ غبِیز سؼؽاؼ اخكای آـای
5,+(,+ َٔثب
The prototype
ؽ سبٞ ٔی ؼٜ ٌٕب اخبقٝ ٔی ثبٌؽ)ایٗ غبِیز ثprototype ' غبِیزٝبی آـایٞ یىی ؼیٍف اق غبِیز
) ٕ٘بییؽٝب أبفٞ ٝ آـایّٕٝ اق خٝؼ ؼـ ِفطٛخٛٔ اخكایٝبی ٔػشّفی ـا ثٞ ٔشؽٚ یمٌیٚ
Array.prototype.new_property=default_value;
5,+(,, َٔثب
Array.prototype.attitude = "cool";
var s_list = new Array();
window.alert("This place is "+s_list.attitude);
5,+(,- َٔثب
Array.prototype.attitude = "cool";
var s_list = new Array();
window.alert("This place is "+s_list.attitude);
5,+(,. َٔثب
ِهتذّای آرای
ٚ بٞ ٝبی آـایٞ سب ٔشؽٜلز آٖ ـویؽٚ ب ٌؽیؽٌٙ آٝبی آـایٞ غبِیزٚ بٞ یمٌیٚ ؼی ثبٚ سب ضؽٝضبال و
)ٓیٞـؼ ثفـوی لفاـ ؼٛٔ آٖ ـاٜاـؼ اوشفبؼٛٔ
Method Description
concat() Combines the elements of two or more arrays into one new array
join() Combines the elements of an array into a single string with a separator
character
pop() Removes the last element from an array and then returns the removed
element if needed
push() Adds elements to the end of an array and then returns the numeric value of
the new length of the array if needed
reverse() Reverses the direction of the elements in an array: the first ele
shift() Removes the first element from an array and then returns that element if
needed
unshift() Adds elements to the beginning of an array and returns the numeric value of
the length of the new array if needed
slice() Pulls out a specified section of an array and returns the section as a new array
splice() Removes elements from an array or replaces elements in an array
sort() Sorts the elements of an array into alphabetical order based on the string
values of the elements
toString() Combines the elements of an array into a single string with a comma as a
separator character
5,+(,/ َٔثب
)ؼٌٛ ٌٝشٛ٘ ٘ؽ ثبیؽ ٘بْ آٖ ؼـ اثشؽاٌٛ ٜـؼٚ ؼـ اثشؽا آٝیؽ اخكای یه آـایٞاٛ ثػٝىٙثفای ای
5,+(,0 َٔثب
5,+(,1 َٔثب
5,+(,2 َٔثب
)ؼٌٛ ٔیfish, chicken, corn, peas, oranges, apples ٝؼـ ٔثبَ ثبال ٘شید
5,+(,3 َٔثب
<body>
<script type="text/javascript">
var fruits = new Array("oranges","apples","pears");
var fruit_string = fruits.join();
document.write("The new string is "+fruit_string);
</script>
</body>
)ؼٌٛ سفویت ٔیThe new string is ٝ ثب ـٌشfruits یٝؼـ ٔثبَ ثبال آـای
5,+(,4 َٔثب
<body>
<script type="text/javascript">
var fruits = new Array("oranges","apples","pears");
var fruit_string = fruits.join(":");
document.write("The new string is "+fruit_string);
</script>
</body>
)ؽٙ" سغییف دیؽا ٔی و:" ٝ ثٝبی اخكای آـایٞ ٜؽٙٙؼـ ٔثبَ ثبال خؽاو
5,+(-+ َٔثب
5,+(-, َٔثب
)ؼٚ ثىبـ ٔی ـٝبی یه آـایٟ ا٘شٝ وفؼٖ یه خكء ثٝایٗ ٔشؽ ثفای أبف
5,+(-- َٔثب
5,+(-. َٔثب
5,+(-/ َٔثب
5,+(-0 َٔثب
)ؽٙ ـا ضؿف ٔی وّٝف اثشؽایی آـایٙ ػٝر وٚؽ ثب ایٗ سفبٙ ػُٕ ٔی وpop ؽ ٔشؽٕٙ٘بٞ ایٗ ٔشؽ
5,+(-1 َٔثب
5,+(-2 َٔثب
ٝ٘شید
)arrayname.slice(start,stop
ٔثبَ 5,+(-3
٘شیدٝ
apples , pears
ٔثبَ 5,+(-4
ؼـ ٔثبَ ثبال spliceوفؼٖ اق اق ػّٙف وٌ "pears" ْٛفٚع ٌؽ ٚ ٜؼـ ٕٞب٘دب ٓٞغبسٕٔ ٝی یبثؽ ٚؼـ
٘شید ٝثبػث ضؿف آٖ ٔی ٌٛؼ)
ٔثبَ 5,+(.+
"pears"ّْٛف وٙ ثف ػٜٚ ػالٝر وٚؼ ثب ایٗ سفبٌٛ ع ٔیْٚ ػّٕیبر ٌفّٛف وٙؽ ٔثُ ثبال اق ػٕٙ٘بٞ
)ؼٌٛ " ٘یك ضؿف ٔیgrapes" یٙبـْ یؼّٟف زٙػ
5,+(., َٔثب
ٝ ثwatermelons ّفٙ ثؼؽ ػّٝؼ)وذه ؼـ ٔفضٌٛ ضؿف ٔیٝ اق آـایpears ّفَٙ ػٚ اّٝؼـ ٔفض
)ٕبٖ ٔىبٖ خبیٍكیٗ ٔی ٌفؼؼٞ ؼـٚ ٖخبی آ
5,+(.- َٔثب
ٔیٝ آٖ أبفٝ ٘یك ثplums ٚ watermelons بِفٙ ػٝؼ ثّىٌٛ ضؿف ٕ٘یّٝفی اق آـایٙیر ػٞ
)ٌفؼ٘ؽ
5,+(.. َٔثب
5,+(./ َٔثب
هتذّای تیطتز
فٞ ٔی ٕ٘بییؽ) ؼـٜؽَٞ قیف ٍٔبٚ ؼـ خؽٝؼ ؼاـؼ وٛخٚ ثبالسف یه وفی ٔشؽٚ ,)1 ااوىفیذزٚؼـ خب
ؽ ؼـٌٙ ثبٝ یب ٘شبیح یىىب٘ی ٘ؽاٌشٚ ؽٙٙ ؼـوشی وبـ ٘ىٝب ثٞـٌفٚب ٕٔىٗ اوز ؼـ سٕبْ ٔفٞـر ایٗ ٔشؽِٛ
)یؽٙب ثب اضشیبٖ ـفشبـ وٟ٘ اق آٜ ؼـ اوشفبؼٝ٘شید
Method Description
filter() Returns a new array containing elements from an array that returned true
based on the function used to filter it
forEach() Calls a specified function for each element in the array
every() Returns true if all elements in the array return true for the specified
function used to test them
indexOf() Returns the lowest index number for an element that has a value equal to
the specified value sent as a parameter
lastIndexOf() Returns the highest index number for an element that has a value equal to
the specified value sent as a parameter
map() Returns a new array that results from calling a specified function on every
element in the array
reduce() Runs a function on two values in the array at a time, from left to right,
until only a single value is left
reduceRight() Runs a function on two values in the array at a time, from right to left,
until only a single value is left
some() Returns true if one or more elements return true for the specified function
used to test them
ـٛ٘ ٝبیی ـا ثٞ ٜیؽ ؼاؼٞاٛ ٔثال ٌٕب ثػٝؼ وٌٛ قٔب٘ی ٔفیؽ ٔیٝب ثفای وبغز آـایٞ ٝ اق ضّمٜاوشفبؼ
)یؽٙاَ وٛدیبدی اق وبـثف و
5,+(.0 َٔثب
بـ ثب سىفاـ ٔی ٌفؼؼٟ ایٗ ػُٕ زٚ ؽٞاـؼ وفؼٖ یه اوٓ ـا ٔی ؼٚ اوزٛ اق وبـثف ؼـغٝٔؼـ ایٗ ٔثبَ ثف٘ب
)٘ؽٌٛ ٔیٜ ؾغیفs_list ٝ ؼـ آـایٜاـؼ ٌؽٚ بیز اوبٔیٟ٘ ؼـٚ
5,+(.1 َٔثب
)ی ثیىزٚزىشفٔىبٛ وٚ ج ثكـي سف اق ِففٚ ٕ٘بیً اػؽاؼ قٚ بوبییٌٙ ثفایٝٔثف٘ب
5,+(.2 َٔثب
<body>
<h1>Student Names</h1>
<script type="text/javascript" >
152 دَلران پی سی
Hadi Ahmadi Learning Java Script
)ٓ ٕ٘بییٜ اوشفبؼfor ٝی ضّمٙ وؽ قیف ثفای خبیٍكیٝؼٙا٘یٓ اق لٛ ثبال ٔی سٝٔؼـ ثف٘ب
for(var count=0;count<s_list.length;count++)
5,+(.3 َٔثب
5,+(.4 َٔثب
s_list[3]="Jennifer";
for (student in s_list) {
document.write(s_list[student] + "<br />");
}
تؼزیف
ٝیٓ) ایٗ آـایٙی ـا ٔؼففی ٔی وٕٙبی ٌفوز دؿیف یب ا٘دٞ ٝ' آـایٝـؼ آـایٛٔ اٖ آغفیٗ ٔجطث ؼـٛٙ ػٝث
) ٔمؽاـ ٔدكایی ـا اغشّبَ ؼاؼٚ ْف یه اق اخكای آٖ ٘بٞ ٝاٖ ثٛ ٔی سٝؽ وٌٙب ؼاـای ایٗ غبِیز ٔی ثبٞ
5,+(/+ َٔثب
5,+(/, َٔثب
<body>
<h1>Student Names</h1>
<script type="text/javascript">
var s_list= new Array();
s_list["tall"]="Thomas";
s_list["cool"]="Roger";
s_list["clever"]="Amber";
s_list["attentive"]="Jennifer";
document.write("The tall one is " +s_list["tall"]+ "<br />");
document.write("The cool one is " +s_list["cool"]+ "<br />");
document.write("The clever one is " +s_list["clever"]+ "<br
/>");
document.write("The attentive one is " +s_list["attentive"]+
"<br />");
</script>
</body>
اضیا ریاضی
ؽ ٌٕب ـا ؼـٙ٘اٛ ٔی سٝ اوز وٜ ٌؽٜ ای سؽاـن ؼیؽٝاثغ اق دیً وبغشٛ سٚ ااوىفیذز اٌیبٚؼـ خب
)ؽٙؼ یبـی ٕ٘بیٛٔطبوجبر غ
Properties
ب ـا ؼـ قیفٟ٘ ِیىشی اق آٝؽ وٙٞبی قیبؼی ـا ؼـ اغشیبـ ٌٕب لفاـ ٔی ؼٞ غبِیزٚ بٞ یمٌیٚ اٌیب ـیبٔی
) ٔی ٕ٘بییؽٜؽٍٞٔب
Property Value
E Value of Euler’s constant (E), which is about 2.71828…
LN10 Value of the natural logarithm of 10, which is about 2.302585…
LN2 Value of the natural logarithm of 2, which is about 0.693147…
LOG10E Value of the base 10 logarithm of E, which is about 0.43429…
LOG2E Value of the base 2 logarithm of E, which is about 1.442695…
PI Value of pi, often used with circles, which is about 3.14159…
SQRT2 Value of the square root of 2, which is about 1.4142…
SQRT1_2 Value of the square root of one half, which is about 0.7071…
5,,(, َٔثب
<body>
<form>
To find the area of a circle, input a radius:<br />
<input type="text" id="radius" />
<input type="button" value="Get the Area!" id="getarea" />
</form>
<script type="text/javascript">
var area_button = document.getElementById("getarea");
area_button.onclick = function() {
var rad = document.getElementById("radius").value;
if (rad.length < 1) {
158 دَلران پی سی
Hadi Ahmadi Learning Java Script
ٖاـؼ وفؼٚ ـرِٛ ؼـٚ اوز یه ػؽؼ ٔی ٕ٘بیؽٛ اق ٌٕب ؼـغٝؼ وٌٛ ثبال اثشؽا وبؼـی ثبق ٔیٝٔؼـ ثف٘ب
اـؼ ٍ٘ؽٖ ػؽؼ ِطیصٚ ـرِٛ ؼـٝ ٕ٘بیً ؼـ ٔی آیؽ) لبثُ ؾوف اوز وٝآٖ ؼـ وبؼـ ؼیٍفی ٔطیٗ آٖ ث
)ؼٌٛ ٔیٜبوت ثب آٖ ٕ٘بیً ؼاؼٙدیبْ ٔش
Methods
ؼیؽٛب ٘جٟ٘ ا٘دبْ آٝ لجال لبؼـ ثٝؽ سب ٔطبوجبسی ـا وٙٙ ٌٕب وٕه ٔی وٝؼ ؼـ ٌی ـیبٔی ثٛخٛٔ بیٞٔشؽ
)یؽٞؼ ا٘دبْ ؼٛبی غٞ ـا ؼـ اوىفیذز
) ٔی ٕ٘بییؽٜؽٞب ـا ٍٔبٟ٘فیه اق آٞ ـؼٛٔ ٔیطبسی ؼـٛ سٜٕفاٞ ٝب ثَٞ قیف ِیىشی اق ایٗ ٔشؽٚؼـ خؽ
Method Purpose
abs() Returns the absolute value of the number sent as a parameter
acos() Returns the arccosine of the number sent as a parameter, in radians
asin() Returns the arcsine of the number sent as a parameter, in radians
atan() Returns the arctangent of the number sent as a parameter, in radians
Returns the arctangent of the quotient of two numbers sent as parameters, in
atan2()
radians
Returns the smallest integer greater than or equal to the number sent as a
ceil()
parameter
cos() Returns the cosine of the number sent as a parameter, in radians
Returns the value of E to the power of the number sent to the method as a
exp()
parameter
Returns the largest integer less than or equal to the number sent as a
floor()
parameter
log() Returns the natural logarithm of the number sent as a parameter
159 دَلران پی سی
Hadi Ahmadi Learning Java Script
max() Returns the larger of the two numbers that are sent as parameters
min() Returns the smaller of the two numbers that are sent as parameters
Returns the numeric value of the first parameter raised to the power of the
pow()
second parameter
random() Returns a random number between 0 and 1; does not require a parameter
Returns the value of the number sent as a parameter rounded to the nearest
round()
integer
sin() Returns the sine of the number sent as a parameter, in radians
sqrt() Returns the square root of the number sent as a parameter
tan() Returns the tangent of the number sent as a parameter, in radians
هتذّای اصلی
ٖٝ ٘یبق ثٚ ثؽٚ اـؼ وفؼٖ یه ػؽؼٚ ب ثبٟٙ سٝ وٝؽ وٙىشٞ بییٞبی اِّی ؼـ ایٗ وشبة' ٔشؽٞـ اق ٔشؽٛظٙٔ
)یؽٙ ٔی وٜؽٞب ـا ؼـ قیف ٍٔبٞؽ)ِیىشی اق ایٗ ٔشؽٙ٘ ـا ثفٔی ٌفؼاٝیه ٘شیدٛ٘ ٝٔ غبِی ثفای ثف٘بٝٔطبوج
● abs()
● acos()
● asin()
● atan()
● cos()
● exp()
● log()
● sin()
● sqrt()
● tan()
5,,(- َٔثب
<body>
<form>
Enter a (positive) number or zero: <br />
<input type="text" id="sr_num" />
<input type="button" value="Get a Square Root" id="getroot" />
</form>
<script type="text/javascript">
var root_button = document.getElementById("getroot");
root_button.onclick = function() {
var thenum = document.getElementById("sr_num").value
if (thenum < 0) {
window.alert("Hey! I said to enter a positive number! Try
again.");
return false;
}
else if (thenum != (thenum*1)) {
window.alert("Input must be numeric!");
return false;
}
else {
var theroot = Math.sqrt(thenum);
window.alert("The square root of "+thenum+" is "+theroot);
return false;
}
};
</script>
</body
هتذّای دٍ پاراهتزی
اقٝ دبـأشف ؼاـ٘ؽ' وٚ ؼـیبفز ؼٝ ثفای ِطیص اخفا ٌؽٖ ٘یبق ثٝبیی اوز وٞـؼ ٔشؽٛٔ ایٗ ٔجطث ؼـ
) وفؼٜاٖ اٌبـٛ یب سٚ زه سفٛ وٚ ای ثكـي سفٝبی ٔمبیىٞ ٔشؽٝاٖ ثٛب ٔی سٞ ایٗ ٔشؽّٕٝخ
● atan2()
● max()
● min()
● pow()
5,,(. َٔثب
<body>
<form>
<input type="button" value="Which Number is Bigger?" id="getmax"
/>
</form>
<script type="text/javascript">
var max_button = document.getElementById("getmax");
max_button.onclick = function() {
var num1 = window.prompt("Enter a number.","");
var num2 = window.prompt("Enter another number","");
var largenum = Math.max(num1,num2);
var smallnum = Math.min(num1,num2);
if (largenum == smallnum) {
window.alert("Those two numbers are equal!");
}
else {
window.alert(largenum+" is larger than "+smallnum);
}
};
</script>
</body>
ٖؽ ٌؽٙیٗ ثفای وبـثف دىٕٙسٞ ٚ ٔی ثبٌؽٚ min() ٚ max() بیٞ اق ٔشؽٜاوبن وبـ سبثغ ثبال اوشفبؼ
ب اقٟ٘ خبی آٝا٘یؽ ثٛ ٌٕب ٔی سٝ ایٓ وٜ وفؼٜز ؼـیبفز اػؽاؼ اوشفبؼٟ خprompt بیٞ اق وبؼـٝٔف ثف٘بٞظب
) ٕ٘بییؽٜی اوشفبؼٙبی ٔشٞوبؼـ
pow() تَاى
)ْ ٔی ـوب٘ؽٚاٖ ٔمؽاـ ؼٛ سَٝ ـا ثٚ وذه ٔمؽاـ اٚ اٖ دبـأشف ٔی ٌیفؼٛٙ ػٝ ٔمؽاـ ـا ثٚایٗ ٔشؽ ؼ
Math.pow(2,3); 5 ؽٙ٘ٔب
5,,(/ َٔثب
<body>
<form>
<input type="button" value="Find a Power" id="getpow" />
</form>
<script type="text/javascript">
var pow_button = document.getElementById("getpow");
pow_button.onclick = function() {
var num1 = window.prompt("Enter a base number.","");
var num2 = window.prompt("What power should we set it to (a
number)?","");
var theresult = Math.pow(num1,num2);
window.alert(num1+" to the power of "+num2+" is "+theresult);
};
</script>
</body>
دیگز هتذّا
● ceil()
● floor()
● round()
The ceil() Method
ـ ٔثبَ اٌف ٌٕب ػؽؼٛ٘ ٝؽ) ثٞ ـا ٍ٘بٖ ٔی ؼٝ٘ٛ ػؽؼ ٔفثٝایٗ ٔشؽ ثكـي سفیٗ ػؽؼ ِطیص ٘كؼیه ث
) ـا ثفٔی ٌفؼا٘ؽ,, ٝاـؼ ٕ٘بییؽ ایٗ سبثٚ ٓ_ ـاٞ ٕٔیك یه ؼٜ_ؼ,+),
5,,(0 َٔثب
<body>
<form>
<input type="button" value="Find a Ceiling" id="getceiling" />
</form>
<script type="text/javascript>
var ceil_button = document.getElementById("getceiling");
ceil_button.onclick = function() {
var num1= window.prompt("Enter a number.","");
var theceil= Math.ceil(num1);
window.alert("The ceiling of "+num1+ " is "+theceil);
};
</script>
</body>
5,,(1 َٔثب
163 دَلران پی سی
Hadi Ahmadi Learning Java Script
10.2 10
10.5 11
10.6 11
The random() Method
ـا, ٚ + ٗ یه ػؽؼ اسفبلی ثیٜاـٕٛٞ ْٝ ٔی ثبٌؽ)زفا وٚبی ٌی ـیبٔی ٔشؽ ـا٘ؽٞیىی اق ٔفیؽسفیٗ ٔشؽ
ؼ ا٘شٍبـٛة غٚ ـر اسفبلی ؼـّٛفزیك ؼثٍفی ـا ثٞ یٓ ٔشٗ ػىه یبٞاٛ ثػٝالؼی وٛٔ ثفایٚثفٔیٍفؼا٘ؽ
)ؼٚا٘ؽ ثىبـ ـٛیٓ ٔی سٞؼ
5,,(2 َٔثب
)ؼٌٛ ٔیٜاَ اسفبلی اق وبـثف دفویؽٛ یه وٝفثبـ ـففي ٌؽٖ ِفطٞ ؼـ ٔثبَ قیف ثب
<body>
<h1>My Random Quote for You:</h1>
<div id="my_quote">
Look in the mirror. Are you looking at me?
</div>
<script type="text/javascript" >
var quotes= new Array(10);
quotes[0]="Look in the mirror. Are you looking at me?";
quotes[1]="It is time for a rhyme, I guess.";
quotes[2]="Where is my JavaScript book?";
quotes[3]="If I had a buck for every dollar I spent--Oops, never
mind.";
quotes[4]="I suppose you were expecting a real quote here.";
quotes[5]="Quotes are great, but don't quote me on that.";
quotes[6]="What should I write here?";
quotes[7]="Wut hapns iff eye miss spel ohn purpas?";
quotes[8]="Mark my words, I will mark my words.";
quotes[9]="This spot reserved for a better quote.";
var q_div = document.getElementById("my_quote");
var rand_int = Math.floor(Math.random()*10);
q_div.innerHTML = quotes[rand_int];
</script>
</body>
اضیای ػذدی
ٗؽ ؼـ ایٌٙ ٔشؽ ٔفیؽ ثفای ؼوشىبـی اػؽاؼ ٔی ثبٚ یمٌیٚ ٗؽیٙااوىفیذز ٌبُٔ زٚاٌیبی ػؽؼی ؼـ خب
)ٓیٞـؼ ثفـوی لفاـ ؼٛٔ ب ـاٟ٘یٓ سؼؽاؼی اق آٙثػً وؼی ٔی و
ٍیژگی ّا
)یؽٙ ٔی وٜؽٞ اٌیب ػؽؼی ـا ٍٔبٖٝ ثٛبی ٔفثٞ یمٌیٚ َ قیف ِیىشی اقٚؼـ خؽ
Property Purpose
constructor Holds the value of the constructor function that created the object
Holds a constant number value, representing the largest value
MAX_VALUE
before JavaScript interprets a number as infinity
Holds a constant number value, representing the smallest value
MIN_VALUE
before JavaScript interprets a number as negative infinity
NaN Represents the value of “Not a Number”
NEGATIVE_INFINITY Represents the value of negative infinity
POSITIVE_INFINITY Represents the value of infinity
prototype Enables you to add properties to the object if you wish
هتذّا
) ٔی ٕ٘بییؽٜؽٞب ـا ٍٔبَٞ قیف ِیىشی اق ایٗ ٔشؽٚؼـ خؽ
Method Purpose
Returns a string value that represents the number in exponential
toExponential()
notation
Returns a string value that represents the number rounded to the
toFixed()
specified number of digits after the decimal
Returns a string value that represents the number rounded to the
toPrecision()
specified number of significant digits
toSource() Returns a string value that represents the source code of the object
toString() Returns a string value for a Number object
valueOf() Used by JavaScript internally most often
٘شیدٝ
1e+2
ٔثبَ 5,,(4
ؼـ ٔثبَ قیف سمفیت ضؿف اػؽاؼ اػٍبـی ـا -ؼـ ٘ظف ٌففش ٝایٓ ثؽیٗ ٔؼٙب و ٝسب ؼ ٚـلٓ اق اػؽاؼ اػٍبـی
ـا ضفظ ٕ٘بیؽ ٚثمی ٝـا ضؿف وٙؽ)
٘شید5ٝ
ؼـ ِٛـر اوشفبؼ٘ ٜىفؼٖ اق ٔشؽ )( toFixedؼـ ٔثبَ ثبال ٘شید ٝثِٛ ٝـر قیف ٔی ٌٛؼ5
٘شیدٝ
45.57
ٕٞب٘ٛٙـ و ٝاق ٘بْ آٖ دیؽا اوز ثفای سجؽیُ اػؽاؼ ث ٝـٌشٞ ٝبی ٔشٙی ث ٝوبـ ٔی ـٚؼ)
اضیای سهاى
ایٗ اٌیب ٘یك یىی ؼیٍف اق اٌیبی اق دیً سؼفیف ٌؽ ٜؼـ خبٚااوىفیذز ٞىشٙؽ و ٝثٌٕ ٝب سٛا٘بیی
ؼاؼٖ*ٌففشٗ یه قٔبٖ ٍٔػُ ث*ٝاق اوىفیذز غٛؼ ٔی ٕ٘بیٙؽ)
ٍیژگی ّا
ؼـ خؽ َٚقیف ایٗ ٚیمٌی ٞب ـا ٍٔبٞؽٔ ٜی ٕ٘بییؽ
Property Purpose
constructor Holds the value of the constructor function that created the object
prototype Enables you to add properties to the object if you wish
ثٌٕ ٝب ایٗ لبثّیز ـا ٔی ؼٞؽ وٚ ٝیمٌی یب ٔشؽ خؽیؽی ـا ثٌ ٝی Dateأبف ٝوٙیؽ)
5,,(,, َٔثب
Date.prototype.morning="a.m.";
var rightnow= new Date();
window.alert("This date is "+rightnow.morning);
هتذّا
سؼؽاؼ قیبؼیّٝویٚ ٝؽ أب ایٗ ٌی ثٞبی قیبؼی ـا ؼـ اغشیبـ ٌٕب لفاـ ؼٞ ا٘ؽ لبثّیزٌٛی قٔبٖ ٕ٘ی س
)ؽٙٞبی قیبؼی ـا ؼـ اغشیبـ ٌٕب لفاـ ٔی ؼٟ لبثّیشٚ بٞ یمٌیٚ بٞ ایٗ ٔشؽٝؼ وٌٛ ٔشؽ دٍشیجب٘ی ٔی
) ٔی ٕ٘بییؽٜؽَٞ قیف ٍٔبٚب ـا ؼـ خؽِٞیىشی اق ایٗ ٔشؽ
Method Purpose
getDate() Returns the day of the month based on the viewer’s local time
Returns the number of days into the week based on the viewer’s
getDay()
local time (0–6)
Returns the number of hours into the day based on the viewer’s local
getHours()
time (0–23)
Returns the number of milliseconds into the second based on the
getMilliseconds() viewer’s local
time (0-999)
Returns the number of minutes into the hour based on the viewer’s
getMinutes()
local time (0–59)
Returns the number of months into the year based on the viewer’s
getMonth()
local time (0–11)
Returns the seconds into the minute based on the viewer’s local time
getSeconds()
(0–59)
Returns the number of milliseconds since 1/1/1970 for the Date
getTime()
object
Returns the time-zone offset (from Greenwich Mean Time) in
getTimezoneOffset()
minutes based on the viewer’s local time zone
getYear() Returns the year based on the viewer’s local time (two digits)
getFullYear() Returns the full year based on the viewer’s local time (four digits)
getUTCDate() Returns the day of the month in Coordinated Universal Time
Returns the number of days into the week in Coordinated Universal
getUTCDay()
Time (0-6)
getUTCFullYear() Returns the full year in Coordinated Universal Time (four digits)
Returns the number of hours into the day in Coordinated Universal
getUTCHours()
Time (0–23)
Returns the number of milliseconds into the current second in
getUTCMilliseconds()
Coordinated Universal Time (0-999)
Returns the number of minutes into the hours in Coordinated
getUTCMinutes()
Universal Time (0–59)
● getDate()
● getDay()
● getHours()
● getMilliseconds()
● getMinutes()
● getMonth()
● getSeconds()
● getTime()
● getTimezoneOffset()
● getYear()
● getFullYear()
● getUTCDate()
● getUTCDay()
● getUTCFullYear()
● getUTCHours()
● getUTCMilliseconds()
● getUTCMinutes()
● getUTCMonth()
● getUTCSeconds()
5ؽٙ٘یؽ ٔبٙب ٌٕب ثبیؽ ؼـ اثشؽا یه ٌی قٔب٘ی ایدبؼ وٞ اق ایٗ وشؽٜثفای اوشفبؼ
ٜق ٔبٚدٕیٗ ـٙق دٚیٓ ویىشٓ ٌٕب ـا ثفٔی ٌفؼا٘ؽ) ٔثال اٌف أفٛق خبـی ثفاوبن سمٚ ـٜایٗ ٔشؽ ٌٕبـ
) ـا ثفٔی ٌفؼا٘ؽ0 ثبٌؽ ایٗ ٔشؽ ٔمؽاـ ػؽؼی
5,,(,- َٔثب
ق خبـی ـا ثفٔیٚ اوٓ ـٜ خبی ٌٕبـٝ ثٝر وٚؽ ثب ایٗ سفبٙؽ ٔشؽ ثبال ػُٕ ٔی وٕٙ٘بٞ ایٗ ٔشؽ
)friday ؽٌٙ٘فؼا٘ؽ)ٔب
5,,(,. َٔثب
range 0-59
5,,(,1 َٔثب
)ٖؼ ؼـ ٌی قٔبٛخٛٔ سب سبـیع,42+ ٝیٛ٘ لا, ب اق سبـیعٞ ٝثفٌفؼا٘ؽٖ سؼؽاؼ ٔیّی ثب٘ی
5,,(,4 َٔثب
اغشالف ؼاـیؽ ـا ثفٔی ٌفؼا٘ؽ)ٔثال اغشالف قٔب٘ی ٔب ثبGMT قٔب٘یٝمٙٙٔ ٌٕب ثبٝسؼؽاؼ ؼلبیمی و
) ـا ثف ٔی ٌفؼا٘ؽ/-+ وفؼٖ اق ایٗ سبثغٜـر اوشفبؼِٛ ؼـٝ وبػز اوز و2 GMT
5,,(-+ َٔثب
5,,(-, َٔثب
)ب اـوبَ ٕ٘بییؽٟ٘ آٝب ٌٕب ثبیؽ یه دبـأشف ػؽؼی ـا ثٞثفای ثىبـ ثفؼٖ ایٗ ٔشؽ
5,,(-- َٔثب
دیگز هتذّا
)بی ثبال لفاـ ٕ٘ی ٌیف٘ؽٞ ٝیر یه اق ؼوشٞ ؼـٝبیی اوز وٞایٗ لىٕز ٌبُٔ ٔشؽ
ٖ آٝاٖ دبـأشف ثٛٙ ػٝ ثٝ قٔب٘ی ـا وٚ_,42+ ٝیٛ٘ لا,_ بی ثیٗ قٔبٖ ٔجؽاٞ ٝایٗ ٔشؽ سؼؽاؼ ٔیّی ثب٘ی
)یٓ ـا ثفٔی ٌفؼا٘ؽٙاـؼ ٔی وٚ
5,,(-. َٔثب
<script type="text/javascript">
var rightnow= new Date();
var thenum= Date.parse("Dec 12, 1999")
)ٓ ایٜاـؼ وفؼٚ اٖ دبـأشفٛٙ ػٝ ـا ثDec 12, 1999 ٖؼـ ٔثبَ ثبال قٔب
toLocaleTimeString()
toDateString()
toTimeString()
toLocaleDateString()
toString()
toGMTString()
oLocaleString()
یٙبی ٔشٞ ٝع ـٌشٛ٘ ب اقٟ٘خی آٚ غفٝر وٚؽ ثب ایٗ سفبٙٞبی ثبال ـا ا٘دبْ ٔی ؼٞب وبـ ٔشؽٞایٗ ٔشؽ
)ب ٔی ثبٌؽٞؽ ؼیٍف ٔشؽٙ٘ب ٘یك ٔبٟ٘ ثىبـٌیفی آٝ ٘فیمٚ اوز)ؼـ
175 دَلران پی سی
Hadi Ahmadi Learning Java Script
5,,(-/ َٔثب
یٓ) القْ ثؿوف اوزٞقي ٔی ؼٛٔ ٌٕب آٝدب ثٙبٔیه ـا ؼـ ایٙاٖ ٔثبَ دبیب٘ی وبغز یه وبػز ؼیٛٙ ػٝث
) اوزٜ ٌؽٜدب٘ؽٌٙ ایٗ وشبة ٘یكٜٕفاٞ ٕٝبی ٕٔیٞ ٜلٚ ایٗ وبػز ؼـ دفٝو
<body>
<h1>Current Time:</h1>
<div id="my_clock">
<!-- call to a server-side script for backup could go here -->
</div>
<script type="text/javascript" >
function startclock() {
var thetime = new Date();
var hours = thetime.getHours();
var mins = thetime.getMinutes();
var secn = thetime.getSeconds();
var ap = (hours >= 12) ? "p.m." : "a.m.";
if (hours >= 13) {
hours -= 12;
}
if (hours < 1) {
hours = 12;
}
if (mins < 10) {
mins = "0" + mins;
}
if (secn < 10) {
secn = "0" + secn;
}
var clock_div = document.getElementById("my_clock");
clock_div.innerHTML = hours + ":" + mins + ":" + secn + " " +
ap;
setTimeout("startclock()", 1000);
}
startclock();
</script>
</body>
اقٜ اوز اوشفبؼٜیبیی آٖ ٌؽٛ ثبػث دٚ اوزٝ ؼـ وبغز ایٗ وبػز ثىبـ ـفشٝ ای وٝ٘ىش
)ظف ثیبیؽٙؼ سب وبػز ٔشطفن ثٌٛ ثبػث ٔیٝ ٔی ثبٌؽ وinnerHTML
176 دَلران پی سی
Hadi Ahmadi Learning Java Script
ثفای وبـ ثب ـٌشٞ ٝبی ٔشٙی ؼـ خبٚا اوىفیذز ٌٕب ثبیؽ ثب زٍٍ٘ٛی ثىبـثفؼٖ ٚیمٌی ٞب ٔ ٚشؽٞبی ٔفثٖٛ
ث ٝآٟ٘ب آٌٙبیی ؼاٌش ٝثبٌیؽ)ؼـ ایٗ فُّ ا َٚثب وبغشبـ ٚسؼفیف ـٌشٞ ٝب ؼـ خبٚااوىفیذز آٌٙب ٔی ٌٛیؽ
وذه ثىبـٌیفی ٔشؽٞب ٚـٌشٞ ٝب ـا غٛاٞیؽ آٔٛغز)
سٔٛیطبر 5
)"ٔ :("string value hereطشٛای ـٌشٔ ٝشٙی ٌٕب ؼـ ایٙدب ٚثیٗ ("") قزار هی گیزد.
ٔثبَ 5,-(,
ٗیؽ' ایٙ یه ٔشغییف ایدبؼ وٝ اق ٘ىجز ؼاؼٖ آٖ ثٜب ثب اوشفبؼٟٙی ـا سٙا٘یؽ یه ِیشفاَ ٔشٌٕٛب ٔی س
5 ؽ قیفٙ٘ؽ ٔبٌٙشبـی ّٔفف ٔی وٛ٘ ٌیٝیه ٔمؽاـ فٕبی وٕشفی ـا ٘ىت ثٙسى
5,-(- َٔثب
یؽ ؼاٌزٞاٛیمٌی ٔفیؽ ـا غٚ ٚ سؼؽاؼ قیبؼی ٔشؽٝ ؼوشفوی ثٜ ٕ٘بییؽ اخبقٜي لجّی اوشفبؼٚأب اٌف اق ـ
يٚ وفؼٖ اق ـٜـر اوشفبؼِٛ ؼـٝؽ' أفی وٙؼ یبـی ٕ٘بیٛشف وفؼٖ اوىفیذز غٟؽ ٌٕب ـا ؼـ ثٙ٘اٛ ٔی سٝو
)ؼٌٛ شفی ٔیٙٔ ْ سمفیجبٚؼ
) ؽ ٔی ٕ٘بییؽَٞ قیف ٍٔبٚ ؼـ خؽٝیمٌی ـا ؼاـا ٔی ثبٌؽ وٚ ٝی فمٗ وٌٙی ٔش
Property Purpose
constructor Holds the value of the constructor function for an instance of the object
length Holds the numeric value of the length of the string (its number of
characters)
prototype Allows you to add properties to the object
بیٞ وؽٜی ثفٌفؼا٘ؽٖ وبق٘ؽٙااوىفیذز) یؼٚ ؼـ اٌیب ؼیٍف خبٝؽ وٕٙبٖ وبـی ـا ٔی وٞ یمٌیٚ ٗای
)یٙٔطّی ٌی ٔش
5,-(. َٔثب
<body>
<script type="text/javascript">
var guitar_string = new String("G");
document.write(guitar_string.constructor);
</script>
</body>
document.write(guitar_string.constructor);
)ً ٕ٘بیٝ ؼـ ِفطconstructor یمٌیٚ زبح وفؼٖ ٔمؽاـ
ی) ٌٕبٙ ٔشٝؼ ؼـ ـٌشٛخٛٔ بیٞی سؼؽاؼ وبـوشفٙی ـا ثفٔی ٌفؼا٘ؽ' یؼٙ ٔشَٝ ـٌشٛ٘ یمٌیٚ ٗای
) ٕ٘بییؽٜ ِیشفاِی اوشفبؼٝٓ اق ـٌشٞ ٚ یٙٓ اق ٌی ٔشٞ ا٘یؽٛٙٔی
5,-(/ َٔثب
<body>
<script type="text/javascript">
var myname="John";
document.write("The name has "+myname.length+" characters.");
</script>
</body>
var myname="John";
یٙوبغز یه ِیشفاَ ٔش
)یؽٞی اغشّبَ ؼٙ ٌی ٔشٝبی ؼٌفی ـا ثٞ یمٌیٚ ٚ بٞیمٌی' ٔشؽٚ ٗ اق ایٜا٘یؽ ثب اوشفبؼٌٕٛب ٔی س
5,-(0 َٔثب
String.prototype.attitude="cool";
var rightnow= new String("Joe");
window.alert("This string is "+rightnow.attitude);
String.prototype.attitude="cool";
)ؽٙ ٔیىٝی أبفٙ ٌی ٔشٝ یه غبِیز خؽیؽ ـا ثprototype یمٌیٚ
هتذّای ضی هتٌی
بٞ اق ایٗ ٔشؽٜ ٔی ٕ٘بییؽ) اوشفبؼٜؽَٞ قیف ٍٔبٚب ـا ؼـ خؽٟ٘ آٝبی ثىیبـ قیبؼی ؼاـؼ وٞی ٔشؽٌٙی ٔش
)ؽ ثبٌؽٙٙا٘ؽ ثىیبـ وٕه وٛ قیجب ٔی سٚ یبٛ دٝ ثفای وبغز یه ِفطٚ بی ٔػشّفٞ لؼیزٛٔ ؼـ
Method Purpose
anchor() Creates an HTML anchor tag with a target on a page
big() Adds <big> and </big> tags around a string value
blink() Adds <blink> and </blink> tags around a string value
bold() Adds <b> and </b> tags around a string value
charAt() Finds out which character is at a given position in a string
charCodeAt() Finds the character code of a character at a given position in a string
concat() Adds two or more strings together and returns the new combined string
value
fixed() Adds <tt> and </tt> tags around a string value
fontcolor() Adds <font color=“color”> and </font> tags around a string value, which
change the color of the string to a specified color
fontsize() Adds <font size=“number”> and </font> tags around a string value,
which change the size of the string to a specified size given as a number
fromCharCode() Uses character codes sent as parameters to create a new string
indexOf() Searches for a character sent as a parameter in a string: if it’s found, the
position of the first instance of the character is returned; otherwise, it
returns –1
italics() Adds <i> and </i> tags around a string value
lastIndexOf() Searches for a character sent as a parameter in a string: if it’s found, the
position of the last instance of the character is returned; otherwise, it
returns –1
link() Creates HTML links using the string as the link text and linking to the
URL sent as a parameter
match() Compares a regular expression and a string to see if they match
replace() Finds out if a regular expression matches a string and then replaces a
matched string with a new string
search() Executes the search for a match between a regular expression and a
specified string
slice() Pulls out a specified section of a string value and returns a new string
small() Adds <small> and </small> tags around a string value
split() Separates a string into an array of strings based on a character sent as a
parameter to the method
strike() Adds <strike> and </strike> tags around a string value
sub() Adds <sub> and </sub> tags around a string value
substr() Allows a portion of the string specified with a starting position and
ending after a certain number of characters to be returned
substring() Allows a portion of the string specified with a starting position and an
ending position to be returned
sup() Adds <sup> and </sup> tags around a string value
toString() Returns the string literal value of a String object
toLowerCase() Converts a string to all lowercase letters and returns the result
toUpperCase() Converts a string to all uppercase letters and returns the result
ی ؼـٌز سفٙؽ) یؼٙٞ ا٘دبْ ٔی ؼhtml < ؼـ ِفطبرbig></big> بیٞ ًٕبٖ وبـ سٞ ایٗ ٔشؽ
)ٖٛ وبیف ٔشٝ' ٘ىجز ثّٕٝوفؼٖ خ
5,-(0 َٔثب
5 َٔؼبؼ
یٙؽ) یؼٙٞ ا ا٘دبْ ٔی ؼhtml < ؼـ ِفطبرblink ></ blink > بیٞ ًٕبٖ وبـ سٞ ایٗ ٔشؽ
ٕٝٞ ایٗ ٔشؽ ؼـٝـؼ)اِیشٚـر زٍٕه قٖ ؼـ ٔی آِٛ ٝبی آٖ لفاـ ٔیٍیفؼ ـا ثٞ ً ؼـ ثیٗ سٝی وٙٔش
)ؼٌٛ ب اخفا ٕ٘یٞـٌفٚٔف
5,-(1 َٔثب
5 َٔؼبؼ
ٚ bold(), fixed(), italics(), small(), strike(), sub(),ٝاٖ ثٛ ٔی سٜٚبی ایٗ ٌفٞاق ؼیٍف ٔشؽ
ٗ ٔشٝ اوشبِی غبِی ـا ثٚ فوؽاْ وجهٞ ٚ بی لجّی ٔی ثبٌؽِٟٕبٖ ٔثبٞ بٟ٘ ٘فق وبـ آٝ وفؼ) وٜ اٌبـsup()
)ؽٍٙٔی ثػ
ب ثفای اـسجبٖ لىٕزٟ٘ اق آٝبیی وٞ ًبیی ؼاـیؽ' سٌٙ ؼـ اذ سی اْ اَ آ#ٍفِٙ$ـٛبی آ٘سٞ ًضشٕب ثب س
هٙ لىٕز ؼیٍف ِیٝ لىٕشی اق یه ٔشٗ ثٝـر وِٛ ٗؼ' ثؽیٌٛ ٔیٜ یه ؼیٍف اوشفبؼٝبی ٔػشّف ٔشٗ ثٞ
ٌؽ ٚ ٜثب وّیه ثفـٚی ِیٙه ٔجؽا ثِ ٝیٙه ٔمّؽ' ؤ ٝی سٛا٘ؽ ٔشٗ' ػىه ٚیب غیف ٜثبٌؽ ؼوشفوی دیؽا
ٔی وٙیٓ) ؼـ ایٗ لىٕز ٔی غٛاٞیٓ ٔشؽی ـا ٔؼففی وٙیٓ و ٝایٗ وبـ ـا ثفای ٔب ٚایٗ ثب ثٚ ٝویّ ٝوؽٞبی
خبٚااوىفیذز ا٘دبْ ٔی ؼٞؽ 5یؼٙی ٔ 5شؽ )(anchor
ٔثبَ 5,-(2
ٔی غٛاٞیٓ یه سً آ٘سٛـ ثفای ؼوشفوی ث part 1 ٝایدبؼ وٙیٓ ) اثشؽا ثبیؽ part 1ـا ث ٝث ٝؼـوشی ٚ
ثٚ ٝویّ ٝقیف سؼفیف وٙیٓ 5
اٌف ثػٛاٞیٓ ث ٝیه ٔشٗ ؼـ htmlـً٘ غبِی ثجػٍیٓ ثبیؽ زیكی ٕٞب٘ٙؽ وؽ قیف ـا ثىبـ ثجفیٓ' اِجشٝ
ؼـ ایٙدب ٔٙظٛـ سٟٙب اوشفبؼ ٜاق وؽٞبی ٚ htmlثؽ ٖٚوٕه ٌففشٗ اق ٔ Cssی ثبٌؽ 5
><font color="color_value">text_string</font
أب اٌف ثػٛاٞیٓ ٕٞیٗ ػُٕ ـا ثٚ ٝویّ ٝخبٚااوىفیذز ٌجی ٝوبقی وٙیٓ ٔی سٛا٘یٓ اق ٔشؽ
)( fontcolorثٟف ٜثجفیٓ ٕٞب٘ٙؽ ٔثبَ قیف)
ٔثبَ 5,-(3
><body
>"<script type="text/javascript
;"!var the_text = "I am so mad I am red
;))"document.write(the_text.fontcolor("red
185 دَلران پی سی
Hadi Ahmadi Learning Java Script
</script>
</body>
5 َٔؼبؼ
)ٓ ٕ٘بییٜٓ اوشفبؼٞ بٞ ًٍ٘كاؼویٕبَ ـٞ َا٘یٓ اق ٔؼبؼٛ اق ٔشؽ ثبال ٔی سٍٜبْ اوشفبؼٙٞ ؼـ
5,-(4 َٔثب
<body>
<script type="text/javascript">
var the_text = "I am so mad I am red!";
document.write(the_text.fontcolor("#FF0000"));
</font>
5َٔؼبؼ
اق ٘بْ آٖ ثفٔیٝـ وٕٛٙ٘بٞ ٝ ٔی ثبٌؽ' وfontsize() ااوىفیذزٚؼ ؼـ خبٛخٛٔ بیٞیىی ؼیٍف اق ٔشؽ
)ؼٚ وبـ ٔی ـٝ ٔشٗ ثٜظیٓ وفؼٖ ا٘ؽاقٙآیؽ ثفای س
5,-(,+ َٔثب
<body>
<script type="text/javascript">
5 َٔؼبؼ
)ٓ ٔی ٕ٘بییٜ اق ایٗ ٔشؽ اوشفبؼٝبی ٔػشّف ؼـ ِفطٞ هٙثفای لفاـ ؼاؼٖ ِی
5,-(,, َٔثب
<body>
<script type="text/javascript">
var link_text = "A Web Site";
var full_link = link_text.link("http://www.dlp.lxb.ir");
document.write(full_link);
</script>
</body>
5 َٔؼبؼ
5,-(,- َٔثب
5 غزٛٔیؽ آٞاٛـ ـا غٛ آ٘سٚ هٙبی ِیٞ وبـ ٔشؽٜٛؼـ ٔثبَ قیف ٘ط
<body>
<p>
<script type="text/javascript">
var anchor_text = "Part 1";
var full_anchor = anchor_text.anchor("part1");
document.write(full_anchor);
</script>
</p>
<p>
Part 1 is about this, that and the other thing.<br />
<br /><br /><br /><br />
This is irrelevant text in this case used for filler.
</p>
<p>
<p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br />
<p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br />
<p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
<br /><p>This is irrelevant text in this case used for
filler.</p><br />
<br />
ٖ ٌٕب ثب ؼاؼٝب وٙؼ) ثؽیٗ ٔؼٚ ثىبـ ٔی ـٝلؼیز ؼـ ـٌشٛٔ بظف ثب یهٙایٗ ٔشؽ ثفای دیؽا وفؼٖ ضفف ٔش
ٗ اق ایٜیؽ وفؼ) ؼـ اوشفبؼٞاٛؽ ـا ؼـیبفز غٙىشٞ بظف ثب آٖ ػؽؼٙ ٔشٝ ؼـ ـٌشٝفی ـا وٚ ضفٝدبـأشف ػؽؼی ث
) یهٝ٘ ٘ؽٌٛ ع ٔیٚلؼیز ِفف ٌؿٛٔ اقٝف یه ـٌشٚ ضفٝ ثبٌیؽ وٝٔشؽ ثبیؽ ؼلز ؼاٌش
5,-(,. َٔثب
.charAt() وٕه ٔشؽٝ ثٚ length اق غبِیزٜ ثب اوشفبؼّٕٝدیؽا وفؼٖ ضفف آغف یه و
ثبٌیؽ أب دیؽاٝ ؼوشفوی ؼاٌشَٝ یه ـٌشٚ ضفف اٝ ثcharAt() اق ٔشؽٜا٘یؽ ثب اوشفبؼٛ ٔی سٍٕٝیٞ ٌٕب
) ثىیب ـاضز سف ٔی ثبٌؽlength اق غبِیزٜوفؼٖ ضفف آغف ثب اوشفبؼ
5,-(,/ َٔثب
189 دَلران پی سی
Hadi Ahmadi Learning Java Script
ؽٙ یب زٚ زىجب٘ؽٖ ؼٝی ثٙغشیٓ) یؼٛٔب آٞ ٝ آـایٖٝ ثٛ ؼـ فُّ ٔفثٝؽ وٕٞبٖ وبـی ـا ا٘دبْ ٔیؽٞ ایٗ ٔشؽ
)ٝـٌش
5,-(,0 َٔثب
5 ٝ٘شید
5,-(,1 َٔثب
5 ٝ٘شید
)ؽٞف وبـوشف ػّٕی ـا ا٘دبْ ٔیؽٞ ٖٝ ثٛ اق وؽ ٔفثٜایٗ ٔشؽ ثب اوشفبؼ
5,-(,1 َٔثب
window.alert(String.fromCharCode(72,73));
ٕٗیٞ وذهٜؼٕٛ٘ ُ سجؽیH یٙ ٔؼبؼَ آٖ یؼٝ آٖ ـا ثٚ ٝ ـا ٌففش2- یِٙیٗ دبـأشفیؼٚایٗ وؽ اثشؽا ا
ٖاٛٙ ػٝ ـا ثHI بیزٟ٘ ؼـٚ ٔی ٕ٘بیؽٕٝ سفخI ٝ آٖ ـا ثٚ ٜٓ ا٘دبْ ؼاؼٞ 2. یْٙ یؼٚوبـ ـا ثب دفأشف ؼ
)خی ثفٔی ٌفؼا٘ؽٚغف
( ـا, ؼ ٔمؽاـٍٛ٘ ـؼ ٘ظف دیؽاٛٔ اٌف ضففٚ) ـا ثفٔی ٌفؼا٘ؽٝ یب ـٌشِّٕٝیٗ ضفف یه وٚلؼیز اٛٔ ایٗ ٔشؽ
)ثفٔی ٌفؼا٘ؽ
5,-(,2 َٔثب
) ٔی ثبٌؽCool ِّٕٝیٗ ضفف وٚ اC ـا ثفٔی ٌفؼا٘ؽ قیفا ضفف0 ؼـ ٔثبَ ثبال
5,-(,3 َٔثب
ٝؼ)ؼلز ٕ٘بییؽ وٌٛ ـؼ ٘ظف یبفز ٕ٘یٛٔ ّٕٝ" ؼـ وc" ـا ثفٔی ٌفؼا٘ؽ قیفا-1 ؼـ ٔثبَ ثبال ٔشؽ ٔمؽاـ
)زهٛ وc ٝ٘ اوزٜع ٌؽٚ ثكـي ٌفC ثبCool
5,-(,4 َٔثب
ـؼ ٘ظفٛٔ ؼ قیفا ضففٌٛ ٔیٜ ثفٌفؼا٘ؽYour character is not in the string ّٕٝؼـ ٔثبَ ثبال خ
)ؼٌٛ یبفز ٕ٘یّٕٝؼـ و
5,-(-+ َٔثب
"( ؼـo") ِٗیٚ ـا ثفٔی ٌفؼا٘ؽ) قیفا اYour character is at position 1 ّٕٝؼـ ٔثبَ ثبال خ
)ؼٌٛ یبفز ٔیCool ّٕٝ و, لؼیزٛٔ
5,-(-, َٔثب
}
else {
window.alert("Please consider fruit rather than candy.");
}
indexOf ؼ قیفا ٔشؽٌٛ ٔیٜ"( ثفٌٍز ؼاؼYes, fruit is good for you!") ّٕٝؼـ ٔثبَ ثبال خ
)َ ـا ثفٔی ٌفؼا٘ؽٚ إّٝجیك ؼاؼٖ آٖ خٙ ثب سٚ ٜبوبیی وفؼٌٙ ـؼ ٘ظفٛٔ ٝ ـا ؼـ ـٌشfruit ّٕٝو
وذهٜ ـا دبیً وفّٕٝ اثشؽا سٕبْ خٝب وٙؽ' ثؽیٗ ٔؼٙ ػُٕ ٔی وindexOf() ایٗ ٔشؽ ػىه ٔشؽ
)ـؼ ٘ظف اوز ـا ثفٔی ٌفؼا٘ؽٛٔ ٝلؼیشی وٛٔ ٗآغفی
5,-(-- َٔثب
)ؼٌٛ ٔیٜ ؼاؼٝ ٔمؽاـ ِفف ثفٌٍشٝ ایٓ ؼـ ٘شیدٜ وفؼٜ اوشفبؼindexOf ؼـ ٔثبَ ثبال اق ٔشؽ
5 ؼـ وؽ قیفٝؼـ ضبِی و
ایٗ ٔشؽ ؼ ٚدبـأشف ؼـیبفز ٔی وٙؽ) ٘ ٚفیم ٝػُٕ وفؼٖ آٖ ثؽیٗ ِٛـر اوز و ٝاثشؽا دبـأشف ا َٚـا ثب ٔشٗ
یب ـٌش ٝسٙجیك ٔی ؼٞؽ ٚؼـ ِٛـر یبفشٗ آٖ ؼـ ٔشٗ یب ـٌش 'ٝدبـأشف ؼ ْٚـا خبیٍكیٗ آٖ ٔی وٙؽ)
ٔثبَ 5,-(-.
><body
>"<script type="text/javascript
;"var the_text = "Benz Cruising Car
;)"var the_text_replace = the_text.replace("Benz","BMW
;)window.alert(the_text_replace
></script
></body
ایٗ ٔشؽ ثفای خؽا وفؼٖ لىٕشی اق ٔشٗ یب ـٌش ٚ ٝسجؽیُ آٖ ث ٝـٌش ٝای خؽاٌب٘ ٝث ٝوبـ ٔی ـٚؼ) ایٗ
ٔشؽ ؼ ٚدبـأشف ؼـیبفز ٔی وٙؽ' و ٝدبـأشف اٛٔ َٚلؼیز وبـوشفی ـا ٍ٘بٖ ٔی ؼٞؽ و ٝػّٕیبر اق آٖ ٌفٚع
ٔی ٌٛؼ ٚدبـأشف ؼٛٔ ْٚلؼیز وبـوشفی ـا ٍ٘بٖ ٔی ؼٞؽ و ٝػّٕیبر ثفي سب آ٘دب اؼأٔ ٝی یبثؽ)
ٔثبَ 5,-(-/
ؼـ ٔثبَ ثبال ” “Do notث ٝػٛٙاٖ غفٚخی ٟ٘بیی ثفٌفؼا٘ؽٔ ٜی ٌٛؼ)
ایٗ ٔشؽ یه آـای ٝاق اخكایی و ٝؼـ ـٌشٔ ٝشٙی لفاـ ؼاـ٘ؽ ایدبؼ ٔی وٙؽ)ایٗ ٔشؽ دبـأشفی ـا ث ٝػٛٙاٖ
خؽاوٙٙؽ ٜؼـیبفز ٔی ٕ٘بیؽ ٚثف اوبن آٖ ػُٕ splitوفؼٖ ـا ا٘دبْ ٔی ؼٞؽ)
ٔثبَ 5,-(-1
ُٕ دبـأشف ثفای ایٗ ػٚؽ) اق ؼٞخی ٔیؽٚاٖ غفٛٙ ػٝ آٖ ـا ثٚ ٜایٗ ٔشؽ لىٕ شی اق ٔشٗ ـا ضؿف وفؼ
)ؽٙلف آٖ ـا اػالْ ٔی وْٛ سٚ دبـأشف ؼٚ ع ػّٕیبرَٚ ٌفٚ دبـأشف اٝؽ وٙ ٔی وٜاوشفبؼ
5,-(-2 َٔثب
ب ـا ثفایٞؽ سب دبـأشفٞ ٔی ؼٜ ٌٕب اخبقٝؽ أب ایٗ ٔشؽ ثٙ ػُٕ ٔی وsubstr() ؽ ٔشؽٕٙ٘بٞ ایٗ ٔشؽ
)خی ثفٔی ٌفؼا٘ؽٚاٖ غفٛٙ ػٝ ـا ثٜبیز ٔشٗ ضؿف ٌؽٟ٘ ؼـٚ یؽٙ اـوبَ وّٕٝلؼیز ؼـ خٛٔ ٗ آغفیٚ ِٗیٚا
5,-(-3 َٔثب
)ؼٚ وبـ ٔی ـٝ ثstring literal بهString object ایٗ ٔشؽ ثفای سجؽیُ یه
5,-(-4 َٔثب
ایٗ ٔشؽ ا٘دبْ ٔیٝزه وبـی اوز وٛف وٚ ضفٝب ثٟ٘ سجؽیُ آٚ ٗؼ ؼـ ٔشٛخٛٔ ف ثكـيٌٚففشٗ ضف
)ؽٞؼ
5,-(.+ َٔثب
<body>
<script type="text/javascript">
var the_text = "I FEEL CALM, REALLY.";
document.write(the_text.toLowerCase());
</script>
</body>
5ٝ٘شید
)ف ثكـيٚ ضفٝزه ثٛف وٚی سجؽیُ ضفٙؽ) یؼٞػّٕی ػىه ٔشؽ ثبال ا٘دبْ ٔی ؼ
5,-(., َٔثب
<body>
<script type="text/javascript">
var the_text = "I am yelling!";
document.write(the_text.toUpperCase());
</script>
</body>
5 ٝ٘شید
I AM YELLING!
ٗؽ سب اق ایٙ اوز زٜ ایؽ' ؼـ ٔثبَ قیف وؼی ٌؽٜب ٌؽٌٙ ای آٝؼ ؼـ ٌی ـٌشٛخٛٔ بیٞضبال ٌٕب ثب ٔشؽ
)ٓ وبـ ثجفیٝٓ ثٞ ب ـا ثبٞٔشؽ
5,-(.- َٔثب
<body>
<h1>Welcome!</h1>
<script type="text/javascript">
function getname() {
var the_text=window.prompt("Enter your first and last
name","");
if (the_text.indexOf(" ") == -1) {
window.alert("Put a space between your first and last name.
Try again.");
getname();
}
var split_text= the_text.split(" ");
if ((split_text[0].charAt(0) != "Z") ||
(split_text[0].charAt(0) != "z")) {
var shorter_fn_string =
split_text[0].substring(1,split_text[0].length);
new_fn_name = "Z"+shorter_fn_string;
}
else {
var shorter_fn_string =
split_text[0].substring(1,split_text[0].length);
new_fn_name = "W"+shorter_fn_string;
}
if ((split_text[1].charAt(0)!= "Z") ||
(split_text[1].charAt(0)!= "z")) {
var shorter_ln_string=
split_text[1].substring(1,split_text[1].length);
new_ln_name="Z"+shorter_ln_string;
}
else {
var shorter_ln_string=
split_text[1].substring(1,split_text[1].length);
new_ln_name="W"+shorter_ln_string;
}
window.alert("Now your name is "+new_fn_name+"
"+new_ln_name+"!");
}
getname();
</script>
</body>
ؼـ ایٗ ٔثبَ اثشؽا اوٓ وبـثف ؼـیبفز ٔی ٌٛؼ ٚوذه ضفٚف ا َٚثب ضفف " "Zخبیٍكیٗ ٔی ٌٛؼ) ٞف
زٙؽ ٔثبَ ثبال یه ٔثبَ وبـثفؼی ٕ٘ی ثبٌؽ أب ٌٕب ـا ثب اوشفبؼ ٜاق ٔشؽٞبی ٔٛخٛؼ ؼـ ٌی ـٌشٔ ٝشٙی
ثیٍشف آٌٙب ٔی وٙؽ)
RegExpچیست
یه ػجبـر ٔٙظٓ (ٌ )Regular Expressionی ئی اوز و ٝاٍِٛی وبـاوشفٞب ـا ٍٔػُ ٔی وٙؽ)
ٚلشی وٌٕ ٝب ؼـ ضبَ خىشد ٛؼـثبـ ٜػجبـسی ؼـ یه ٔشٗ ٞىشیؽ ٍ٘بٖ ؼٙٞؽ ٜایٗ اوز وٌٕ ٝب ث ٝؼ٘جبَ
زٔ ٝی ٌفؼیؽ) یه اٍِٛی وبؼٔ ٜی سٛا٘ؽ ٌبُٔ فمٗ یه وبـاوشفثبٌؽ)
اٍِٞٛبی دیسیؽ ٜسف وبـاوشفٞبی ثیٍشفی ؼاـ٘ؽ ٚثفای سدكی ،)Parse( ٝثفـوی وبغشبـ یب ففٔز،
خبٍ٘یٙی ٚیب وبـٞبی ؼیٍف ٔٛـؼ اوشفبؼ ٜلفاـ ٌیفؼ)
ػجبـار ٔٙظٓ اثكاـی لؽـسٕٙؽ ثفای اخفای "اٍِ ٛوٙدی ٚخىشد ٚ ٛخبثدبیی" ؼـ ٔشٗ ٞب ث ٝوبـ ٔی
ـٚؼ)
ٔثبَ 5,-(..
}
else {
window.alert("Not my name, but it will work!");
}
Wow, we have the same name ْ ثبٌؽ دیبJohn ؽٙاـؼ ٔی وٚ وبـثفٝؼـ ٔثبَ ثبال اٌف ٘بٔی و
)ؼٌٛ ٔیٜ ٕ٘بیً ؼاؼNot my name, but it will work ْـر دیبّٛٙ ٕ٘بیً ؼـ ٔی آیؽ ؼـ غیف ایٝث
Adding Flags
جیك ؼاؼٖ ؼـ ایٗ ٔشٗ یبٙ ػّٕیبر سٝیؽ وٙا٘یؽ ٍٔػُ وٛ) ٌٕب ٔی سFlags( بٞ ٓ اق دفزٜثب اوشفبؼ
)ف ٔشٗ ؼیٍفی ا٘دبْ ٌیفؼٞ ؼـ
Flag(s) Purpose
i Makes the match case insensitive
g Makes the match global
m Makes the match work in multiline mode
: Flag i
فٚزىی ضفٛظف اق ثكـٌی یب وٙجیك' ِففٙؽ ػّٕیبر سٙ ٍٔػُ ٔی وٝ ىز وٙ ایFlag ٗغبِیز ای
)ؼٚف اق ثیٗ ٔی ـٚزىی ضفٛ وٚ ثكـٌیٝ ضىبویز ثٝا٘دبْ ٌیفؼ ؼـ ٘شید
5,-(./ َٔثب
: Flag g
ؼوزٝا٘ی ٘یك ثٕٛػٞ ِٗیٚب ـا ثؼؽ اق دیؽا وفؼٖ اٞ ا٘یٕٛػٞ ْییٓ سٕبٍٛ ثٝىٙ ثفای ایg (global$ اق
ؽٕٙ٘بٞ ؽَٙ ٕ٘ی وٛب ـا لجٞ ٖ آٚ ثكـي ضىبن اوزٚ زهٛف وٚ ضفٝؼ) ایٗ ٔؽیفبیف ثٚ وبـ ٔی ـٝـؼ ثٚآ
ٌٕبٝ ای وّٕٝ ؼ٘جبَ وٝبة وفؼ ایٗ ٔؽیفبیف ثَٙ اخشٚ اIs اقٚ بوبیی وفؼٌٙ ـاis ٚ فمٗ ؼٕٝیٗ ٔثبَ وٞ
)ؽٙ اوشػفاج ٔی وstr ؽ آٖ ـا اق ؼاغُ ٔشغیفٕٙ٘بٞ ٚ ایؽ ٔی ٌفؼؼٜاـؼ وفؼٚ pattern ؼـ لىٕز
5,-(.0 َٔثب
<body>
<h1>Welcome!</h1>
<script type="text/javascript">
var str = "Is this all there is?";
var patt1 = /is/g;
document.write(str.match(patt1));
</script>
</body>
5 ٝ٘شید
is,is
ب ٔی ثبٌؽٞ ٖخی سّفیمی اق آٚ غفٚ اوزٜ ٌؽٜ اوشفبؼFlag ٚف ؼٞ ؼـ ٔثبَ قیف اق
5,-(.1 َٔثب
<body>
<h1>Welcome!</h1>
<script type="text/javascript">
var str = "Is this all there is?";
var patt1 = /is/gi;
document.write(str.match(patt1));
</script>
</body>
5ٝ٘شید
Is,is,is
test)( هتذ
ٝؼ ؼاٌشٛخٚ ٖ ؼـ آٝـسی وِٛ ؼـٚ ـؼ ٘ظف ٌٕب ٔی ٌفؼؼٛٔ ایٗ ٔشؽ ؼـ ؼاغُ یه ٔشٗ ؼ٘جبَ ٔمؽاـ
) ـا ثف ٔی ٌفؼا٘ؽfalse ـر ٔمؽاـِٛ ٗ ؼـ غیف ایٚ true ثبٌؽ ٔمؽاـ
5,-(.2 َٔثب
<body>
<script type="text/javascript">
var patt1 = new RegExp("e");
document.write(patt1.test("the best things in life are
free"));
</script>
</body>
5ٝ٘شید
true
exec)( هتذ
ـا ثفٜـر دیؽا وفؼٖ آٖ ٔشٗ دیؽا ٌؽِٛ ؼـٚ ٝـؼ ٘ظف ٌٕب ٌٍشٛٔ ایٗ ٔشؽ ؼـ ؼاغُ ٔشٗ ؼ٘جبَ ٔمؽاـ
) ـا ثف ٔی ٌفؼا٘ؽnull ؽ ٔمؽاـٙ دیؽا ٘ىٝـسی وِٛ ؼـٚ ٔی ٌفؼا٘ؽ
5,-(.3 َٔثب
<body>
<script type="text/javascript">
var patt1 = new RegExp("e");
document.write(patt1.exec("the best things in life are
free"));
</script>
</body>
5ٝ٘شید
ؼ اوز ـاٛخٛٔ ْٟٛ ٔفٝؼ وٌٛ خی زبح ٔیٚ ؼـ غفe ثبٌیؽ فمٗ یهٝ ٘یك ؼاٌشe ٜؼـ ٔثبَ ثبال اٌف ٌٕب ؼ
)ٔی ـوب٘ؽ
) ٔی ٕ٘بییؽٜؽٞب ـا ٍٔبٟ٘ـؼ آٛٔ ٔیطبسی ٔػشّف ؼـٛ سٚ RegExp بیٞؼـ قیف ِیىشی اق وؽ
varname= stringname.replace(regex,newstring);
5,-(.4 َٔثب
var mystring= "I like the way a new car smells, and cars are
fun.";
var toreplace=/car/;
var newstring= mystring.replace(toreplace,"skunk");
window.alert(newstring);
)ؽْٙ ٔی وٛ ػskunk ّٕٝؽ ـا ثب وٙ ثجیّٕٝ ؼـ خٝ وcar ِّٕٝیٗ وٚ اreplace ؼـ ایٗ ٔثبَ ٔشؽ
5,-(/+ َٔثب
var mystring= "I like the way a new car smells, and cars are
fun.";
var toreplace=/car/g;
var newstring= mystring.replace(toreplace,"skunk");
window.alert(newstring);
ٓ ایٜ وفؼٜ آٖ اوشفبؼٜٕفاٞ ٝ ثRegExp gاق ٝر وٚؽ ٔثبَ لجُ ٔی ثبٌؽ ثب ایٗ سفبٕٙ٘بٞ ٔثبَ ثبال
)ؽْٙ ٔی وٛ ػskunk ّٕٝؽ ـا ثب وٙ ثجیّٕٝ ؼـ خٝ وcar َ ُز کلوreplace ٔشؽٝؼـ٘شید
ؼ ٔشؽ آٖ ـاٌٛ ـؼ ٘ظف اوز) اٌف ایٗ ِغز دیؽاٛٔ ّٕٝبثك ثب وٙٔ ّٕٝ ایٗ ٔشؽ دیؽا وفؼٖ یه وٝظیفٚ
)( ـا ثفٔی ٌفؼا٘ؽ, ـر ٔمؽاـّٛٙ ؼـ غیف ایٚ ثفٔی ٌفؼا٘ؽ
5,-(/, َٔثب
ؼٛ غٝ٘ ایٗ ٔشؽٝ ؼاـؼ) اِجشٜؽٟ ـا ثفػّٕٝـؼ ٘ظف ؼـ خٛٔ ثفٌفؼا٘ؽٖ ِغزٚ ٖ دیؽا وفؼٝظیفٚ ایٗ ٔشؽ
)ِیٗ ضفف آٖ ـا ثفٔی ٌفؼا٘ؽٚلؼیز ػؽؼی اٛٔ ِٝغز ـا و
5,-(/- َٔثب
5,-(/. َٔثب
ىزٙ اوىفیذز قیف ایٝظیفٚ )یؽٙ ٔی وٜؽٞ ـا ٍٔبRegExp ٚ ؽ ٔشؽٙ اق زٜ اوشفبؼٜٛؼـ ٔثبَ قیف ٘ط
)ؼ ؼاـؼ یب غیفٛخٚ ـؼ ٘ظف ػؽؼیٛٔ ٗؽ آیب ؼـ ٔشٙ ٍٔػُ وٝو
<body>
<form>
Enter some text: <input type="text" id="has_digits" />
<input type="button" id="t_btn" value="Test" />
</form>
<script type="text/javascript">
var t_button = document.getElementById("t_btn");
t_button.onclick = function() {
var has_num = document.getElementById("has_digits").value;
var tomatch = /\d+/;
if (tomatch.test(has_num)) {
window.alert("Your entry contained one or more numbers!");
}
else {
window.alert("Your entry did not contain any numbers!");
}
}
</script>
</body>
اطالػات تیطتز
یىی اقٝٔیطبر ثیٍشف ثٛا٘یؽ ثفای سٛؼ ؼـ ایٗ ثػً ـا ٘بوبفی ٔی ؼا٘یؽ ٔی سٛخٛٔ ٔیطبرٛ سٝب٘سٙز
) ٕ٘بییؽٝبی قیف ٔفاخؼٟثىبیشٚ
● www.regular-expressions.info/
● www.regular-expressions.info/javascript.html (includes specifics on the JavaScript engine)
● https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
فزم ّا
قٔب٘ی وٌٕ ٝب ؼ ٚسً > <form></formـا ؼـ وؽ htmlغٛؼ ٚاـؼ ٔی وٙیؽ یه ٌی ففْ ایدبؼ
وفؼ ٜایؽ ففْ ٞب ٘مٍی ضیبسی ٚاوبوی ؼـ اـسجبٖ وبـثف ثب ِفطبر ٚة ایفب ٔی وٙٙؽ) ؼـ ٘شیدٔ ٝشؽٞب ٚ
غبِیز ٞبی ففاٚا٘ی ثفای وبـ ثب آٟ٘ب ؼـ خبٚا اوىفیذز ففاٌ ٓٞؽ ٜاوز)
ٔثبَ 5,.(,
]document.forms[0
٘ىشٕٞ 5 ٝب٘ٛٙـ و ٝؼـ ٔثبَ ثبال ٍٔبٞؽٔ ٜی وٙیؽ ٌفٚع آـای ٝاق ػؽؼ ِفف اوز ؼـ ٘شیدٔ ٝثال
] document.forms[1ث ٝؼٔٚیٗ ففْ ٔٛخٛؼ ؼـ ِفط ٝاٌبـٔ ٜی وٙؽ)
length Property
ایٗ غبِیز سؼؽاؼ اخكای ٔٛخٛؼ ؼـ یه ففْ ـا ثفٔی ٌفؼا٘ؽ)
5,.(- َٔثب
<body>
<form>
Name: <input type="text" /> <br />
E-mail: <input type="text" /> <br />
<input type="submit" value="Submit" />
</form>
<p>
<script type="text/javascript">
document.write("The form has "+document.forms[0].length+"
elements.");
</script>
</p>
</body>
5 ٝ٘شیط
submit ٕٝ یه ؼوٚ یٙؼی ٔشٚـٚ ٚ اوز ؼٝ خكء لفاـ ٌففشٝقیفا ؼـ ففْ ٔثبَ ثبال و
5 ٝ٘ىش
) ـا ثفٔی ٌفؼا٘ؽx لؼیزٛٔ ؼـٝؼ ؼـ ففْ لفاـ ٌففشٛخٛٔ سؼؽاؼ اخكای5 document.forms[x].length
5,.(. َٔثب
<body>
<h1>Form Lengths</h1>
<h2>Form 1</h2>
<form>
Name: <input type="text" /><br />
E-mail: <input type="text" /><br />
<input type="submit" value="Submit" />
211 دَلران پی سی
Hadi Ahmadi Learning Java Script
</form>
<h2>Form 2</h2>
<form>
Favorite Color: <input type="text" /><br />
Favorite Food: <input type="text" /><br />
<input type="reset" value="Reset" />
<input type="submit" value="Submit" />
</form>
<h2>Results</h2>
<script type="text/javascript" >
for(var count=0;count<document.forms.length;count++) {
var formnum = count+1;
document.write("Form "+formnum+" has
"+document.forms[count].length);
document.write(" elements.<br />");
}
</script>
</body>
5ٝ٘شید
ٖ آّٝویٚ ٝ یه ٘بْ اغشّبَ ؼاؼ سب ثؼؽا ثٝف ففْ ؼـ ِفطٞ ٝاٖ ثٛ لجُ سف ثیبٖ وفؼیٓ ٔی سٝـ وٕٛٙ٘بٞ
ٌٕب ٘یبقیٝ ففْ ؼـ ایٗ اوز وٝ اق آـایٜ اوشفبؼٝ ففْ ؼوشفوی دیؽا وفؼ )ٔكیز ایٗ أف ٘ىجز ثٝاٖ ثٛ٘بْ ثش
)اـؼ ٕ٘بییؽٚ ْیؽ ؼاٌز) ٌٕب ثبیؽ ایٗ ٘بْ ـا ؼـ سً ففٞاٛ ٘ػٝلؼیز ففْ ؼـ ِفطٛٔ ؼا٘ىزٝث
name=“yourname”
5,.(/ َٔثب
ثب وٕهٚ ْ اق ایٗ ٘بٜ ثؼؽ ثب اوشفبؼٚ ٓ ایٜ ففْ اغشّبَ ؼاؼٝ _ ثinfo_form_ ْؼـ ٔثبَ قیف یه ٘ب
)ٓ ایٜ آٖ ؼوشفوی دیؽا وفؼٝ ثlength غبِیز
<body>
211 دَلران پی سی
Hadi Ahmadi Learning Java Script
<form name="info_form">
Name: <input type="text" /><br />
<input type="submit" />
</form>
<p>
<script type="text/javascript">
document.write("The form has "+document.info_form.length+"
elements.");
</script>
</p>
</body>
5 ٝ٘شید
ID استفادُ اس
ID ثب ؼاؼٖ یهٝىز وٙ ؼاـؼ ایٝ ٔكیشی وٚ ففْ اوزٝؽ ؼاؼٖ یه ٘بْ ثٕٙ٘بٞ ٘یكID اقٜاوشفبؼ
)ٓیٙب ـا اق یىؽیٍف ٔشٕبیك وٟ٘ آٝف ففْ ؼـ ِفطٞ ٝ ففؼ ثٝطّف ثٙٔ
5,.(0 َٔثب
<body>
<form id="info_form">
Name: <input type="text" /><br />
<input type="submit" />
</form>
<p>
<script type="text/javascript">
var f_length = document.getElementById("info_form").length;
document.write("The form has "+f_length+" elements.");
</script>
</p>
</body>
Properties
٘ؽ ؼـٌٛ ة ٔیٛب ٘یك ٌی ٔطىٞ ْٖ ففٛ زٚ بیی ٔی ثبٌؽٞ ا اوىفیذز ؼاـای غبِیزٚف ٌی ؼـ خبٞ
) ٔی ٕ٘بییؽٜؽَٞ قیف ٍٔبٚؽ) سؼؽاؼی اق غئبَ ٌی ففْ ـا ؼـ خؽٙی ٘یىشٙ ٔىشثٜ اق ایٗ لبػؽٝ٘شید
Property Value
action The value of the action attribute in the HTML form tag
An array that includes an array element for each form element in an
elements
HTML form
encoding The value of the enctype attribute, which varies with different browsers
length The value of the total number of elements in an HTML form
method The value of the method attribute in an HTML form tag
name The value of the name attribute in an HTML form tag
target The value of the target attribute in an HTML form tag
5,.(1 َٔثب
<body>
<form name="info_form"
action="http://someplace.com/php/form.php">
Name: <input type="text" /><br />
<input type="submit" />
</form>
<p>
<script type="text/javascript">
document.write("The form goes to "+document.info_form.action);
</script>
</p>
</body>
ٔثبَ 5,.(2
>"<form name="info_form
>Name: <input type="text" /><br /
><input type="submit" /
></form
ٌٕب ٕٞسٙیٗ ٔی سٛا٘یؽ اق ٘ IDیك ثفای ؼوشفوی ث ٝاخكای آٟ٘ب وٕه ثٍیفیؽ)
ٔثبَ 5,.(3
><form
>Name: <input type="text" id="yourname"><br /
>"<input type="submit
></form
5,.(4 َٔثب
<body>
<form>
Check box to say Yes: <input type="checkbox" id="yes_no">
<br /><br />
<input type="button" value= "See the Answer"
onclick="is_it_checked();" />
</form>
<script type="text/javascript">
function is_it_checked() {
var y_n = document.getElementById("yes_no");
if (y_n.checked) {
window.alert("Yes! The box is checked!");
}
else {
window.alert("No, the box is not checked!");
}
}
</script>
</body>
ٝیی ثٛ ـاؼیٕٝی ثف ػالٔز قؼٖ * ٘كؼٖ ؼوٙ دیغبٔی ٔجSee the Answer ٕٝی ؼوٚثب وّیه وفؼٖ ـ
)ٕ٘بیً ؼـ ٔی آیؽ
) ٔی ٕ٘بییؽٝئٞفوؽاْ ـا ٍٔبٞ بیٞ یمٌیٚ ٚ بٞ ٔشؽٜٕفاَٟ قیف اخكای یه ففْ ثٚؼـ خؽ
5,.(,+ َٔثب
<form>
Do you want us to send you e-mail updates and offers?<br />
Yes <input type="checkbox" id="yes" checked="checked" />
No <input type="checkbox" id="no" />
</form>
) ٔی ثبٌؽtext area ٚ text box ٝ ٔشؽ ثبال ؼاؼٖ ٔمؽاـ دیً ففْ ثٝظیفٚ
5,.(,, َٔثب
ٚ ؽٙ یب ٔمؽاـ آٖ ـا دبن وٚ ی سبیخ ٕ٘بیؽٙ ٔشٝؼ ـا ؼـ خؼجٛـؼ ٘ظف غٛٔ ؼـ ٔثبَ قیف اٌف وبـثف وبیز
ایٗ أفٝ ثReset Default ٕٝ ٕ٘بیؽ ثب قؼٖ ؼوٜؽ اق ٔمؽاـ دیً ففْ اوشفبؼٞاٛف ؼِیّی ثػٞ ٝضبَ ث
)ؽٙؼوز دیؽا ٔی و
<body>
<form>
Favorite URL:<br />
<input type="text" id="favurl" value="http://www.yahoo.com">
<br /><br />
<input type="button" value="Reset Default"
onclick="back_to_default();">
</form>
<script type="text/javascript">
function back_to_default() {
var url_box = document.getElementById("favurl");
url_box.value = url_box.defaultValue;
}
</script>
216 دَلران پی سی
Hadi Ahmadi Learning Java Script
</body>
ٖ ففْ یب یىی اق اخكای آٝ ثفای اـخبع ثthis وّیؽیّٕٝ ٌٕب اق وٝؽ وٞایٗ ٔشؽ قٔب٘ی ـظ ٔی ؼ
ی ـا سغییفٙ ٔشٝیٓ ٔمؽاـ دیً ففْ یه خؼجٞاٛ اٌف ثػٝیؽ وفؼ وٞاٛ غٜؽٞ ٕ٘بییؽ)ؼـ ٔثبَ قیف ٍٔبٜاوشفبؼ
ٝیٓ وذه ثٙ وٜ اٌبـthis وّیؽیّٕٝ اق وٜ ؼـ آٖ لفاـ ؼاـؼ ثب اوشفبؼٝ خؼجٝ ففٔی وٝیٓ ثبیؽ اثشؽا ثٞؼ
ٓیٞیٓ سغییف ؼٞاٛ ٔی غٝـؼ ٘ظف وٛٔ ثؼؽ اق آٖ غبِیزٚ textbox ْ وذه ٘بform ّٕٝسفسیت اثشؽا و
)ٓیٙاـؼ وٚ بیز ٔمؽاـ خؽیؽ ـاٟ٘ ؼـٚ value ؽٙ٘ٔب
5,.(,- َٔثب
<form>
Favorite URL:<br />
<input type="text" name="favurl" value=http://www.yahoo.com />
<br /><br />
<input type="button" value="Change"
onclick="this.form.favurl.value='http://www.lycos.com';" />
</form>
5,.(,. َٔثب
<body>
<form>
Fruits:
<select id="optlist">
<option selected="selected" value="orange">Orange</option>
<option value="apple">Apple</option>
<option value="pear">Pear</option>
</select>
</form>
<p>
<script type="text/javascript">
var fbox = document.getElementById("optlist");
document.write("The second option is ");
document.write(fbox.options[1].value);
</script>
</body>
option زِّٕٙف اٙٔیٗ ػٚ ٔمؽاـ ؼٝ ثfbox.options[1].value اق وؽٜؼـ ٔثبَ ثبال ثب اوشفبؼ
)ٓ ایٜؼوشفوی دیؽا وفؼ
)ؼٌٛ ٔیٜ ؼـ ففْ اوشفبؼّٝف ثىبـ ـفشٙع ػٛ٘ ٖثفای ٍٔػُ وفؼ
5,.(,/ َٔثب
type=“text”
type=“button”
)ؽٙ ٔی وٜؽٞ وبـثف ٍٔبٕٝبٖ ٘بٔی اوز وٞ ْؽ) ایٗ ٘بٞؼ ؼـ ففْ اغشّبَ ٔی ؼٛخٛٔ ّفٙ ػٝیه ٘بْ ث
)ؼٌٛ ی یىی اق اخكای ففْ ٔشٕفوكٚؽ وبـ ثفٞاٛ وبـثف ثػٝؽ وٍٞبٔی ـظ ٔی ؼٙٞ
5,.(,0 َٔثب
<body>
<form>
Your Favorite Food
<input type="text" id="fav_food" /><br />
Drink <input type="text" />
</form>
<script type="text/javascript">
)ْی اخكای ففٚؽ سفن سٕفوك ثف ـٞػّٕی ثفػىه غبِیز ثبال ا٘دبْ ٔی ؼ
5,.(,1 َٔثب
<body>
<form>
Your Favorite Food
<input type="text" name="fav_food"
onblur="this.form.annoy.click();"
/><br />
Drink <input type="text" />
<br /><br />
<input type="reset" name="annoy" value="Reset Form">
</form>
</body>
َٚی اٙ ٔشٝؼ ـا اق خؼجٛالغ سٕفوك غٚ یؽ ؼـْٙ وّیه ٔی وٚی ؼٙ ٔشٝی خؼجٚ ـٍٝبٔی وٙٞ ؼـ ٔثبَ ثبال
) ٕ٘بیً ؼـ ٔی آیؽٝ دیبٔی ثفای ٌٕب ثٚ ٘ؽؼٛع ٔی دیٛلٚ ٝ ٔشؽ ثٝ ؼـ ایٗ ِطظٚ ایؽْٜ ا٘شمبَ ؼاؼٚ ؼٝث
post ٚ get ٔمؽاـٚ ؼاـای ؼٚ ـ اوزٚ وفٝ اـوبَ ا٘الػبر ثٝایٗ غبِیز ثفای ٍٔػُ وفؼٖ ٘فیم
)اوز
5,.(,2 َٔثب
5,.(,3 َٔثب
<form name="cool_form">
<!-- form contents here -->
</form>
5 ٌبُٔ ٔمبؼیف قیف ٔی ثبٌؽٚ)ؽٞـر ٘یبق ـا ٍ٘بٖ ٔی ؼِٛ خؽیؽ ؼـٜدفٙف ٌؽٖ دٞایٗ غبِیز ٔطُ ظب
blank
new
parent
top
self
) ٕ٘بییؽٝبِؼٙٔ ـاhtml قيٖٛٔ آٛبِت ایٗ ثػً ٔشٙٔ ٖؼ ثفای فٕیؽٌٛ ٔیِٝیٛس
5,.(,4 َٔثب
Methods
5,.(-+ َٔثب
<body>
<form>
Your Favorite Food
<input type="text" name="fav_food" onblur="this.form.reset();"
/><br />
Drink <input type="text" />
<br /><br />
<input type="reset" value="Reset Form" />
<input type="submit" value="Submit Form" />
</form>
</body>
5,.(-, َٔثب
<body>
<form action="http://site.com/php/form.php">
Your Favorite Food
<input type="text" name="fav_food"
onblur="this.form.submit();" /><br />
Drink <input type="text" />
<br /><br />
<input type="submit" value="Submit Form" />
</form>
</body>
) ا٘دبْ ٔی ٌیفؼSubmit ی ػّٕیبرٙ ٔشٝ خؼجٚاـؼ وفؼٖ ا٘الػبر ؼـ ؼٚ ؼـ ٔثبَ ثبال ثؼؽ اق
5,.(-- َٔثب
یب
)ؼٌٛ ُ اخكای ففْ ٍٔػٝب ثٞ اثىشٍی ثفزىتٚ ؼ سبٌٛ ثبػث ٔیlabelبیٞ ً اق سٜاوشفبؼ
5,.(-. َٔثب
<label for="yourname">Name</label>
<input type="text" name="yourname" id="yourname" /><br />
یهٝبیی وٞcheck box ٚ بٞradio button ٌٕب ؼـ ایدبؼٝا٘ؽ ثٛب ٔی سٞfieldset اقٜاوشفبؼ
)ؽٞ لفاـ ؼٜٚب ـا ؼـ یه ٌفٟ٘ آٚ ؽٙ ٍٔػُ ؼاـ٘ؽ وٕه وٝظیفٚ
5,.(-/ َٔثب
<fieldset>
<legend>Select a Fruit:</legend>
<input type="radio" name="fruits" id="fruits1" value="Orange"
/>
<label for="fruits1">Orange</label>
<input type="radio" name="fruits" id="fruits2" value="Banana"
/>
<label for="fruits2">Banana</label>
<input type="radio" name="fruits" id="fruits3" value="Apple"
/>
<label for="fruits3">Apple</label>
</fieldset>
یىی اق أىب٘بسی و html ٝثفای ٘فاضبٖ ففا ٓٞآٚـؼ ٜاوز ایٙىز ؤ ٝی سٛا٘یٓ ثٚ ٝویّ ٝیىىفی
ػٙبِف غبَ ثٕ٘ ٝبیً زٙؽیٗ ِفط ٝؼـ یه دٙدفٔ ٜفٚـٌف ثذفؼاقیٓ' ؼـ اُِ ثب اوشفبؼ ٜاق ایٗ ػٙبِف ٔی
سٛا٘یٓ و ٝدٙسفٔ ٜفٚـٌف ـا ث ٝزٙؽ ثػً و ٝثٞ ٝفوؽاْ یه ففیٓ (ٌ )frameفشٔ ٝی ٌٛؼ سمىیٓ وفؼ ٚؼـ
ٞف لىٕز یه ِفط ٝـا ٕ٘بیً ؼٞیٓ)
ایٗ ثػً ٔفث ٖٛثٔ ٝشؽٞب ٚـٚي ٞبیی اوز و ٝخبٚااوىفیذز ثفای وبـ ثب ففیٓ ٞب آٔبؼ ٜوفؼ ٜاوز وٝ
ؼـ اؼأ ٝثٔ ٝؼففی آٟ٘ب غٛاٞیٓ دفؼاغز)
ثفای ٌفٚع آٔٛقي ؼـ اثشؽا ٘یبق اوز و ٝؼِ ٚفط html ٝثب ٘بْ ٞبی ٚ frame1.html
frame2.htmlایدبؼ ٕ٘ٛؼ ٚ ٜؾغیفٕ٘ ٜبییؽ)
frame1.html
><html
><body
!I am frame1.html, and I am on the left side
></body
></html
frame2.html
><html
><body
!I am frame2.html, and I am on the right side
></body
></html
<html>
<frameset cols="20%,80%">
<frame src="frame1.html"></frame>
<frame src="frame2.html"></frame>
</frameset>
<noframes>
Sorry, your browser does not support frames. Use the link
below to go to the frameless version of the site.<br />
<a href="noframes.html">Frameless Site</a>
</noframes>
</html>
ـٌف ثىبـ ٔیٚ ٔفٜدفٙ ا٘ؽ ثفای سمىیٓ وفؼٖ دٝ< ثىبـ ـفشframeset> ً ؼـ سٝبیی وٞ ؼـ ِؽ5ٝ٘ىش
)ؽٙٞ اغشّبَ ٔی ؼ- ٓ ففیٝ ـا ثٜدفٙ" فٕبی د3+ ٚ ,ٓ ففیٝ ـا ثٜدفٙ" د-+ ٝـر وِٛ ٗ٘ؽ ثؽیٚـ
) ٕ٘بییؽٜ ؾغیفٌٝٛب ـا ؼـ دٟ٘ آٚ ایدبؼ ٕ٘بییؽhtml ٝ ِفطٝ وؽ ثبال وٝ اق وٜثب اوشفبؼ
Frame Options
بٞ ٓـر افمی ثفای ٕ٘بیً ففیِٛ ٝ ـا ثٝا٘ؽ ِفطٛ ٔی سframeset ًیؽ سٙ ٔی وٜؽٞ ٍٔبٝـ وٕٛٙ٘بٞ
5 ؽ ٔثبَ قیفٙ٘ؽ ٔبٙسمىیٓ و
5,/(, َٔثب
<html>
<frameset rows="20%,80%">
<frame src="frame1.html"></frame>
<frame src="frame2.html"></frame>
</frameset>
<noframes>
Use the link below to go to the frameless version of the
site.<br />
<a href="noframes.html">Frameless Site</a>
</noframes>
</html>
اي ثفایٜ" ا٘ؽاق3+ ٚ ٓ اي ثفای ٕ٘بیً یه ففیٜ" ا٘ؽاق-+ بیی ثفاثف ثبٞفٙ وٝ ثٝؼـ ٔثبَ ثبال ِفط
) اوزٜٕ٘بیً ؼیٍف ففیٓ سمىیٓ ٌؽ
ففیٓ ؼیٍفٝ ثٜ ٔمؽاـ ثبلی ٔب٘ؽٝـی وٛٙیؽ ثٙب ـا ٍٔػُ وٞ ٜدفٙ یىی اق دٜ فمٗ ا٘ؽاقٝیؽ وٞاٛاٌف ثػ
.ا٘یؽ اق ػالٔز "*" استفادٍ ًواییذٛاغشّبَ یبثؽ ٔی س
5,/(- َٔثب
<html>
<frameset rows="150,*">
<frame src="frame1.html"></frame>
<frame src="frame2.html"></frame>
</frameset>
<noframes>
Use the link below to go to the frameless version of the
site.<br />
<a href="noframes.html">Frameless Site</a>
</noframes>
</html>
)ؽ ٔثبَ قیف ػُٕ ٕ٘بییؽٕٙ٘بٞ ا٘یؽٛ٘ی ٘یك ٔی سٛـر وشِٛ ٝب ثٞفٙثفای ٕ٘بیً و
دیىىُ ثفای ففیٓ وٕز ـاوز ؼـ ٘ظف,0+ ٚ دیىىُ ثفای ففیٓ وٕز زخ,0+ ؼـ ٔثبَ قیف ٔمؽاـ
)ی اغشّبَ ٔی یبثؽٙوٚ ٓ ففیٝ ففیٓ ثٚ ثیٗ ؼٜ ؼیٍف فٕبی ثبلی ٔب٘ؽٚ اوزٜ ٌؽٌٝففش
5,/(. َٔثب
<html>
<frameset cols="150,*,150">
<frame src="frame1.html"></frame>
<frame src="frame2.html"></frame>
<frame src="frame3.html"></frame>
</frameset>
<noframes>
Use the link below to go to the frameless version of the
site.<br />
<a href="noframes.html">Frameless Site</a>
228 دَلران پی سی
Hadi Ahmadi Learning Java Script
></noframes
></html
ٔثبَ 5,/(/
ؼـ قیف ٘ط ٜٛسمىیٓ وفؼٖ ِفط ٝث ٝففیٓ ٞبی افمی ٚػٕٛؼی ثٛ٘ ٝـ ٕٞكٔبٖ ـا ٍٔبٞؽٔ ٜی ٕ٘بییؽ)
>"*<frameset rows="100,
><frame src="frame1.html"></frame
>"*<frameset cols="150,
><frame src="frame2.html"></frame
><frame src="frame3.html"></frame
></frameset
></frameset
><noframes
Use the link below to go to the frameless version of the
>site.<br /
><a href="noframes.html">Frameless Site</a
></noframes
ؼـ ٔثبَ ثبال ,++دیىىُ اق وُ فٕبی افمی ِفط ٝث ٝففیٓ ,اغشّبَ ٔی یبثؽ' وذه فٕبی ثبلی
ٔب٘ؽِ ٜفطٕ٘ ٝبیً ثفای ٕ٘بیً ففیٓ ٞبی ػٕٛؼی اوشفبؼٔ ٜی ٌفؼؼ)
آیب ٔی ؼا٘یؽ و ٝزٍٔ ٝ٘ٛی سٛاٖ ث ٝففیٓ ٞب ؼـ خبٚا اوىفیذز ؼوشفوی دیؽا وفؼ؟ ؼـ اؼأ ٝایٗ فُّ ثٝ
ِٛـر ٔفُّ ث ٝایٗ أف غٛاٞیٓ دفؼاغز)
ٔثبَ 5,/(0
229 دَلران پی سی
Hadi Ahmadi Learning Java Script
<html>
<frameset cols="60%,40%">
<frame src="frame1.html"></frame>
<frame src="frame2.html"></frame>
</frameset>
<noframes>
Use the link below to go to the frameless version of the
site.<br />
<a href="noframes.html">Frameless Site</a>
</noframes>
</html>
) ٕ٘بییؽframe2.html ثفای سىٕیُ ٌؽٖ ایٗ ٔثبَ ٌٕب ثبیؽ وؽ قیف ـا ؼـ
<body>
The first frame is from: <br />
<script type="text/javascript">
document.write(top.frames[0].location);
</script>
</body>
ٖ ؼـ آٖ ففیٓ وٕز ـاوز ٔىبٝ ٔی ٕ٘بییؽ وٜؽٞ ـا ٍٔبٝ اِّی ٘شیدٜدفٙضبَ ٌٕب ثب ثبق وفؼٖ د
)یىؽٛ٘ وبقی ففیٓ وٕز زخ ـا ٔیٜؾغیف
5,/(1 َٔثب
<html>
<frameset cols="20%,80%">
<frame src="frame1.html"></frame>
<frame src="frame2.html"></frame>
</frameset>
<noframes>
Use the link below to go to the frameless version of the
site.<br />
<a href="noframes.html">Frameless Site</a>
</noframes>
</html>
<html>
<body>
I am frame 1!
</body>
</html>
<body>
<script type="text/javascript">
for (var count=0; count<top.frames.length; count+=1) {
var framenum = count+1;
document.write("Frame "+framenum+" is from
"+top.frames[count].location);
document.write("<br />");
}
</script>
</body>
I am frame 1!
5,/(2 َٔثب
5یؽٙاـؼ وٚ بی قیف ـاٞف وؽاْ اق ِفػبر وؽٞ ؼـٚ ایدبؼ ٕ٘بییؽٝ٘ خؽاٌبٝ ِفطٝو
main farme
<html>
<frameset cols="50%,50%">
<frame src="frame1.html" name="left_side"></frame>
<frame src="frame2.html" name="right_side"></frame>
</frameset>
<noframes>
Use the link below to go to the frameless version of the
site.<br />
<a href="noframes.html">Frameless Site</a>
</noframes>
</html>
frame1.html
<body>
The second (right) frame is from: <br />
<script type="text/javascript">
document.write(top.right_side.location);
</script>
</body>
frame2.html
<body>
The first (left) frame is from: <br />
<script type="text/javascript">
document.write(top.left_side.location);
</script>
</body>
ضبَ اٌف ثػٛاٞیٓ ایٗ ػُٕ ـا ثب اوشفبؼ ٜاق خبٚااوىفیذز ا٘دبْ ؼٞیٓ ثبیؽ اق ٘فیك وؽٞبی قیف ث ٝآٟ٘ب
ؼوشفوی دیؽا وفؼ ٜوذه آٟ٘ب ـا ػ ْٛوٙیٓ)
ٔثبَ 5,/(3
ففْ وٙیؽ یه ِفط ٝخؽیؽ ث ٝاوٓ nextpageایدبؼ وفؼ ٜایٓ ٔ ٚی غٛاٞیٓ آٖ ـا خبیٍكیٗ
right_sideؼـ ٔثبَ لجُ ٕ٘بییٓ ثفای ایٗ ػُٕ ثِٛ ٝـر قیف ػُٕ ٔی وٙیٓ)
;"top.right_side.location="nextpage.html
ٚیب
>"'<a href="javascript:top.right_side.location='newpage.html
>New Page</a
ثفای ػ ْٛثیً اق یه ففیٓ ؼـ قٔبٖ ٌٕب ثبیؽ اق خبٚا اوىفیذز اوشفبؼٕ٘ ٜبییؽ)
ٔثبَ 5,/(4
اثشؽا یه فبیُ ثب ٘بْ htmlایدبؼ ٕ٘بییؽ وذه وؽ قیف ـا ؼـ آٖ ٚاـؼ وفؼ ٚ ٜآٖ ـا ثب ٘بْ
frameset4.htmlؾغیفٕ٘ ٜبییؽ)
><html
>"<frameset cols="20%,80%
><frame src="frame1.html" name="left_side"></frame
><frame src="frame2.html" name="right_side"></frame
</frameset>
<noframes>
Use the link below to go to the frameless version of the
site.<br />
<a href="noframes.html">Frameless Site</a>
</noframes>
</html>
ٖاـؼ وفؼٚ ده اقٚ ایدبؼframe4.html سبframe1.html بیٞ ْ ؼیٍف ثب ٘بhtml ُبـ فبیٟضبَ ز
) ٕ٘بییؽٜب ـا ؾغیفٟ٘بی قیف آٞوؽ
frame1.html
<body>
<script type="text/javascript">
function twoframes() {
top.right_side.location="frame4.html";
self.location="frame3.html";
}
</script>
<a href="javascript:twoframes();">Change Both Frames</a>
</body>
frame2.html
<body>
I am frame 2!
</body>
frame3.html
<body>
I am frame 3!
</body>
frame4.html
<body>
234 دَلران پی سی
Hadi Ahmadi Learning Java Script
ضبَ فبیُ frameset4.htmlـا ثبق ٕ٘بییؽ ٍٔبٞؽٔ ٜی ٕ٘بییؽ و ٝثب وّیه ثف ـٚی Change Both
Framesففیٓ ٞبی - ٚ ,ػٔ ْٛی ٌ٘ٛؽ ٚففیٓ ٞبی / ٚ .خبیٍكیٗ آٟ٘ب ٔی ٌ٘ٛؽ)
اٌف ٌٕب ثػٛاٞیؽ و ٝیه خؼج ٝا٘شػبة ثب اوشفبؼ ٜاق ففیٓ ٞب ایدبؼ ٕ٘بییؽ ؼـ ٔٛـؼ select box
navigationؼـ خبٚا اوىفیذز ا٘الػبسی ؼاٌش ٝثبٌیؽ)
ٔثبَ 5,/(,+
ٔی غٛاٞیٓ یه دٙدفٌ ٜبُٔ ؼ ٚففیٓ ایدبؼ ٕ٘بییٓ ثٛٙـیى ٝؼـ ففیٓ ثبال یه خؼج ٝا٘شػبة ٚخٛؼ ؼاٌشٝ
ثبٌؽ و ٝثب ا٘شػبة ٌكیٞ ٝٙبی آٖ اػٕبَ ٔٙبوجی ؼـ ففیٓ قیفیٗ ِٛـر دؿیفؼ)اق خّٕ ٝػ ْٛوفؼٖ ففیٓ
قیفیٗ ثب ففیٓ ٞبی ؼیٍف)
frameset5.html
><html
>"*<frameset rows="120,
><frame src="frame1.html" name="t_frame"></frame
><frame src="frame2.html" name="b_frame"></frame
></frameset
><noframes
Use the link below to go to the frameless version of the
>site.<br /
><a href="noframes.html">Frameless Site</a
></noframes
></html
frame1.html
<body>
<form onsubmit="return go_there();">
<label for="s1">Change the lower frame:</label>
<select id="s1">
<option selected="selected" value="#">Choose
Destination</option>
<option value="frame3.html">Frame 3</option>
<option value="frame4.html">Frame 4</option>
<option value="frame2.html">Back to Frame 2</option>
</select>
<input type="submit" id="submit" value="Go!" />
</form>
<script type="text/javascript">
function go_there() {
s = document.getElementById("s1");
top.b_frame.location = s.options[s.selectedIndex].value;
return false;
}
</script>
</body>
frame2.html
<body>
I am frame 2!
</body>
frame3.html
<body>
I am frame 3!
</body>
frame4.html
<body>
I am frame 4!
</body>
یه خؼج ٝا٘شػبة ؼـ ففیٓ ,ثٕ٘ ٝبیً ؼـ ٔی آیؽ و ٝثب ا٘شػبة ٌكیٞ ٝٙبی آٖ ٚفٍفؼٖ ؼوٕGo! ٝ
سغییفار ؼـ ففیٓ -ایدبؼ ٔی ٌ٘ٛؽ)
یىی اق ٔٛاـؼ ٔفیؽ اوشفبؼ ٜاق ففیٓ ایٙىز وٌٕ ٝب ٔی سٛا٘یؽ ٔشغیفی ـا ؼـ یه ففیٓ ؾغیف ٜوفؼ ٚ ٜؼـ
ؼیٍف ففیٓ ٞب ث ٝآٖ ؼوشفوی دیؽا ٕ٘بییؽ)ثفای ٔثبَ ٌٕب ٔی سٛا٘یؽ ا٘الػبر یه ففْ ـا ؼـ یه ففیٓ ؾغیفٜ
وفؼ ٚ ٜوذه آٖ ـا ؼـ ففیٓ ؼیٍفی ؼـ ٍٙٞبْ ِٛؼ ٌؽٖ ثبـٌكاـی ٕ٘بییؽ)
ٔثبَ 5,/(,,
اوٙبؼ قیف ـا ٔفضّ ٝثٔ ٝفضّ ٝایدبؼ وفؼ ٚ ٜؼـ یه ٔىبٖ ؾغیفٕ٘ ٜبییؽ)
frameset6.html
><html
>"*<frameset cols="150,
><frame src="frame1.html" name="left_side"></frame
><frame src="frame2.html" name="right_side"></frame
></frameset
><noframes
Use the link below to go to the frameless version of the
>site.<br /
><a href="noframes.html">Frameless Site</a
></noframes
></html
ایٗ ففیٓ وز ,0+دیىىُ ثفای ففیٓ وٕز زخ ٚثمی ٝا٘ؽاقِ ٜفطٕ٘ ٝبیٍٍف ـا ث ٝففیٓ وٕز ـاوز
اغشّبَ ٔی ؼٞؽ)
frame1.html
<head>
<script type="text/javascript">
var thename="";
var thefood="";
</script>
</head>
<body>
This is frame1.html; it holds the variable values.
You can put any content you like here.
</body>
) ٔی ٌفؼ٘ؽٜ ؾغیفٚ ٖ ٔمؽاـ ایدبؼٚبیی ثؽٞ ؼـ وٕز زخ لفاـ ٔی ٌیفؼ ٔشغییفٝؼـ ایٗ ففیٓ و
frame2.html
<body>
I'd like to get your name. Please enter it below.
<br />
<form onsubmit="return store_info();">
<label for="yourname">Your Name:</label>
<input type="text" id="yourname" size="25" />
<br /><br />
<input type="submit" value="Continue" />
</form>
<script type="text/javascript">
function store_info() {
var yn = document.getElementById("yourname").value;
top.left_side.thename= yn;
self.location="frame3.html";
return false;
}
</script>
</body>
ؼـ ٔشغیفٚ اـؼ ٕ٘بییؽ' ایٗ ٘بْ ؼـ ففیٓ وٕز زخٚ ْؼ سب یه ٘بٌٛ اوز ٔیٛؼـ ایٗ ففیٓ اق ٌٕب ؼـغ
)ؼٌٛ ٔیٜ ؾغیفthename
اقٜ ثب اوشفبؼٚ وّیه ٕ٘بیؽٕٝی ؼوٚ وبـثف ـٝؼ وٌٛ ٔیٜ قٔب٘ی ِؽاؼ قؼstore_info() سبثغ
ٝ ثtop.left_side.thename اقٜ ثب اوشفبؼٚ)ؼٌٛ ففیٓ وٕز زخ ؼوشفوی دیؽا ٔیٝ ثtop.left_side
)ؼٌٛ ٔیٜ ٘ىجز ؼاؼthename ٔشغیف
frame3.html
<body>
<script type="text/javascript">
document.write("Hi, "+top.left_side.thename+"!<br />");
</script>
Now I'd like to get your favorite food. Please enter it below:
<br />
<form onsubmit="return more_info();">
<label for="yourname">Your Name:</label>
<input type="text" id="yourname" size="25" />
<br />
<label for="yourfood">Favorite Food:</label>
<input type="text" id="yourfood" size="25" />
<br /><br />
<input type="submit" value="Continue" />
</form>
<script type="text/javascript">
var tn = document.getElementById("yourname");
tn.value = top.left_side.thename;
function more_info() {
var fd = document.getElementById("yourfood");
top.left_side.thefood= fd.value;
self.location="frame4.html";
return false;
}
</script>
</body>
ٖ ٘بْ ٌٕب ـا ٍ٘بٚ ٕ٘بیً ؼـ آیؽْٝ سب ثٛثز ففیٓ وٛ٘ جبـٙ ایٝٔ اؼإٝ قؼٖ ؼوٚ ْاـؼ وفؼٖ ٘بٚ ثؼؽ اق
) سبٖ ـا ٔی ٕ٘بیؽٝـؼ ػاللٛٔ اـؼ وفؼٖ غؿایٚ اوزٛ اق ٌٕب ؼـغٚ ٜؼاؼ
frame4.html
<body>
<script type="text/javascript">
function print_info() {
239 دَلران پی سی
Hadi Ahmadi Learning Java Script
ْبـٟ ففیٓ زٝب ثٞیؽ ٔمبؼیف ٔشغیفْٞ فٍبـ ٔی ؼٛ ـا ؼـ ففیٓ وٝٔ اؼإٝ ٌٕب ؼوٝ قٔبٖ وٚ بٟؼـ ا٘ش
)ؽٙ ٕ٘بیً ؼـ ٔی آیٝ ؼـ آ٘دب ثفای ٌٕب ثٚ ٜشمُ ٌؽٙٔ
<body>
<script type="text/javascript">
function print_info() {
document.write("Thank you, "+top.left_side.thename+"!");
document.write("<br /><br />");
document.write("You must really like
"+top.left_side.thefood+"!");
}
print_info();
</script>
</body>
سخي پایاًی
ؼـ ایٗ آٔٛقي ففْ ثف ایٗ ٌؿاٌشٌ ٝؽ وٌٕ ٝب ٔی ؼا٘یؽ و ٝزٍ ٝ٘ٛخبٚااوىفیذز ـا ثفای دٛیبسف ٌؽٖ
ٚخؿاة سف ٌؽٖ ٚة وبیششبٖ ثِ ٝفطبر HTMLغٛؼ أبف ٝوٙیؽ) ٌٕب یبؼ ٌففشیؽ زٍ ٝ٘ٛثٔ ٝشؽٞب دبوع
ؼٞیؽ ،زٍ ٝ٘ٛففْ ٞب ـا اػشجبـوٙدی وٙیؽ ٚزٍ ٝ٘ٛاوىفیذز ٞبی ٔػشّفی ثفای دبوع ث ٝوٙبـیٞٛبی ٔػشّف
ثىبقیؽ)
ایٗ آٔٛقي ٞب آٔٛقي ثف٘بٔٛ٘ ٝیىی ث ٝقثبٖ خبٚااوىفیذز ثٛؼ ٝ٘ ٚآٔٛقي سففٙؽٞبی خبٚااوىفیذز ٚ
ثیٍشف ٔٙبوت وىب٘ی اوز ؤ ٝی غٛاٙٞؽ ایٗ قثبٖ ـا ث ٝػٛٙاٖ یه قثبٖ ثف٘بٔٛ٘ ٝیىی وٕز والیٙز
ففاثٍیف٘ؽ ٝ٘ ٚوىب٘ی و ٝثػٛاٙٞؽ لبِت ٚثالي غٛؼ ـا ثب وؽٞبی خبٚااوىفیذز قیجب سف وٙٙؽ) اِجش ٝثب ایٗ
آٔٛقي ٞب ٚسّفیك آٖ ٞب ث ٝوبؼٌی سٕبْ ایٗ وبـٞبیی و ٝث ٝػٛٙاٖ وؽٞبی خبٚااوىفیذز ثفای ٚثالي ٞب
اـائٔ ٝی ٌٛؼ ـا ؼـ ِٛـسی و ٝوٕی ثب ػّٓ ثف٘بٔٛ٘ ٝیىی آٌٙب ثبٌیؽ ا٘دبْ ؼٞیؽ)
ٌبْ ثؼؽی یبؼٌیفی AJAX ٚ jQuery ، HTML DOMاوز) اٌف ٔی غٛاٞیؽ یه قثبٖ وٕز
وفٚـ ـا ففاثٍیفیؽ ٌ ASP.NET ٚ PHPكیٞ ٝٙبی دیً ـٚی ٌٕبوز
HTML DOM
HTML DOMاوشب٘ؽاـؼی ثفای ؼوشفوی ٚوبـ وفؼٖ ٚایدبؼ سغییف ـٚی ػٙبِف HTMLاوز)
یه دّشففْ ٔ ٚىشمُ اق قثبٖ ثف٘بٔٛ٘ ٝیىی اوز وٌٕ ٝب ٔی سٛا٘یؽ ؼـ قثبٖ ٞبی ثف٘بٔٛ٘ ٝیىی ٔػشّف اػٓ اق
))) ٚ VBScript ،JavaScript ،Javaاق آٖ اوشفبؼ ٜوٙیؽ)
ضشٕبً ایٗ دّشففْ ـا ثیبٔٛقیؽ قیفا و ٝثىیبـ ث ٝخبٚااوىفیذز ٚخبٚااوىفیذز ث ٝآٖ ٚاثىش ٝاوز $غِّٛبً
اق وٕز خبٚااوىفیذزٌ #بیؽ ثؼؽ اق خی وئفی ٚیب ٌبیؽ ٕٞكٔبٖ ثب ٓٞآٔٛقي ایٗ ثػً ـا ٌ ٓٞفٚع
وٙیٓ)
jQuery
AJAX
یه قثبٖ ثف٘بٔٛ٘ ٝیىی ٘یىز ِٚی ـاٞی خؽیؽ ثفای اوشفبؼ ٜاق اوشب٘ؽاـٞبی ٔٛخٛؼ اوز)
ؼـثبـ ٜخبثدب وفؼٖ ؼاؼٞ ٜب ثب یه وفٚـ ٚث ٝـٚقـوب٘ی ثػٍی اق ِفط ٝثؽ ٖٚثبـٌكاـی ٔدؽؼ ِفطٝ
اوز)
ٔثبَ ٞبیی اق آلاوه ٔثبَ ٌٔ ٌُٛخ ،خیٕیُ ،یٛسیٛة ٚست ٞبی فیه ثٛن اوز)
ASP.NET / PHP
ٕٞب٘ٛٙـ و ٝیه فبیُ HTMLؼـ وٕز والیٗ $ؼـ ؼاغُ ٔفٚـٌف #اخفا ٔی ٌٛؼ یه فبیُ
ASP.NET/PHPؼـ وٕز وفٚـ اخفا ٔی ٌٛؼ)
ثب اوشفبؼ ٜاق ایٗ قثبٖ ٞب ٌٕب ٔی سٛا٘یؽ ثِٛ ٝـر دٛیب ٞف ٔطشٛایی ـا ثٚ ٝة وبیششبٖ أبف ،ٝاق آٖ وٓ ٚ
یب آٖ ـا ٚیفایً وٙیؽ) ٔی سٛا٘یؽ اق ؼیشبثیه ٞب ثفای ؾغیف ٜا٘الػبر اوشفبؼٕ٘ ٜبییؽ)
به زودی...