ŗŻŗŻß£

ŗŻŗŻß£Share a Scribd company logo
JAVASCRIPT

                         BaĆøi 1: TOƅNG QUAN VEƀ JAVASCRIPT.

1. ƑaĆ«c tĆ­nh cuĆ»a ngoĆ¢n ngƶƵ Javascript:

J  avascript laĆø moƤt ngoĆ¢n ngƶƵ thoĆ¢ng dĆ²ch (interpreter), chƶƓng trƬnh nguoĆ n cuĆ»a noĆ¹ ƱƶƓĆÆc nhuĆ¹ng
   (embedded) hoaĆ«c tĆ­ch hĆ“ĆÆp (integated) vaĆøo taƤp tin HTML chuaĆ„n. Khi file ƱƶƓĆÆc load trong
Browser (coĆ¹ support cho JavaScript), Browser seƵ thoĆ¢ng dĆ²ch caĆ¹c Script vaĆø thƶĆÆc hieƤn caĆ¹c coĆ¢ng vieƤc
xaĆ¹c Ć±Ć²nh. ChƶƓng trƬnh nguoĆ n JavaScript ƱƶƓĆÆc thoĆ¢ng dĆ²ch trong trang HTML sau khi toaĆøn boƤ trang
ƱƶƓĆÆc load nhƶng trĆ¶Ć“Ć¹c khi trang ƱƶƓĆÆc hieĆ„n thĆ².
Javascript laĆø moƤt ngoĆ¢n ngƶƵ coĆ¹ ƱaĆ«c tĆ­nh:
ā€¢ Ć‘Ć“n giaĆ»n.
ā€¢ ƑoƤng (Dynamic).
ā€¢ HĆ¶Ć“Ć¹ng ƱoĆ”i tƶƓĆÆng (Object Oriented).


2. NgoĆ¢n ngƶƵ JavaScript:
MoƤt trong nhƶƵng ƱaĆ«c tĆ­nh quan troĆÆng cuĆ»a ngoĆ¢n ngƶƵ JavaScript laĆø khaĆ» naĆŖng taĆÆo vaĆø sƶƻ duĆÆng caĆ¹c ƱoĆ”i
tƶƓĆÆng (Object). CaĆ¹c Object naĆøy cho pheĆ¹p ngƶƓĆøi laƤp trƬnh sƶƻ duĆÆng ƱeĆ„ phaĆ¹t trieĆ„n Ć¶Ć¹ng duĆÆng.
Trong JavaScript ,caĆ¹c Object ƱƶƓĆÆc nhƬn theo 2 khĆ­a caĆÆnh:
    a. CaĆ¹c Object ƱaƵ toĆ n taĆÆi.
    b. CaĆ¹c Object do ngƶƓĆøi laƤp trƬnh xaĆ¢y dƶĆÆng.
Trong caĆ¹c Object ƱaƵ toĆ n taĆÆi ƱƶƓĆÆc chia thaĆønh 2 kieĆ„u:
    a. CaĆ¹c Object cuĆ»a JavaScript (JavaScript Built-in Object).
    b. CaĆ¹c ƱoĆ”i tƶƓĆÆng ƱƶƓĆÆc cung caĆ”p bĆ“Ć»i moĆ¢i trƶƓĆøng Netscape.

3. Built-in Object trong JavaScript:
JavaScript cung caĆ”p 1 boƤ caĆ¹c Built-in Object ƱeĆ„ cung caĆ”p caĆ¹c thoĆ¢ng tin veĆ  sƶĆÆ hieƤn haĆønh cuĆ»a caĆ¹c
ƱoĆ”i tƶƓĆÆng ƱƶƓĆÆc load trong trang Web vaĆø noƤi dung cuĆ»a noĆ¹.CaĆ¹c ƱoĆ”i tƶƓĆÆng naĆøy bao goĆ m caĆ¹c phƶƓng
phaĆ¹p (Method) laĆøm vieƤc vĆ“Ć¹i caĆ¹c thuoƤc tĆ­nh (Properties) cuĆ»a noĆ¹.

4. CaĆ¹c ƱoĆ”i tƶƓĆÆng ƱƶƓĆÆc cung caĆ”p bĆ“Ć»i moĆ¢i trƶƓĆøng Netscape:
Netscape Navigator cung caĆ”p caĆ¹c ƱoĆ”i tƶƓĆÆng cho pheĆ¹p JavaScript tƶƓng taĆ¹c vĆ“Ć¹i file HTML, caĆ¹c ƱoĆ”i
tƶƓĆÆng naĆøy cho pheĆ¹p chuĆ¹ng ta ƱieĆ u khieĆ„n vieƤc hieĆ„n thĆ² thoĆ¢ng tin vaĆø ƱaĆ¹p Ć¶Ć¹ng caĆ¹c sƶĆÆ kieƤn trong moĆ¢i
trƶƓĆøng Navigator.VĆ­ duĆÆ

ƑoĆ”i tƶƓĆÆng                             MoĆ¢ taĆ»
Window                Cung caĆ”p caĆ¹c phƶƓng phaĆ¹p vaĆø caĆ¹c tĆ­nh chaĆ”t cho cƶƻa soĆ„ hieƤn haĆønh
                      cuĆ»a trƬnh duyeƤt,bao goĆ m caĆ¹c ƱoĆ”i tƶƓĆÆng cho moĆ£i frame.


JAVASCRIPT                               MEDIASPACE CLUB (HTD)                                       PAGE: 1
Location             Cung caĆ”p caĆ¹c tĆ­nh chaĆ”t vaĆø phƶƓng phaĆ¹p laĆøm vieƤc vĆ“Ć¹i caĆ¹c Ć±Ć²a chƦ
                     URL hieƤn haĆønh ƱƶƓĆÆc mĆ“Ć».
History              CaĆ¹c ƱoĆ”i tƶƓĆÆng history cung caĆ”p thoĆ¢ng tin veĆ  caĆ¹c danh saĆ¹ch cuƵ vaĆø
                     coĆ¹ theĆ„ giĆ“Ć¹i haĆÆn sƶĆÆ tƶƓng taĆ¹c vĆ“Ć¹i danh saĆ¹ch.
Document             ƑaĆ¢y laĆø moƤt ƱoĆ”i tƶƓĆÆng ƱƶƓĆÆc sƶƻ duĆÆng nhieĆ u nhaĆ”t .NoĆ¹ chĆ¶Ć¹a ƱƶĆÆng caĆ¹c
                     ƑoĆ”i tƶƓĆÆng,tĆ­nh chaĆ”t vaĆø caĆ¹c phƶƓng phaĆ¹p laĆøm vieƤc vĆ“Ć¹i caĆ¹c thaĆønh
                     phaĆ n cuĆ»a taĆøi lieƤu nhƶ caĆ¹c :form,link,anchor,applet.

5. CaĆ¹c ƱoĆ”i tƶƓĆÆng do ngƶƓĆøi laƤp trƬnh xaĆ¢y dƶĆÆng:
a. Ć‘Ć²nh nghĆ³a thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng: (Object Properties)
CuĆ¹ phaĆ¹p : Object-name.Property-name (teĆ¢n ƱoĆ”i tƶƓĆÆng.teĆ¢n ƱaĆ«c tĆ­nh)
VĆ­ duĆÆ :MoƤt ƱoĆ”i tƶƓĆÆng airplane coĆ¹ caĆ¹c thuoƤc tĆ­nh nhƶ sau:
Airplane.model                                             Airplane.maxspeed
Airplane.price                                             Airplane.fuel
Airplane.seating
b. TheĆ¢m caĆ¹c phƶƓng phaĆ¹p cho ƱoĆ”i tƶƓĆÆng:( Method to Object)
Sau khi ƱaƵ coĆ¹ caĆ¹c thoĆ¢ng tin veĆ  airplane ta tieĆ”p tuĆÆc xaĆ¢y dƶĆÆng phƶƓng phaĆ¹p ƱeĆ„ sƶƻ duĆÆng thoĆ¢ng tin
naĆøy.VĆ­ duĆÆ baĆÆn muoĆ”n in ra moĆ¢ taĆ» cuĆ»a airplane hoaĆ«c tĆ­nh toaĆ¹n khoaĆ»ng caĆ¹ch toĆ”i Ʊa cuĆ»a cuoƤc haĆønh
trƬnh vĆ“Ć¹i nhieĆ¢n lieƤu ƱaƵ coĆ¹:
Airplane.description()
Airplane.distance()
c. TaĆÆo moƤt instance cuĆ»a ƱoĆ”i tƶƓĆÆng:
TrĆ¶Ć“Ć¹c khi thao taĆ¹c vĆ“Ć¹i moƤt ƱoĆ”i tƶƓĆÆng cuĆ»a JavaScript ta phaĆ»i taĆÆo moƤt instance cho ƱoĆ”i tƶƓĆÆng ƱoĆ¹.

6 . NhuĆ¹ng JavaScript vaĆøo trong taƤp tin HTML:
CuĆ¹ phaĆ¹p:
<SCRIPT LANGUAGE=ā€JavaScriptā€>
JavaScript Program
</SCRIPT>
ThuoƤc tƭnh cuƻa theƻ SCRIPT
+ SRC :Ć‘Ć²a chƦ URL chƦ ƱeĆ”n taƤp tin chƶƓng trƬnh JavaScript (*.js)
+ LANGUAGE: ChƦ Ć±Ć²nh ngoĆ¢n ngƶƵ ƱƶƓĆÆc sƶƻ duĆÆng trong Script vaĆø caĆ¹c phieĆ¢n baĆ»n sƶƻ duĆÆng (vĆ­ duĆÆ
nhƶ :JavaScript ,JavaScript .1.2 vvā€¦ ,VBScript).
7. Aƅn caĆ¹c Scripts ƱoĆ”i vĆ“Ć¹i caĆ¹c Browser khoĆ¢ng cung caĆ”p JavaScript:
<SCRIPT LANGUAGE=ā€JavaScriptā€>
<!- - DoĆøng daĆ”u Script ƱoĆ”i vĆ“Ć¹i caĆ¹c Browser khoĆ¢ng cung caĆ”p (support)
JavaScript Program
//DoĆøng keĆ”t thuĆ¹c vieƤc daĆ”u Script vaĆø chuĆ¹ thĆ­ch - - >
</SCRIPT>




JAVASCRIPT                              MEDIASPACE CLUB (HTD)                                       PAGE: 2
8. Sƶƻ duĆÆng taƤp tin JavaScript beĆ¢n ngoaĆøi :
<SCRIPT LANGUAGE=ā€JavaScriptā€ SRC=/slideshow/tai-lieu-javascript/5089421/ā€http:/www.hcmuns.edu.vn/scroll.jsā€>
<!- - DoĆøng daĆ”u Script ƱoĆ”i vĆ“Ć¹i caĆ¹c Browser khoĆ¢ng cung caĆ”p (support)
JavaScript Program
//DoĆøng keĆ”t thuĆ¹c vieƤc daĆ”u Script vaĆø chuĆ¹ thĆ­ch - - >
</SCRIPT>
9. TheĆ¢m chƶƓng trƬnh vaĆøo taƤp tin HTML:
<HTML>
<HEAD>
<TITLE>Listing 2.1</TITLE>
</HEAD>
<BODY>
Here is result:
<SCRIPT LANGUAGE="Javascript">
<!--
document.writeln("It work<BR>");
-->
</SCRIPT>
</BODY>
</HTML>
                               BaĆøi 2: SƖƛ DUƏNG JAVASCRIPT

1. CuĆ¹ phaĆ¹p cĆ“ baĆ»n cuĆ»a leƤnh :
JavaScript xaĆ¢y dƶĆÆng caĆ¹c haĆøm,caĆ¹c phaĆ¹t bieĆ„u,caĆ¹c toaĆ¹n tƶƻ vaĆø caĆ¹c bieĆ„u thĆ¶Ć¹c treĆ¢n cuĆøng moƤt doĆøng vaĆø
keĆ”t thuĆ¹c baĆØng ;
VĆ­ duĆÆ: document.writeln("It work<BR>");
2. CaĆ¹c khoĆ”i leƤnh:
NhieĆ u doĆøng leƤnh coĆ¹ theĆ„ ƱƶƓĆÆc lieĆ¢n keĆ”t vĆ“Ć¹i nhau vaĆø ƱƶƓĆÆc bao bĆ“Ć»i {   }
VĆ­ duĆÆ:
{
document.writeln("Does It work");
document.writeln("It work!");
}
3. XuaƔt dƶƵ lieƤu ra cƶƻa soƄ trƬnh duyeƤt:
DuĆøng 2 phƶƓng phaĆ¹p document.write() vaĆø document.writeln()
VĆ­ duĆÆ:
document.write(ā€œTestā€);
document.writeln(ā€œTestā€);
4. XuaĆ”t caĆ¹c theĆ» HTML tƶĆø JavaScript


JAVASCRIPT                                MEDIASPACE CLUB (HTD)                                         PAGE: 3
VĆ­ duĆÆ 1:
<HTML>                                                <SCRIPT LANGUAGE="Javascript">
<HEAD>                                                <!- -
<TITLE>Outputting Text</TITLE>                        document.write("This is text bold </B>");
</HEAD>                                               - ->
<BODY>                                                </SCRIPT>
This is text plain <BR>                               </BODY>
<B>                                                   </HTML>


VĆ­ duĆÆ 2:
<HTML>                                                     document.write('<IMG
<HEAD>                                                     SRC=/slideshow/tai-lieu-javascript/5089421/"welcome.gif">&
<TITLE>Example 2.4 </TITLE>                                document.write("<BR><H1>WELCOME TO
</HEAD>                                                    NETSCAPE 2.1</H1>");
<BODY>                                                     - ->
<SCRIPT LANGUAGE="Javascript">                             </SCRIPT>
<!- -                                                      </BODY>
                                                           </HTML>


5. Sƶƻ duĆÆng phƶƓng phaĆ¹p writeln() vĆ“Ć¹i theĆ» PRE:
<HTML>                                                     document.writeln("Two,");
<HEAD>                                                     document.write("Three");
<TITLE>Outputting Text</TITLE>                             document.write("...");
</HEAD>                                                    - ->
<BODY>                                                     </SCRIPT>
<PRE>                                                      </PRE>
<SCRIPT LANGUAGE="Javascript">                             </BODY>
<!- -                                                      </HTML>
document.writeln("One,");
6. CaĆ¹c kĆ­ tƶĆÆ Ć±aĆ«c bieƤt trong chuoĆ„i:
n : New line
t : Tab
r : carriage return
f : form feed
b: backspace
VĆ­ duĆÆ:
document.writeln("It work!n");

7. LaĆøm vieƤc vĆ“Ć¹i caĆ¹c dialog boxes
Sƶƻ duĆÆng haĆøm alert() ƱeĆ„ hieĆ„n thĆ² thoĆ¢ng baĆ¹o trong moƤt hoƤp.
VĆ­ duĆÆ:
<HTML>                                                     alert("Welcome to Netscape Navigator
<HEAD>                                                     21");
<TITLE>Example 2.5 </TITLE>                                document.write('<IMG
</HEAD>                                                    SRC=/slideshow/tai-lieu-javascript/5089421/"welcome.gif">&
<BODY>                                                     - ->
<SCRIPT LANGUAGE="Javascript">                             </SCRIPT>
<!- -                                                      </BODY>



JAVASCRIPT                               MEDIASPACE CLUB (HTD)                           PAGE: 4
8. TƶƓng taĆ¹c vĆ“Ć¹i ngƶƓĆøi sƶƻ duĆÆng:
Sƶƻ duĆÆng phƶƓng phaĆ¹p promt() ƱeĆ„ tƶƓng taĆ¹c vĆ“Ć¹i ngƶƓĆøi sƶƻ duĆÆng.
VĆ­ duĆÆ 1:
<HTML>                                                   document.write("Your favorite color
<HEAD>                                                   is:");
<TITLE>Listing 2.6</TITLE>                               document.writeln(prompt("enter your
</HEAD>                                                  favorite color:","Blue"));
<BODY>                                                   - ->
<SCRIPT LANGUAGE="Javascript">                           </SCRIPT>
<!- -                                                    </BODY>
                                                         </HTML>


VĆ­ duĆÆ 2:
<HTML>                                                   document.write("<H1>Greeting ,");
<HEAD>                                                   document.writeln(prompt("enter your
<TITLE>Listing 2.6</TITLE>                               name:","name"));
</HEAD>                                                  document.write("Welcome to netscape
<BODY>                                                   navigator 2.01 </H1>");
<SCRIPT LANGUAGE="Javascript">                           - ->
<!- -                                                    </SCRIPT>
document.write('<IMG                                     </BODY>
SRC=/slideshow/tai-lieu-javascript/5089421/"welcome.gif">&                                    </HTML>


Sƶƻ duĆÆng daĆ”u + ƱeĆ„ coƤng 2 chuoĆ„i ƱƓn laĆÆi:
VĆ­ duĆÆ 3:
<HTML>                                                   document.write("<H1>Greeting ," +
<HEAD>                                                   prompt("enter your name:","name") + "
<TITLE>Listing 2.6</TITLE>                               Welcome to netscape navigator 2.01
</HEAD>                                                  </H1>");
<BODY>                                                   - ->
<SCRIPT LANGUAGE="Javascript">                           </SCRIPT>
<!- -                                                    </BODY>
document.write('<IMG                                     </HTML>
SRC=/slideshow/tai-lieu-javascript/5089421/"welcome.gif">&


9. CaĆ¹c kieĆ„u dƶƵ lieƤu trong JavaScript:
a. DƶƵ lieƤu kieƄu soƔ:
   + SoĆ” nguyeĆ¢n: vĆ­ duĆÆ 720
   + SoĆ” Octal: vĆ­ duĆÆ :056
   + SoĆ” Hexa:vĆ­ duĆÆ:0x5F
   + SoĆ” thaƤp phaĆ¢n :vĆ­ duĆÆ :7.24 , -34.2 ,2E3
b. DƶƵ lieƤu kieƄu chuoƄi:
   vĆ­ duĆÆ: ā€ Helloā€
   ā€™245ā€™
   ā€œā€œ
c. DƶƵ lieƤu kieƄu Boolean:
   KeĆ”t quaĆ» traĆ» veĆ  laĆø true hoaĆ«c false.



JAVASCRIPT                                MEDIASPACE CLUB (HTD)                        PAGE: 5
d. DƶƵ lieƤu kieƄu null:
   TraĆ» veĆ  giaĆ¹ trĆ² roĆ£ng.
e. DƶƵ lieƤu kieĆ„u vaĆŖn baĆ»n (gioĆ”ng nhƶ kieĆ„u chuoĆ„i)

10. TaĆÆo bieĆ”n trong JavaScript:
Var example;
Var example=ā€Helloā€;
Ta coĆ¹ theĆ„ duĆøng document.write(example); ƱeĆ„ xuaĆ”t noƤi dung cuĆ»a bieĆ”n.
VĆ­ duĆÆ 1: duĆøng tƶĆø khoĆ¹a var ƱeĆ„ khai baĆ¹o bieĆ”n
<HTML>                                                       <BODY>
<HEAD>                                                       <SCRIPT LANGUAGE="Javascript">
<TITLE>Example 3.1</TITLE>                                   <!--
                                                             document.write('<IMG
<SCRIPT LANGUAGE="Javascript">                               SRC=/slideshow/tai-lieu-javascript/5089421/"Welcome.gif">&
<!- -                                                        document.write("<H1>Greeting ," +
var name=prompt("enter your                                  name + " Welcome to netscape
name:","name");                                              navigator 2.01 </H1>");
- ->                                                         -->
</SCRIPT>                                                    </SCRIPT>
                                                             </BODY>
</HEAD>                                                      </HTML>
VĆ­ du 2: taĆÆo laĆÆi moƤt giaĆ¹ trĆ² mĆ“Ć¹i cho bieĆ”n
<HTML>                                                       </HEAD>
<HEAD>                                                       <BODY>
<TITLE>Example 3.2</TITLE>                                   <SCRIPT LANGUAGE="Javascript">
                                                             <!--
<SCRIPT LANGUAGE="Javascript">                               document.write('<IMG
var name=prompt("enter your                                  SRC=/slideshow/tai-lieu-javascript/5089421/"Welcome.gif">&
name:","name");                                              document.write("<H1>Greeting ," +
alert ("greeting " + name + " , ");                          name + " Welcome to netscape
name=prompt("enter your friend's                             navigator 2.01 </H1>");
name:","friend's name");                                     -->
                                                             </SCRIPT>
</SCRIPT>                                                    </BODY>
                                                             </HTML>


11. LaĆøm vieƤc vĆ“Ć¹i bieĆ”n vaĆø bieĆ„u thĆ¶Ć¹c:
ā€¢   ThieĆ”t laƤp bieĆ„u thĆ¶Ć¹c:
    CuĆ¹ phaĆ¹p: <bieĆ”n> <toaĆ¹n tƶƻ > <bieĆ„u thĆ¶Ć¹c>
    * ToaĆ¹n tƶƻ:
    = ThieĆ”t laƤp giaĆ¹ trĆ² beĆ¢n phaĆ»i cho beĆ¢n traĆ¹i
       VĆ­ duĆÆ :x=5
    += CoƤng traĆ¹i vaĆø phaĆ»i ,sau ƱoĆ¹ gaĆ¹n keĆ”t quaĆ» cho beĆ¢n traĆ¹i pheĆ¹p toaĆ¹n
       VĆ­ duĆÆ: cho x=10,y=5
         x+=y => x=15
    -= TrƶĆø beĆ¢n traĆ¹i cho beĆ¢n phaĆ»i ,gaĆ¹n keĆ”t quaĆ» laĆÆi cho beĆ¢n traĆ¹i
         x-=y => x=5



JAVASCRIPT                                 MEDIASPACE CLUB (HTD)                           PAGE: 6
*= NhaĆ¢n beĆ¢n traĆ¹i cho beĆ¢n phaĆ»i,gaĆ¹n keĆ”t quaĆ» cho beĆ¢n traĆ¹i
        x*=y => x=50
    /= Chia beĆ¢n traĆ¹i cho phaĆ»i ,gaĆ¹n keĆ”t quaĆ» laĆÆi cho beĆ¢n traĆ¹i
       x/=y => x=2
    %= Chia beĆ¢n traĆ¹i cho beĆ¢n phaĆ»i vaĆø laĆ”y soĆ” dƶ gaĆ¹n laĆÆi cho beĆ¢n traĆ¹i
       x%=y => x=0
    * CaĆ¹c toaĆ¹n tƶƻ khaĆ¹c:
    VĆ­ duĆÆ:
    x+=15+3                                             y=++x; (=> y=6 vƬ x taĆŖng leĆ¢n 6)
    => x=18                                             z=x++; (=> z=6 vƬ sau ƱoĆ¹ x gaĆ¹n cho z)
    8+5                                                 sau ƱoĆ¹ x taĆŖng 1 => x=7
    32.5 * 72.3                                         Do ƱoĆ¹ ta coĆ¹ keĆ”t quaĆ» cuoĆ”i cuĆøng laĆø:
    12 % 5                                              x=7;y=6;z=6;
    DaĆ”u ++ vaĆø daĆ”u - - vaĆø daĆ”u -                     VĆ­ duĆÆ: x=5;
    VĆ­ duĆÆ:                                             x=-x => x=-5
    x=5;

ā€¢   PheĆ¹p toaĆ¹n Logic
    && : vaĆø
    ||: hoaƫc
    ! not
    VĆ­ duĆÆ:
    x=5 ,y=2 ,c=3
    (x>y) && (x>c)
    false && anything is always false
    (x>y) || (c<x) => true
    !x
ā€¢ ToaĆ¹n tƶƻ so saĆ¹nh trong JavaScript:
    ==                                         1==1 => true
    !=                                         3<1 =>false
    >                                          5 >=4 =>true
    <                                          ā€œtheā€ != ā€œheā€ => true
    >=                                         4==ā€4ā€ =>true
    <=
    VĆ­ duĆÆ:
ā€¢ ToaĆ¹n tƶƻ ƱieĆ u kieƤn:
CuĆ¹ phaĆ¹p:
(ƱieĆ u kieƤn ) ? giaĆ¹ trĆ² 1 : giaĆ¹ trĆ² 2
NeĆ”u ƱieĆ u kieƤn ƱuĆ¹ng thƬ traĆ» veĆ  giaĆ¹ trĆ² 1
NeĆ”u ƱieĆ u kieƤn sai thƬ traĆ» veĆ  giaĆ¹ trĆ² 2
VĆ­ duĆÆ:


JAVASCRIPT                               MEDIASPACE CLUB (HTD)                                     PAGE: 7
(day=ā€Saturdayā€) ? ā€œWeekendā€ : ā€œNot Saturdayā€
ā€¢ ToaĆ¹n tƶƻ chuoĆ„i:
ā€œ Welcome to ā€œ + ā€œ Netscape Navigatorā€
VĆ­ duĆÆ:
Var welcome=ā€Welcome toā€

Welcome += ā€œ Netscape Navigatorā€
! welcome= ā€œWelcome to Netsacpe Navigatorā€
VĆ­ duĆÆ : Sƶƻ duĆÆng toaĆ¹n tƶƻ ƱieĆ u kieƤn ƱeĆ„ kieĆ„m tra ngoĆ» vaĆøo
<HTML>                                                        var output = (response==answer) ?
<HEAD>                                                        correct:incorrect;
<TITLE>Example 3.3</TITLE>                                    </SCRIPT>
                                                              </HEAD>
<SCRIPT LANGUAGE="Javascript">
var question="What is 10+10 ?";                               <BODY>
var answer=20;                                                <SCRIPT LANGUAGE="Javascript">
var correct='<IMG                                             <!--
SRC=/slideshow/tai-lieu-javascript/5089421/"correct.gif">&                                          document.write(output);
var incorrect='<IMG                                           -->
SRC="incorect.gif">';                                         </SCRIPT>
var response=prompt(question,"0");                            </BODY>
                                                              </HTML>


12. CaĆ”u truĆ¹c ƱieĆ u kieƤn if ā€“ else
if ƱieƠu kieƤn
leƤnh ;

if ƱieƠu kieƤn {
MaƵ JavaScript
}
VĆ­ duĆÆ:                                                       If (day!=ā€Saturdayā€) {
if (day==ā€Saturdayā€) {                                        document.writeln(ā€œItā€˜s not Saturdayā€);
document.writeln(ā€œItā€˜s the weekendā€);                         }
alert(ā€œ Itā€™s the weekendā€);                                   Sƶƻ duĆÆng caĆ”u truĆ¹c else ā€“ if cho vĆ­ duĆÆ Ć“Ć» treĆ¢n
}                                                             If (day==ā€Saturdayā€) {
VĆ­ duĆÆ:                                                       document.writeln(ā€œItā€˜s the weekendā€);
If (day==ā€Saturdayā€) {                                        }
document.writeln(ā€œItā€˜s the weekendā€);                         else {
}                                                             document.writeln(ā€œItā€˜s not Saturdayā€);
                                                              }

CaĆ”u truĆ¹c keĆ”t hĆ“ĆÆp :
if ƱieƠu kieƤn 1 {
  CaĆ¹c leƤnh JavaScript



JAVASCRIPT                                 MEDIASPACE CLUB (HTD)                                         PAGE: 8
if ƱieƠu kieƤn 2 {
     CaĆ¹c leƤnh JavaScript
    } else {
               caĆ¹c leƤnh khaĆ¹c
             }
    CaĆ¹c leƤnh JavaScript
} else {
        CaĆ¹c leƤnh khaĆ¹c
}

VĆ­ duĆÆ 1 : Sƶƻ duĆÆng phƶƓng phaĆ¹p confirm() vĆ“Ć¹i phaĆ¹t bieĆ„u if
<HTML>                                                             response=prompt(question,"0");
<HEAD>                                                   }
<TITLE>Example 3.3</TITLE>
                                                         var output = (response ==answer ) ?
<SCRIPT LANGUAGE="Javascript">                           correct:incorrect ;
var question="What is 10+10 ?";
var answer=20;                                           </SCRIPT>
var correct='<IMG
SRC=/slideshow/tai-lieu-javascript/5089421/"correct.gif">&                                     </HEAD>
var incorrect='<IMG                                      <BODY>
SRC="incorect.gif">';                                    <SCRIPT LANGUAGE="Javascript">
                                                         <!--
var response=prompt(question,"0");                       document.write(output);
                                                         -->
if (response != answer) {                                </SCRIPT>
   if (confirm("Wrong ! press OK for                     </BODY>
a second change"))                                       </HTML>
VĆ­ duĆÆ 2 : Sƶƻ duĆÆng phƶƓng phaĆ¹p confirm() vĆ“Ć¹i phaĆ¹t bieĆ„u if - else
<HTML>                                                                   question="What is 10*10";
<HEAD>                                                                   answer=100;
<TITLE>Example 3.3</TITLE>
                                                         response=prompt(question,"0");
<SCRIPT LANGUAGE="Javascript">                                   }
var question="What is 10+10 ?";                          }
var answer=20;
var correct='<IMG                                        var output = (response ==answer ) ?
SRC=/slideshow/tai-lieu-javascript/5089421/"correct.gif">&                                     correct:incorrect ;
var incorrect='<IMG
SRC="incorect.gif">';                                    </SCRIPT>

var response=prompt(question,"0");                       </HEAD>
                                                         <BODY>
if (response != answer) {                                <SCRIPT LANGUAGE="Javascript">
   if (confirm("Wrong ! press OK for                     <!--
a second change"))                                       document.write(output);
        response=prompt(question,"0");                   -->
}else {                                                  </SCRIPT>
     if (confirm("Correct ! press OK                     </BODY>
for a second question"))                                 </HTML>
MTWRFSS
         {




JAVASCRIPT                              MEDIASPACE CLUB (HTD)                                PAGE: 9
BAƘI 3: HAƘM VAƘ ƑOƁI TƖƔƏNG

Trong kyƵ thuaƤt laƤp trƬnh caĆ¹c laƤp trƬnh vieĆ¢n thƶƓĆøng sƶƻ duĆÆng haĆøm ƱeĆ„ thƶĆÆc hieƤn moƤt ƱoaĆÆn chƶƓng
trƬnh theĆ„ hieƤn cho moƤt module naĆøo ƱoĆ¹ ƱeĆ„ thƶĆÆc hieƤn moƤt coĆ¢ng vieƤc naĆøo ƱoĆ¹.
Trong Javascript coĆ¹ caĆ¹c haĆøm ƱƶƓĆÆc xaĆ¢y dƶĆÆng saĆ¼n ƱeĆ„ giuĆ¹p baĆÆn thƶĆÆc hieƤn moƤt chĆ¶Ć¹c naĆŖng naĆøo ƱoĆ¹ vĆ­
duĆÆ nhƶ haĆøm alert(), document.write(), parseInt() vaĆø baĆÆn cuƵng coĆ¹ theĆ„ Ć±Ć²nh nghĆ³a ra caĆ¹c haĆøm khaĆ¹c
cuĆ»a mƬnh ƱeĆ„ thƶĆÆc hieƤn moƤt coĆ¢ng vieƤc naĆøo ƱoĆ¹ cuĆ»a baĆÆn, ƱeĆ„ Ć±Ć²nh nghĆ³a haĆøm baĆÆn theo cuĆ¹ phaĆ¹p sau:
       function function_name(parameters, arguments)
       {
                command block
       }


TruyeĆ n tham soĆ”:
function printName(name) {
document.write(ā€œ<HR>Your Name is <B><I>ā€);
document.write(name);
document.write(ā€œ</B></I><HR>ā€);
}
VĆ­ duĆÆ:
GoĆÆi haĆøm printName()vĆ“Ć¹i leƤnh sau printName(ā€œBobā€);
Khi haĆøm printName()ƱƶƓĆÆc thi haĆønh giaĆ¹ trĆ² cuĆ»a name laĆø "Bob" neĆ”u goĆÆi haĆøm
printName()vĆ“Ć¹i ƱoĆ”i soĆ” laĆø moƤt bieĆ”n
var user = ā€œJohnā€;
printName(user);
Khi ƱoĆ¹ name laĆø ā€œJohnā€. NeĆ”u baĆÆn muoĆ”n thay ƱoĆ„i giaĆ¹ trĆ² cuĆ»a name baĆÆn coĆ¹ theĆ„ laĆøm nhƶ sau : name
= ā€œMr. ā€œ + name;


PhaĆÆm vi cuĆ»a bieĆ”n:
BieĆ”n toaĆøn cuĆÆc (Global variable)
BieĆ”n cuĆÆc boƤ (Local variable)


TraĆ» veĆ  caĆ¹c giaĆ¹ trĆ²:
VĆ­ duĆÆ:
DuĆøng return ƱeĆ„ traĆ» veĆ  giaĆ¹ trĆ² cuĆ»a bieĆ”n cube.
MTWRFSS




function cube(number) {
var cube = number * number * number;
return cube;
}


JAVASCRIPT                              MEDIASPACE CLUB (HTD)                                    PAGE: 10
VĆ­ duĆÆ:
MTWRFSS




<HTML>                                                       //CHECK THE RESULT
<HEAD>                                                       return (response == answer) ? correct
<TITLE>Example 4.1</TITLE>                                   : incorrect;
<SCRIPT LANGUAGE=ā€JavaScriptā€>                               }
<!-- HIDE FROM OTHER BROWSERS                                // STOP HIDING FROM OTHER BROWSERS --
//DEFINE FUNCTION testQuestion()                             >
function testQuestion(question) {                            </SCRIPT>
//DEFINE LOCAL VARIABLES FOR THE                             </HEAD<
FUNCTION                                                     <BODY>
var answer=eval(question);                                   <SCRIPT LANGUAGE=ā€JavaScriptā€>
var output=ā€What is ā€œ + question +                           <!-- HIDE FROM OTHER BROWSERS
ā€œ?ā€;                                                         //ASK QUESTION AND OUTPUT RESULTS
var correct=ā€™<IMG                                            var result=testQuestion(ā€œ10 + 10ā€);
SRC=/slideshow/tai-lieu-javascript/5089421/ā€correct.gifā€>ā€™;                                         document.write(result);
var incorrect=ā€™<IMG                                          //STOP HIDING FROM OTHER BROWSERS -->
SRC=/slideshow/tai-lieu-javascript/5089421/ā€incorrect.gifā€>ā€™;                                       </SCRIPT>
//ASK THE QUESTION                                           </BODY>
var response=prompt(output,ā€0");                             </HTML>


HaĆøm eval duĆøng chuyeĆ„n ƱoĆ„i giaĆ¹ trĆ² chuoĆ„i soĆ” thaĆønh giaĆ¹ trĆ² soĆ”
eval(ā€œ10*10ā€)traĆ» veĆ  giaĆ¹ trĆ² laĆø 100

HaĆøm goĆÆi laĆÆi haĆøm:
VĆ­ duĆÆ:
<HTML>                                                       //CHECK THE RESULT
<HEAD>                                                       return (response == answer) ? correct
<TITLE>Example 4.2</TITLE>                                   : testQuestion(question);
<SCRIPT LANGUAGE=ā€JavaScriptā€>                               }
<!-- HIDE FROM OTHER BROWSERS                                // STOP HIDING FROM OTHER BROWSERS --
//DEFINE FUNCTION testQuestion()                             >
function testQuestion(question) {                            </SCRIPT>
//DEFINE LOCAL VARIABLES FOR THE                             </HEAD<
FUNCTION                                                     <BODY>
var answer=eval(question);                                   <SCRIPT LANGUAGE=ā€JavaScriptā€>
var output=ā€What is ā€œ + question +                           <!-- HIDE FROM OTHER BROWSERS
ā€œ?ā€;                                                         //ASK QUESTION AND OUTPUT RESULTS
var correct=ā€™<IMG                                            var result=testQuestion(ā€œ10 + 10ā€);
SRC=/slideshow/tai-lieu-javascript/5089421/ā€correct.gifā€>ā€™;                                         document.write(result);
var incorrect=ā€™<IMG                                          //STOP HIDING FROM OTHER BROWSERS -->
SRC=/slideshow/tai-lieu-javascript/5089421/ā€incorrect.gifā€>ā€™;                                       </SCRIPT>
//ASK THE QUESTION                                           </BODY>
var response=prompt(output,ā€0");                             </HTML>
VĆ­ duĆÆ 2:
<HTML>                                                       //DEFINE LOCAL VARIABLES FOR THE
<HEAD>                                                       FUNCTION
<TITLE>Example 4.2</TITLE>                                   var answer=eval(question);
<SCRIPT LANGUAGE=ā€JavaScriptā€>                               var output=ā€What is ā€œ + question +
<!-- HIDE FROM OTHER BROWSERS                                ā€œ?ā€;
//DEFINE FUNCTION testQuestion()                             var correct=ā€™<IMG
function                                                     SRC=/slideshow/tai-lieu-javascript/5089421/ā€correct.gifā€>ā€™;
testQuestion(question,chances) {



JAVASCRIPT                                 MEDIASPACE CLUB (HTD)                           PAGE: 11
var incorrect=ā€™<IMG                                        // STOP HIDING FROM OTHER BROWSERS --
SRC=/slideshow/tai-lieu-javascript/5089421/ā€incorrect.gifā€>ā€™;                                     >
4                                                          </SCRIPT>
//ASK THE QUESTION                                         </HEAD>
var response=prompt(output,ā€0");                           <BODY>
//CHECK THE RESULT                                         <SCRIPT LANGUAGE=ā€JavaScriptā€>
if (chances > 1) {                                         <!-- HIDE FROM OTHER BROWSERS
return (response == answer) ? correct                      //ASK QUESTION AND OUTPUT RESULTS
: testQuestion(question,chances-1);                        var result=testQuestion(ā€œ10 + 10ā€,3);
} else {                                                   document.write(result);
return (response == answer) ? correct                      //STOP HIDING FROM OTHER BROWSERS -->
: incorrect;                                               </SCRIPT>
}                                                          </BODY>
}                                                          </HTML>




                   BaĆøi 4: TAƏO ƑOƁI TƖƔƏNG TRONG JAVASCRIPT

1. Ć‘Ć²nh nghĆ³a thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng:

function student(name,age, grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
ƑeĆ„ taĆÆo moƤt Object ta sƶƻ duĆÆng phaĆ¹t bieĆ„u new.VĆ­ duĆÆ Ć±eĆ„ taĆÆo ƱoĆ”i tƶƓĆÆng student1
student1 = new student(ā€œBobā€,10,75);
3 thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng student1 laĆø :

student1.name
student1.age
student1.grade

VĆ­ duĆÆ Ć±eĆ„ taĆÆo ƱoĆ”i tƶƓĆÆng student2

student2 = new student(ā€œJaneā€,9,82);
ƑeĆ„ theĆ¢m thuoƤc tĆ­nh cho student1 baĆÆn coĆ¹ theĆ„ laĆøm nhƶ sau:
student1.mother = ā€œSusanā€; hoaĆ«c baĆÆn coĆ¹ theĆ„ Ć±Ć²nh nghĆ³a laĆÆi haĆøm student
MTWRFSS




4
function student(name, age, grade, mother) {
this.name = name;
this.age = age;
this.grade = grade;
this.mother = mother;
}



JAVASCRIPT                                MEDIASPACE CLUB (HTD)                          PAGE: 12
ƑoĆ”i tƶƓĆÆng laĆø thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng khaĆ¹c
VĆ­ duĆÆ:
function grade (math, english, science) {
this.math = math;
this.english = english;
this.science = science;
}
bobGrade = new grade(75,80,77);
janeGrade = new grade(82,88,75);

student1 = new student(ā€œBobā€,10,bobGrade);
student2 = new student(ā€œJaneā€,9,janeGrade);


student1.grade.math:duĆøng ƱeĆ„ laĆ”y ƱieĆ„m ToaĆ¹n cuĆ»a student1
student2.grade.science: duĆøng laĆ”y ƱieĆ„m moĆ¢n Khoa hoĆÆc cuĆ»a student2


2. TheĆ¢m phƶƓng phaĆ¹p cho ƱoĆ”i tƶƓĆÆng:
function displayProfile() {
document.write(ā€œName: ā€œ + this.name + ā€œ<BR>ā€);
document.write(ā€œAge: ā€œ + this.age + ā€œ<BR>ā€);
document.write(ā€œMotherā€™s Name: ā€œ + this.mother + ā€œ<BR>ā€);
document.write(ā€œMath Grade: ā€œ + this.grade.math + ā€œ<BR>ā€);
document.write(ā€œEnglish Grade: ā€œ + this.grade.english + ā€œ<BR>ā€);
document.write(ā€œScience Grade: ā€œ + this.grade.science + ā€œ<BR>ā€);
}


function student(name,age, grade) {
this.name = name;
this.age = age;
this.grade = grade;
this.mother = mother;
this.displayProfile = displayProfile;
}

student1.displayProfile();
VĆ­ du:
4
<HTML>                                          function displayInfo() {
<HEAD>                                          document.write(ā€œ<H1>Employee Profile:
<TITLE>Example 4.3</TITLE>                      ā€œ + this.name + ā€œ</H1><HR><PRE>ā€);
<SCRIPT LANGUAGE=ā€JavaScriptā€>                  document.writeln(ā€œEmployee Number: ā€œ
<!-- HIDE FROM OTHER BROWSERS                   + this.number);
//DEFINE METHOD


JAVASCRIPT                       MEDIASPACE CLUB (HTD)                        PAGE: 13
document.writeln(ā€œSocial Security            // STOP HIDING FROM OTHER BROWSERS --
Number: ā€œ + this.socsec);                    >
document.writeln(ā€œAnnual Salary: ā€œ +         </SCRIPT>
this.salary);                                </HEAD>
document.write(ā€œ</PRE>ā€);                    <BODY>
}                                            <SCRIPT LANGUAGE=ā€JavaScriptā€>
//DEFINE OBJECT                              <!-- HIDE FROM OTHER BROWSERS
function employee() {                        newEmployee.displayInfo();
this.name=prompt(ā€œEnter Employeeā€™s           // STOP HIDING FROM OTHER BROWSERS --
Nameā€,ā€Nameā€);                               >
this.number=prompt(ā€œEnter Employee           </SCRIPT>
Number for ā€œ + this.name,ā€000-000");         </BODY>
this.socsec=prompt(ā€œEnter Social             </HTML>
Security Number for ā€œ +
this.name,ā€000-00-0000");
this.salary=prompt(ā€œEnter Annual
Salary for ā€œ + this.name,ā€$00,000");
this.displayInfo=displayInfo;
}
newEmployee=new employee();




Vi du:
<script LANGUAGE="JavaScript">           myhours = mydate.getHours();
<!-- Begin                               ampmhour = (myhours > 12) ? myhours -
var day="";                              12 : myhours;
var month="";                            ampm = (myhours >= 12) ? 'BuoƄ i ChieƠ u '
var ampm="";                             : ' BuoĆ„ i SaĆ¹ ng ';
var ampmhour="";                         mytime = mydate.getMinutes();
var myweekday="";                        myminutes = ((mytime < 10) ? ':0' :
var year="";                             ':') + mytime;
mydate = new Date();                     if(myday == 0)
myday = mydate.getDay();                 day = " Chuƻ NhaƤ t , ";
mymonth = mydate.getMonth();             else if(myday == 1)
myweekday= mydate.getDate();             day = " ThĆ¶Ć¹ hai, ";
weekday= myweekday;                      else if(myday == 2)
myyear= mydate.getYear();                day = " ThĆ¶Ć¹ ba, ";
year = myyear;                           else if(myday == 3)


JAVASCRIPT                    MEDIASPACE CLUB (HTD)                        PAGE: 14
day = " ThĆ¶Ć¹ tƶ, ";                                     else if(mymonth ==5)
else if(myday == 4)                                     month = "thaĆ¹ ng saĆ¹ u ";
day = " ThĆ¶Ć¹ naĆŖ m, ";                                  else if(mymonth ==6)
else if(myday == 5)                                     month = "thaĆ¹ ng baĆ» y ";
day = " ThĆ¶Ć¹ saĆ¹ u , ";                                 else if(mymonth ==7)
else if(myday == 6)                                     month = "thaĆ¹ ng taĆ¹ m ";
day = " ThĆ¶Ć¹ baĆ» y , ";                                 else if(mymonth ==8)
if(mymonth == 0) {                                      month = "thaĆ¹ ng chĆ­n ";
month = "thaĆ¹ ng moƤ t ";}                              else if(mymonth ==9)
else if(mymonth ==1)                                    month = "thaĆ¹ ng mƶƓĆø i ";
month = "thaĆ¹ ng hai ";                                 else if(mymonth ==10)
else if(mymonth ==2)                                    month = "thaĆ¹ ng mƶƓĆø i moƤ t ";
month = "thaĆ¹ ng ba ";                                  else if(mymonth ==11)
else if(mymonth ==3)                                    month = "thaĆ¹ ng mƶƓĆø i hai ";
month = "thaĆ¹ ng tƶ ";                                  // End -->
else if(mymonth ==4)                                    </script>
month = "thaĆ¹ ng naĆŖ m, ";


Trong phaĆ n body baĆÆn coĆ¹ theĆ„ xuaĆ”t ra daĆÆng nhƶ sau:




<body>

<script>
document.write("<b><font color=#0000ff face='VNI-
Times,helvetica,arial'>" + ampmhour + "" + myminutes + ampm)
document.write(" - " + day + " ngaĆø y " + myweekday +" ");
document.write( month + " , naĆŖ m " + year + "</font>");
</script>
</body>


                          BaĆøi 5: SƖƏ KIEƄN TRONG JAVASCRIPT
CaĆ¹c sƶĆÆ kieƤn cung caĆ”p caĆ¹c tƶƓng taĆ¹c vĆ“Ć¹i cƶƻa soĆ„ trƬnh duyeƤt vaĆø taĆøi lieƤu hieƤn haĆønh Ʊang ƱƶƓĆÆc load
trong trang web, caĆ¹c haĆønh ƱoƤng cuĆ»a user khi nhaƤp dƶƵ lieƤu vaĆøo form vaĆø khi click vaĆøo caĆ¹c button
trong form.
Khi sƶƻ duĆÆng boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn baĆÆn coĆ¹ theĆ„ vieĆ”t caĆ¹c haĆøm ƱeĆ„ bieĆ„u dieĆ£n cho caĆ¹c haĆønh ƱoƤng dƶĆÆa vaĆøo
caĆ¹c sƶĆÆ kieƤn ƱƶĆÆoc choĆÆn
BaĆ»ng sƶĆÆ kieƤn trong Javascript

JAVASCRIPT                                MEDIASPACE CLUB (HTD)                                        PAGE: 15
TeĆ¢n sƶĆÆ kieƤn                                     MoĆ¢ taĆ»
               blur                XaƵy ra khi ƱieĆ„m taƤp trungcuĆ»a ngoƵ vaĆøo ƱƶƓĆÆc di chuyeĆ„n ra
                                   khoĆ»i moƤt thaĆønh phaĆ n cuĆ»a Form (Khi user click ra ngoaĆøi moƤt
                                   trƶƓĆøng)
               click               Khi user Click vaĆøo 1 link hoaĆ«c thaĆønh phaĆ n cuĆ»a Form.
               change              XaƵy ra khi giaĆ¹ trĆ² cuĆ»a Form Field bĆ² thay ƱoĆ„i bĆ“Ć»i user.
               focus               XaƵy ra khi ngoƵ vaĆøo taƤp trung vaĆøo thaĆønh phaĆ n cuĆ»a Form
               load                XaƵy ra khi moƤt trang ƱƶƓĆÆc Load vaĆøo trong boƤ duyeƤt.
               mouseover           XaƵy ra khi User di chuyeƄn mouse qua moƤt Hyperlink.
               select              XaƵy ra khi User choĆÆn 1 trƶƓĆøng cuĆ»a thaĆønh phaĆ n Form.
               submit              XaƵy ra khi User xaĆ¹c nhaƤn ƱaƵ nhaƤp xong dƶƵ lieƤu.
               unload              XaƵy ra khi User rĆ“Ćøi khoĆ»i trang Web.


BoƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn (Event Handler)
ƑeĆ„ quaĆ»n lyĆ¹ caĆ¹c sƶĆÆ kieƤn trong javascript ta duĆøng caĆ¹c boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn.
CuĆ¹ phaĆ¹p cuĆ»a moƤt boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn:

<HTML_TAG OTHER_ATTRIBUTES eventHandler=ā€JavaScript Programā€>
VĆ­ duĆÆ:
<INPUT TYPE=ā€textā€ onChange=ā€checkField(this)ā€>
VĆ­ duĆÆ:
<INPUT TYPE=ā€textā€ onChange=ā€
if (parseInt(this.value) <= 5) {
alert(ā€˜Please enter a number greater than 5.ā€™);
}
ā€œ>
VĆ­ duĆÆ:
<INPUT TYPE=ā€textā€ onChange=ā€
alert(ā€˜Thanks for the entry.ā€™);
confirm(ā€˜Do you want to continue?ā€™);
ā€œ>

TƶĆø khoĆ¹a this: quy cho ƱoĆ”i tƶƓĆÆng hieƤn haĆønh.Trong Javascript Form laĆø moƤ ƱoĆ”i tƶƓĆÆng.CaĆ¹c thaĆønh
phaĆ n cuĆ»a Form bao goĆ m text fields, checkboxes, radio buttons, buttons, vaĆø selection lists.
VĆ­ duĆÆ:
<INPUT TYPE=ā€textā€ onChange=ā€checkField(this)ā€>

CaĆ¹c boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn trong Javascript

                ƑoĆ”i tƶƓĆÆng                BoƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn tƶƓng Ć¶Ć¹ng.


JAVASCRIPT                                 MEDIASPACE CLUB (HTD)                                  PAGE: 16
Selection list     onBlur, onChange, onFocus
             Text element       onBlur, onChange, onFocus, onSelect
             Textarea element   onBlur, onChange, onFocus, onSelect
             Button element     OnClick
             Checkbox           onClick
             Radio button       OnClick
             Hypertext link     onClick, onMouseOver
             Reset button       OnClick
             Submit button      OnClick
             Document           onLoad, onUnload
             Window             onLoad, onUnload
             Form               onSubmit


5
CaĆ¹ch duĆøng boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn onLoad & onUnload
<HTML>
<HEAD>
<TITLE>Example 5.1</TITLE>
</HEAD>
<BODY onLoad=ā€alert(ā€˜Welcome to my page!ā€™);ā€
onUnload=ā€alert(ā€˜Goodbye! Sorry to see you go!ā€™);ā€>
<IMG SRC=/slideshow/tai-lieu-javascript/5089421/ā€title.gifā€>
</BODY>
</HTML>
MTWRFSS




Vi du:
<HTML>
<HEAD>
<TITLE>Example 5.1</TITLE>
<SCRIPT LANGUAGE=ā€JavaScriptā€>
<!-- HIDE FROM OTHER BROWSERS
var name = ā€œā€;
// STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>
</HEAD>
<BODY onLoad=ā€
name = prompt(ā€˜Enter Your Name:ā€™,ā€™Nameā€™);
alert(ā€˜Greetings ā€˜ + name + ā€˜, welcome to my page!ā€™);ā€
onUnload=ā€ alert(Goodbye ā€˜ + name + ā€˜, sorry to see you go!ā€™);ā€>
<IMG SRC=/slideshow/tai-lieu-javascript/5089421/ā€title.gifā€>
</BODY>
</HTML>


Vi du
MTWRFSS
<HTML>
<HEAD>



JAVASCRIPT                      MEDIASPACE CLUB (HTD)                 PAGE: 17
<TITLE>Example 5.1</TITLE>
<SCRIPT LANGUAGE=ā€JavaScriptā€>
<!-- HIDE FROM OTHER BROWSERS
// DEFINE GLOBAL VARIABLE
var name = ā€œā€;
function hello() {
name = prompt(ā€˜Enter Your Name:ā€™,ā€™Nameā€™);
alert(ā€˜Greetings ā€˜ + name + ā€˜, welcome to my page!ā€™);
}
function goodbye() {
alert(Goodbye ā€˜ + name + ā€˜, sorry to see you go!ā€™);
}
// STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>
</HEAD>
<BODY onLoad=ā€hello();ā€ onUnload=ā€goodbye();ā€>
<IMG SRC=/slideshow/tai-lieu-javascript/5089421/ā€title.gifā€>
</BODY>
</HTML>


CaĆ¹c sƶĆÆ kieƤn vaĆø Form
Cac sƶĆÆ kieƤn ƱƶƓĆÆc sƶƻ duĆÆng ƱeĆ„ truy xuaĆ”t Form nhƶ:
OnClick, onSubmit, onFocus, onBlur, vaĆø onChange.

VĆ­ duĆÆ:

<INPUT TYPE=text NAME=ā€testā€ VALUE=ā€testā€
onBlur=ā€alert(ā€˜Thank You!ā€™);ā€
onChange=ā€check(this);ā€>
Khi giaĆ¹ trĆ² thay ƱoĆ„i function check() seƵ ƱƶƓĆÆc goĆÆi. Ta duĆøng tƶĆø khoĆ¹a this ƱeĆ„ chuyeĆ„n ƱoĆ”i tƶƓĆÆng cuĆ»a
trƶƓĆøng hieƤn haĆønh ƱeĆ”n haĆøm check()
BaĆÆn cuƵng coĆ¹ theĆ„ dƶĆÆa vaĆøo caĆ¹c phƶƓng phaĆ¹p vaĆø caĆ¹c thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng baĆØng phaĆ¹t bieĆ„u sau:
this.methodName() & this.propertyName.

VĆ­ duĆÆ:

<HTML>                                                     expressionā€,ā€ā€);
<HEAD>                                                     calculate(form);
<TITLE>Example 5.3</TITLE>                                 }
                                                           //STOP HIDING FROM OTHER BROWSERS -->
<SCRIPT LANGUAGE=ā€JavaScriptā€>                             </SCRIPT>
<!-- HIDE FROM OTHER BROWSERS                              </HEAD>
function calculate(form) {                                 <BODY>
form.results.value =                                       <FORM METHOD=POST>
eval(form.entry.value);                                    Enter a JavaScript mathematical
}                                                          expression:
function getExpression(form) {                             <INPUT TYPE=text NAME=ā€entryā€
form.entry.blur();                                         VALUE=ā€ā€
form.entry.value = prompt(ā€œPlease                          onFocus=ā€getExpression(this.form);ā€>
enter a JavaScript mathematical                            <BR>



JAVASCRIPT                               MEDIASPACE CLUB (HTD)                                      PAGE: 18
The result of this expression is:                     </FORM>
<INPUT TYPE=text NAME=ā€resultsā€                       </BODY>
VALUE=ā€ā€                                              </HTML>
onFocus=ā€this.blur();ā€>




MTWRFSS




formObjectName.fieldname:DuĆøng ƱeĆ„ chƦ teĆ¢n trƶƓĆøng cuĆ»a hieƤn haĆønh trong Form.
formObjectName.fieldname.value: duĆøng laĆ”y giaĆ¹ trĆ² cuĆ»a trƶƓĆøng form hieƤn haĆønh.
                     Sƶƻ duĆÆng voĆøng laĆ«p trong JavaScript

1 . VoĆøng laĆ«p for :
CuĆ¹ phaĆ¹p :
for ( init value ; condition ; update expression )
VĆ­ duĆÆ :
for (i = 0 ; i < 5 ; i++)
 {
    leƤnh ;
 }

VĆ­ duĆÆ:
<HTML>
<HEAD>
<TITLE> for loop Examle </TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!- -
var name=prompt("What is your name?" ,"name");
var query= " " ;
document.write("<H1>" + name + " 's 10 favorite foods </H1> ");
for (var i=1 ;i<=10;i++)
   {



JAVASCRIPT                             MEDIASPACE CLUB (HTD)                         PAGE: 19
document.write(i + " " + prompt('Enter food number ' + i, 'food' ) +
'<BR>');
   }
- ->
</SCRIPT>
</BODY>
</HTML>
2 . VoĆøng laĆ«p while :
CuĆ¹ phaĆ¹p:
While ( ƱieƠu kieƤn)
   {
      leƤnh JavaScript ;
   }
VĆ­ duĆÆ:
var num=1;
while(num<=10)
  {
 document.writeln(num);
num++;
}
VĆ­ duĆÆ:
var answer=ā€ ā€œ ;
var correc=100;
var question=ā€ what is 10*10 ?ā€ ;
while(answer!=correct)
  {
     answer=prompt(question,ā€0ā€);
  }

3. TaĆÆo maĆ»ng vĆ“Ć¹i voĆøng laĆ«p for:
function createArray(num)
   {
         this.length=num;
         for ( var j=0 ; j<num; j++)
         this[j]=0;
   }
HaĆøm seƵ taĆÆo moƤt maĆ»ng coĆ¹ giaĆ¹ trĆ² index baĆ©t ƱaĆ u laĆø 0 vaĆø gaĆ¹n taĆ”t caĆ» caĆ¹c giaĆ¹ trĆ² cuĆ»a maĆ»ng veĆ  0 .
ƑeĆ„ sƶƻ duĆÆng ƱoĆ”i tƶƓĆÆng maĆ»ng ta coĆ¹ theĆ„ laĆøm nhƶ sau:
newArray= new createArray(4)
SeƵ taĆÆo ra moƤt maĆ»ng goĆ m 4 thaĆønh phaĆ n newArray[0] ā€¦ NewArray[3]




JAVASCRIPT                                  MEDIASPACE CLUB (HTD)                                         PAGE: 20
Sƶƻ duĆÆng ƱoĆ”i tƶƓĆÆng Windows
Window laĆø ƱoĆ”i tƶƓĆÆng cuĆ»a moĆ¢i trƶƓĆøng Navigator,ngoaĆøi caĆ¹c thuoƤc tĆ­nh Window ƱoĆ”i tƶƓĆÆng window
coĆøn giƶƵ caĆ¹c ƱoĆ”i tƶƓĆÆng khaĆ¹c maĆø coĆ¹ theĆ„ ƱƶƓĆÆc xem nhƶ laĆø caĆ¹c thaĆønh phaĆ n (member) cuĆ»a window,
caĆ¹c ƱoĆ”i tƶƓĆÆng ƱoĆ¹ laĆø:
ā€¢ CaĆ¹c frame ƱaƵ ƱƶƓĆÆc taĆÆo
ā€¢ CaĆ¹c ƱoĆ”i tƶƓĆÆng location vaĆø histtory
ā€¢ ƑoĆ”i tƶƓĆÆng document
ƑoĆ”i tƶƓĆÆng document chĆ¶Ć¹a (encompasses) taĆ”t caĆ» caĆ¹c thaĆønh phaĆ n trong trang HTML.ƑaĆ¢y laĆø moƤt ƱoĆ”i
tƶƓĆÆng hoaĆøn haĆ»o coĆ¹ caĆ¹c ƱoĆ”i tƶƓĆÆng khaĆ¹c cuĆ»a JavaScript gaĆ¹n (attached) vaĆøo noĆ¹ (nhƶ laĆø
anchor,form,history,link).HaĆ u nhƶ moĆÆi chƶƓng trƬnh JavaScript ƱeĆ u coĆ¹ sƶƻ duĆÆng ƱoĆ”i tƶƓĆÆng naĆøy ƱeĆ„
tham khaĆ»o ƱeĆ”n caĆ¹c thaĆønh phaĆ n trong trang HTML.
1) CaĆ¹c thuoƤc tĆ­nh (properties) cuĆ»a ƱoĆ”i tƶƓĆÆng document
a . alink
b . anchor
c . bgColor
d . cookies
e . fgColor
f . form
g . lastModified
h . linkColor
i . links
j . location
k . referrer
l . title
m . vlinkColor
2) CaĆ¹c haĆønh vi (Methods) cuĆ»a ƱoĆ”i tƶƓĆÆng document
a . clear()
b . close()
c . open()
d . write()
e . writeln()
3) CaĆ¹c thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng Window
a . defaultStatus : GiaĆ¹ trĆ² maĆ«t nhieĆ¢n ƱƶƓĆÆc hieĆ„n thĆ² Ć“Ć» thanh traĆÆng thaĆ¹i
b . frames : MaĆ»ng caĆ¹c ƱoĆ”i tƶƓĆÆng chĆ¶Ć¹a ƱƶĆÆng moƤt muĆÆc cho moĆ£i frame con trong moƤt frame taĆøi lieƤu
c . parent : ƑƶƓĆÆc sƶƻ duĆÆng trong FRAMSET
d . self : Cƶƻa soĆ„ hieƤn haĆønh , duĆøng ƱeĆ„ phaĆ¢n bieƤt giƶƵa caĆ¹c cƶƻa soĆ„ hieƤn haĆønh vaĆø caĆ¹c forms coĆ¹ cuĆøng
    teĆ¢n .
e . status : GiaĆ¹ trĆ² cuĆ»a chuoĆ£i vaĆŖn baĆ»n ƱƶƓĆÆc hieĆ„n thĆ² taĆÆi thanh status bar.DuĆøng ƱeĆ„ hieĆ„n thi caĆ¹c thoĆ¢ng
    baĆ¹o cho ngƶƓĆøi sƶƻ duĆÆng .
f . top : ƑƦnh cao nhaĆ”t cuĆ»a cƶƻa soĆ„ cha


JAVASCRIPT                                MEDIASPACE CLUB (HTD)                                       PAGE: 21
g.  window
4)  CaĆ¹c haĆønh vi (Methods) cuĆ»a ƱoĆ”i tƶƓĆÆng window
a.  alert() : HieƤn 1 thoĆ¢ng baĆ¹o trong hoƤp thoaĆÆi vĆ“Ć¹i OK button.
b.  close() : ƑoĆ¹ng cƶƻa soĆ„ hieƤn haĆønh.
c.  open() : MĆ“Ć» moƤt cƶƻa soĆ„ mĆ“Ć¹i vĆ“Ć¹i 1 taĆøi lieƤu ƱƶƓĆÆc chƦ ra hoaĆ«c mĆ“Ć» moƤt taĆøi lieƤu trong moƤt teĆ¢n cƶƻa
    soĆ„ ƱƶƓĆÆc chƦ Ć±Ć²nh.
d . prompt() : HieƤn moƤt hoƤp thoĆ¢ng baĆ¹o
e . setTimeout() :
f . clearTimeout() :
    HaĆønh vi naĆøy cung caĆ”p caĆ¹ch goĆÆi phaĆ¹t bieĆ„u JavaScript sau moƤt khoaĆ»ng thĆ“Ćøi gian troĆ¢i qua .NgoaĆøi
    ra ƱoĆ”i tƶƓĆÆng window coĆ¹ theĆ„ thƶĆÆc hieƤn event handler : onLoad=statement

LaĆøm vieƤc vĆ“Ć¹i status bar
Khi user di chuyeĆ„n qua moƤt hyperlink ta coĆ¹ theĆ„ hieƤn ra moƤt thoĆ¢ng baĆ¹o taĆÆi thanh status bar cuĆ»a
bowser dƶĆÆa vaĆøo event handler onMouseOver vaĆø baĆØng caĆ¹ch ƱaĆ«t self.status laĆø moƤt chuoĆ„i (hoaĆ«c
window.status).
VĆ­ duĆÆ:
<HTML>
<HEAD>
<TITLE>Status Example</TITLE>

<BODY>
<A href=ā€plc.htmā€ onMouseOver=ā€self.status=ā€™Chuyen de PLCā€™ ;return true ; ā€œ >Lop chuyen deĆ  PLC </A>
<A href=ā€tkweb.htmā€ onMouseOver=ā€self.status=ā€™Thiet Ke Trang Webā€™ ;return true ; ā€œ >Thiet Ke Web</A>
</BODY>
</HTML>


MĆ“Ć» vaĆø ƱoĆ¹ng caĆ¹c cƶƻa soĆ„
Sƶƻ duĆÆng phƶƓng phaĆ¹p open() vaĆø close() ta coĆ¹ theĆ„ ƱieĆ u khieĆ„n vieƤc mĆ“Ć» vaĆø ƱoĆ¹ng cƶƻa soĆ„ chĆ¶Ć¹a taĆøi
lieƤu.
open (ā€œURLā€ , ā€œWindowNameā€ , ā€œfeatureListā€) ;
CaĆ¹c ƱaĆ«c ƱieĆ„m trong phƶƓng phaĆ¹p open() goĆ m coĆ¹:
ā€¢ toolbar : taĆÆo moƤt toolbar chuaĆ„n
ā€¢ location: taĆÆo moƤt vuĆøng location
ā€¢ directories: taĆÆo caĆ¹c button thƶ muĆÆc chuaĆ„n
ā€¢ status: taĆÆo thanh traĆÆng thaĆ¹i.
ā€¢ menubar : taĆÆo thanh menu taĆÆi ƱƦnh cuĆ»a cƶƻa soĆ„
ā€¢ scrollbars: taĆÆo thanh scroll bar
ā€¢ resizable: cho pheĆ¹p user thay ƱoĆ„i kĆ­ch thĆ¶Ć“Ć¹c cƶƻa soĆ„
ā€¢ width : chƦ Ć±Ć²nh chieĆ u roƤng cƶƻa soĆ„ theo ƱƓn vĆ² pixel
ā€¢ height : chƦ Ć±Ć²nh chieĆ u cao cƶƻa soĆ„ theo ƱƓn vĆ² pixel
VĆ­ duĆÆ:
window.open( ā€œplc.htmā€,ā€newWindowā€,ā€toolbar=yes,location=1,directories=yes,status=yes,


JAVASCRIPT                                  MEDIASPACE CLUB (HTD)                                      PAGE: 22
menubar=1,scroolbar=yes,resizable=0,copyhistory=1,width=200,height=200ā€);
VĆ­ duĆÆ:
<HTML>
<HEAD>
<TITLE>WINDOWS</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function openWindow(url,name) {
popupWin = window.open(url, name, "scrollbars=yes,width=800, heigth=200 ");
}
-->
</SCRIPT>
</HEAD>
<BODY>
<a href="javascript:openWindow('../chuyende/plc.htm','Win')">PLC</a>,
<a href="javascript:openWindow('../chuyende/suachuaw.htm','stoogeWin')">Sua chua</a>,
<a href="javascript:openWindow('../chuyende/tkweb.htm','stoogeWin')">Thiet ke web</a>
</BODY>
</HTML>

ƑeĆ„ ƱoĆ¹ng cƶƻa soĆ„ ta coĆ¹ theĆ„ duĆøng phƶƓng phaĆ¹p close()
VĆ­ duĆÆ:
<HTML>
<HEAD>
<TITLE>Close Example</TITLE>
</HEAD>
<BODY >
<A href="#" ONCLICK="self.close();return false"><IMG ALIGN="middle"
SRC=/slideshow/tai-lieu-javascript/5089421/"../demo.gif" WIDTH="16" HEIGHT="16" BORDER="0"></A>
 <A href="#" ONCLICK="self.close();return false">Close This Sample</A>
</BODY >
</HTML>


Sƶƻ duĆÆng ƱoĆ”i tƶƓĆÆng string
String laĆø moƤt ƱoĆ”i tƶƓĆÆng cuĆ»a JavaScript,khi duĆøng ƱoĆ”i tƶƓĆÆng string chuĆ¹ng ta khoĆ¢ng caĆ n caĆ¹c phaĆ¹t
bieĆ„u ƱeĆ„ taĆÆo moƤt instance (theĆ„ nghieƤm) cuĆ»a ƱoĆ”i tƶƓĆÆng ,baĆ”t kyĆø luĆ¹c naĆøo ta ƱaĆ«t text giƶƵa hai daĆ”u
ngoaĆ«c keĆ¹p vaĆø gaĆ¹n noĆ¹ ƱeĆ”n moƤt bieĆ”n hoaĆ«c moƤt thuoƤc tĆ­nh thƬ ta ƱaƵ taĆÆo moƤt ƱoĆ”i tƶƓĆÆng string.
1. CaĆ¹c thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng string
ThuoƤc tĆ­nh length giƶƵ soĆ” kĆ­ tƶĆÆ cuĆ»a string.
2. CaĆ¹c haĆønh vi (Methods) cuĆ»a ƱoĆ”i tƶƓĆÆng string
a . Anchor (nameAttribute)
b . big()
c . blink()
d . bold()
e . charAt(index)
f . fixed()
g . fontcolor(color)


JAVASCRIPT                               MEDIASPACE CLUB (HTD)                                   PAGE: 23
h . fontsize(size)
i . indexOf(character,[fromIndex])
j . italics()
k . lastIndexOf(character,[fromIndex])
l . link(URL)
m . small()
n . strike()
o . sub()
p . substring(startIndex,endIndex)
q . sup()
r . toLowerCase()
s . toUpperCase()
                                         ----------------------------
                                                                        BieĆ¢n soaĆÆn: PhaĆÆm phuĆ¹ TaĆøi
                                                                                 Huu Thanh Design




JAVASCRIPT                         MEDIASPACE CLUB (HTD)                                  PAGE: 24

More Related Content

Tai Lieu Javascript

  • 1. JAVASCRIPT BaĆøi 1: TOƅNG QUAN VEƀ JAVASCRIPT. 1. ƑaĆ«c tĆ­nh cuĆ»a ngoĆ¢n ngƶƵ Javascript: J avascript laĆø moƤt ngoĆ¢n ngƶƵ thoĆ¢ng dĆ²ch (interpreter), chƶƓng trƬnh nguoĆ n cuĆ»a noĆ¹ ƱƶƓĆÆc nhuĆ¹ng (embedded) hoaĆ«c tĆ­ch hĆ“ĆÆp (integated) vaĆøo taƤp tin HTML chuaĆ„n. Khi file ƱƶƓĆÆc load trong Browser (coĆ¹ support cho JavaScript), Browser seƵ thoĆ¢ng dĆ²ch caĆ¹c Script vaĆø thƶĆÆc hieƤn caĆ¹c coĆ¢ng vieƤc xaĆ¹c Ć±Ć²nh. ChƶƓng trƬnh nguoĆ n JavaScript ƱƶƓĆÆc thoĆ¢ng dĆ²ch trong trang HTML sau khi toaĆøn boƤ trang ƱƶƓĆÆc load nhƶng trĆ¶Ć“Ć¹c khi trang ƱƶƓĆÆc hieĆ„n thĆ². Javascript laĆø moƤt ngoĆ¢n ngƶƵ coĆ¹ ƱaĆ«c tĆ­nh: ā€¢ Ć‘Ć“n giaĆ»n. ā€¢ ƑoƤng (Dynamic). ā€¢ HĆ¶Ć“Ć¹ng ƱoĆ”i tƶƓĆÆng (Object Oriented). 2. NgoĆ¢n ngƶƵ JavaScript: MoƤt trong nhƶƵng ƱaĆ«c tĆ­nh quan troĆÆng cuĆ»a ngoĆ¢n ngƶƵ JavaScript laĆø khaĆ» naĆŖng taĆÆo vaĆø sƶƻ duĆÆng caĆ¹c ƱoĆ”i tƶƓĆÆng (Object). CaĆ¹c Object naĆøy cho pheĆ¹p ngƶƓĆøi laƤp trƬnh sƶƻ duĆÆng ƱeĆ„ phaĆ¹t trieĆ„n Ć¶Ć¹ng duĆÆng. Trong JavaScript ,caĆ¹c Object ƱƶƓĆÆc nhƬn theo 2 khĆ­a caĆÆnh: a. CaĆ¹c Object ƱaƵ toĆ n taĆÆi. b. CaĆ¹c Object do ngƶƓĆøi laƤp trƬnh xaĆ¢y dƶĆÆng. Trong caĆ¹c Object ƱaƵ toĆ n taĆÆi ƱƶƓĆÆc chia thaĆønh 2 kieĆ„u: a. CaĆ¹c Object cuĆ»a JavaScript (JavaScript Built-in Object). b. CaĆ¹c ƱoĆ”i tƶƓĆÆng ƱƶƓĆÆc cung caĆ”p bĆ“Ć»i moĆ¢i trƶƓĆøng Netscape. 3. Built-in Object trong JavaScript: JavaScript cung caĆ”p 1 boƤ caĆ¹c Built-in Object ƱeĆ„ cung caĆ”p caĆ¹c thoĆ¢ng tin veĆ  sƶĆÆ hieƤn haĆønh cuĆ»a caĆ¹c ƱoĆ”i tƶƓĆÆng ƱƶƓĆÆc load trong trang Web vaĆø noƤi dung cuĆ»a noĆ¹.CaĆ¹c ƱoĆ”i tƶƓĆÆng naĆøy bao goĆ m caĆ¹c phƶƓng phaĆ¹p (Method) laĆøm vieƤc vĆ“Ć¹i caĆ¹c thuoƤc tĆ­nh (Properties) cuĆ»a noĆ¹. 4. CaĆ¹c ƱoĆ”i tƶƓĆÆng ƱƶƓĆÆc cung caĆ”p bĆ“Ć»i moĆ¢i trƶƓĆøng Netscape: Netscape Navigator cung caĆ”p caĆ¹c ƱoĆ”i tƶƓĆÆng cho pheĆ¹p JavaScript tƶƓng taĆ¹c vĆ“Ć¹i file HTML, caĆ¹c ƱoĆ”i tƶƓĆÆng naĆøy cho pheĆ¹p chuĆ¹ng ta ƱieĆ u khieĆ„n vieƤc hieĆ„n thĆ² thoĆ¢ng tin vaĆø ƱaĆ¹p Ć¶Ć¹ng caĆ¹c sƶĆÆ kieƤn trong moĆ¢i trƶƓĆøng Navigator.VĆ­ duĆÆ Ć‘oĆ”i tƶƓĆÆng MoĆ¢ taĆ» Window Cung caĆ”p caĆ¹c phƶƓng phaĆ¹p vaĆø caĆ¹c tĆ­nh chaĆ”t cho cƶƻa soĆ„ hieƤn haĆønh cuĆ»a trƬnh duyeƤt,bao goĆ m caĆ¹c ƱoĆ”i tƶƓĆÆng cho moĆ£i frame. JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 1
  • 2. Location Cung caĆ”p caĆ¹c tĆ­nh chaĆ”t vaĆø phƶƓng phaĆ¹p laĆøm vieƤc vĆ“Ć¹i caĆ¹c Ć±Ć²a chƦ URL hieƤn haĆønh ƱƶƓĆÆc mĆ“Ć». History CaĆ¹c ƱoĆ”i tƶƓĆÆng history cung caĆ”p thoĆ¢ng tin veĆ  caĆ¹c danh saĆ¹ch cuƵ vaĆø coĆ¹ theĆ„ giĆ“Ć¹i haĆÆn sƶĆÆ tƶƓng taĆ¹c vĆ“Ć¹i danh saĆ¹ch. Document ƑaĆ¢y laĆø moƤt ƱoĆ”i tƶƓĆÆng ƱƶƓĆÆc sƶƻ duĆÆng nhieĆ u nhaĆ”t .NoĆ¹ chĆ¶Ć¹a ƱƶĆÆng caĆ¹c ƑoĆ”i tƶƓĆÆng,tĆ­nh chaĆ”t vaĆø caĆ¹c phƶƓng phaĆ¹p laĆøm vieƤc vĆ“Ć¹i caĆ¹c thaĆønh phaĆ n cuĆ»a taĆøi lieƤu nhƶ caĆ¹c :form,link,anchor,applet. 5. CaĆ¹c ƱoĆ”i tƶƓĆÆng do ngƶƓĆøi laƤp trƬnh xaĆ¢y dƶĆÆng: a. Ć‘Ć²nh nghĆ³a thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng: (Object Properties) CuĆ¹ phaĆ¹p : Object-name.Property-name (teĆ¢n ƱoĆ”i tƶƓĆÆng.teĆ¢n ƱaĆ«c tĆ­nh) VĆ­ duĆÆ :MoƤt ƱoĆ”i tƶƓĆÆng airplane coĆ¹ caĆ¹c thuoƤc tĆ­nh nhƶ sau: Airplane.model Airplane.maxspeed Airplane.price Airplane.fuel Airplane.seating b. TheĆ¢m caĆ¹c phƶƓng phaĆ¹p cho ƱoĆ”i tƶƓĆÆng:( Method to Object) Sau khi ƱaƵ coĆ¹ caĆ¹c thoĆ¢ng tin veĆ  airplane ta tieĆ”p tuĆÆc xaĆ¢y dƶĆÆng phƶƓng phaĆ¹p ƱeĆ„ sƶƻ duĆÆng thoĆ¢ng tin naĆøy.VĆ­ duĆÆ baĆÆn muoĆ”n in ra moĆ¢ taĆ» cuĆ»a airplane hoaĆ«c tĆ­nh toaĆ¹n khoaĆ»ng caĆ¹ch toĆ”i Ʊa cuĆ»a cuoƤc haĆønh trƬnh vĆ“Ć¹i nhieĆ¢n lieƤu ƱaƵ coĆ¹: Airplane.description() Airplane.distance() c. TaĆÆo moƤt instance cuĆ»a ƱoĆ”i tƶƓĆÆng: TrĆ¶Ć“Ć¹c khi thao taĆ¹c vĆ“Ć¹i moƤt ƱoĆ”i tƶƓĆÆng cuĆ»a JavaScript ta phaĆ»i taĆÆo moƤt instance cho ƱoĆ”i tƶƓĆÆng ƱoĆ¹. 6 . NhuĆ¹ng JavaScript vaĆøo trong taƤp tin HTML: CuĆ¹ phaĆ¹p: <SCRIPT LANGUAGE=ā€JavaScriptā€> JavaScript Program </SCRIPT> ThuoƤc tĆ­nh cuĆ»a theĆ» SCRIPT + SRC :Ć‘Ć²a chƦ URL chƦ ƱeĆ”n taƤp tin chƶƓng trƬnh JavaScript (*.js) + LANGUAGE: ChƦ Ć±Ć²nh ngoĆ¢n ngƶƵ ƱƶƓĆÆc sƶƻ duĆÆng trong Script vaĆø caĆ¹c phieĆ¢n baĆ»n sƶƻ duĆÆng (vĆ­ duĆÆ nhƶ :JavaScript ,JavaScript .1.2 vvā€¦ ,VBScript). 7. Aƅn caĆ¹c Scripts ƱoĆ”i vĆ“Ć¹i caĆ¹c Browser khoĆ¢ng cung caĆ”p JavaScript: <SCRIPT LANGUAGE=ā€JavaScriptā€> <!- - DoĆøng daĆ”u Script ƱoĆ”i vĆ“Ć¹i caĆ¹c Browser khoĆ¢ng cung caĆ”p (support) JavaScript Program //DoĆøng keĆ”t thuĆ¹c vieƤc daĆ”u Script vaĆø chuĆ¹ thĆ­ch - - > </SCRIPT> JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 2
  • 3. 8. Sƶƻ duĆÆng taƤp tin JavaScript beĆ¢n ngoaĆøi : <SCRIPT LANGUAGE=ā€JavaScriptā€ SRC=/slideshow/tai-lieu-javascript/5089421/ā€http:/www.hcmuns.edu.vn/scroll.jsā€> <!- - DoĆøng daĆ”u Script ƱoĆ”i vĆ“Ć¹i caĆ¹c Browser khoĆ¢ng cung caĆ”p (support) JavaScript Program //DoĆøng keĆ”t thuĆ¹c vieƤc daĆ”u Script vaĆø chuĆ¹ thĆ­ch - - > </SCRIPT> 9. TheĆ¢m chƶƓng trƬnh vaĆøo taƤp tin HTML: <HTML> <HEAD> <TITLE>Listing 2.1</TITLE> </HEAD> <BODY> Here is result: <SCRIPT LANGUAGE="Javascript"> <!-- document.writeln("It work<BR>"); --> </SCRIPT> </BODY> </HTML> BaĆøi 2: SƖƛ DUƏNG JAVASCRIPT 1. CuĆ¹ phaĆ¹p cĆ“ baĆ»n cuĆ»a leƤnh : JavaScript xaĆ¢y dƶĆÆng caĆ¹c haĆøm,caĆ¹c phaĆ¹t bieĆ„u,caĆ¹c toaĆ¹n tƶƻ vaĆø caĆ¹c bieĆ„u thĆ¶Ć¹c treĆ¢n cuĆøng moƤt doĆøng vaĆø keĆ”t thuĆ¹c baĆØng ; VĆ­ duĆÆ: document.writeln("It work<BR>"); 2. CaĆ¹c khoĆ”i leƤnh: NhieĆ u doĆøng leƤnh coĆ¹ theĆ„ ƱƶƓĆÆc lieĆ¢n keĆ”t vĆ“Ć¹i nhau vaĆø ƱƶƓĆÆc bao bĆ“Ć»i { } VĆ­ duĆÆ: { document.writeln("Does It work"); document.writeln("It work!"); } 3. XuaĆ”t dƶƵ lieƤu ra cƶƻa soĆ„ trƬnh duyeƤt: DuĆøng 2 phƶƓng phaĆ¹p document.write() vaĆø document.writeln() VĆ­ duĆÆ: document.write(ā€œTestā€); document.writeln(ā€œTestā€); 4. XuaĆ”t caĆ¹c theĆ» HTML tƶĆø JavaScript JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 3
  • 4. VĆ­ duĆÆ 1: <HTML> <SCRIPT LANGUAGE="Javascript"> <HEAD> <!- - <TITLE>Outputting Text</TITLE> document.write("This is text bold </B>"); </HEAD> - -> <BODY> </SCRIPT> This is text plain <BR> </BODY> <B> </HTML> VĆ­ duĆÆ 2: <HTML> document.write('<IMG <HEAD> SRC=/slideshow/tai-lieu-javascript/5089421/"welcome.gif">& <TITLE>Example 2.4 </TITLE> document.write("<BR><H1>WELCOME TO </HEAD> NETSCAPE 2.1</H1>"); <BODY> - -> <SCRIPT LANGUAGE="Javascript"> </SCRIPT> <!- - </BODY> </HTML> 5. Sƶƻ duĆÆng phƶƓng phaĆ¹p writeln() vĆ“Ć¹i theĆ» PRE: <HTML> document.writeln("Two,"); <HEAD> document.write("Three"); <TITLE>Outputting Text</TITLE> document.write("..."); </HEAD> - -> <BODY> </SCRIPT> <PRE> </PRE> <SCRIPT LANGUAGE="Javascript"> </BODY> <!- - </HTML> document.writeln("One,"); 6. CaĆ¹c kĆ­ tƶĆÆ Ć±aĆ«c bieƤt trong chuoĆ„i: n : New line t : Tab r : carriage return f : form feed b: backspace VĆ­ duĆÆ: document.writeln("It work!n"); 7. LaĆøm vieƤc vĆ“Ć¹i caĆ¹c dialog boxes Sƶƻ duĆÆng haĆøm alert() ƱeĆ„ hieĆ„n thĆ² thoĆ¢ng baĆ¹o trong moƤt hoƤp. VĆ­ duĆÆ: <HTML> alert("Welcome to Netscape Navigator <HEAD> 21"); <TITLE>Example 2.5 </TITLE> document.write('<IMG </HEAD> SRC=/slideshow/tai-lieu-javascript/5089421/"welcome.gif">& <BODY> - -> <SCRIPT LANGUAGE="Javascript"> </SCRIPT> <!- - </BODY> JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 4
  • 5. 8. TƶƓng taĆ¹c vĆ“Ć¹i ngƶƓĆøi sƶƻ duĆÆng: Sƶƻ duĆÆng phƶƓng phaĆ¹p promt() ƱeĆ„ tƶƓng taĆ¹c vĆ“Ć¹i ngƶƓĆøi sƶƻ duĆÆng. VĆ­ duĆÆ 1: <HTML> document.write("Your favorite color <HEAD> is:"); <TITLE>Listing 2.6</TITLE> document.writeln(prompt("enter your </HEAD> favorite color:","Blue")); <BODY> - -> <SCRIPT LANGUAGE="Javascript"> </SCRIPT> <!- - </BODY> </HTML> VĆ­ duĆÆ 2: <HTML> document.write("<H1>Greeting ,"); <HEAD> document.writeln(prompt("enter your <TITLE>Listing 2.6</TITLE> name:","name")); </HEAD> document.write("Welcome to netscape <BODY> navigator 2.01 </H1>"); <SCRIPT LANGUAGE="Javascript"> - -> <!- - </SCRIPT> document.write('<IMG </BODY> SRC=/slideshow/tai-lieu-javascript/5089421/"welcome.gif">& </HTML> Sƶƻ duĆÆng daĆ”u + ƱeĆ„ coƤng 2 chuoĆ„i ƱƓn laĆÆi: VĆ­ duĆÆ 3: <HTML> document.write("<H1>Greeting ," + <HEAD> prompt("enter your name:","name") + " <TITLE>Listing 2.6</TITLE> Welcome to netscape navigator 2.01 </HEAD> </H1>"); <BODY> - -> <SCRIPT LANGUAGE="Javascript"> </SCRIPT> <!- - </BODY> document.write('<IMG </HTML> SRC=/slideshow/tai-lieu-javascript/5089421/"welcome.gif">& 9. CaĆ¹c kieĆ„u dƶƵ lieƤu trong JavaScript: a. DƶƵ lieƤu kieĆ„u soĆ”: + SoĆ” nguyeĆ¢n: vĆ­ duĆÆ 720 + SoĆ” Octal: vĆ­ duĆÆ :056 + SoĆ” Hexa:vĆ­ duĆÆ:0x5F + SoĆ” thaƤp phaĆ¢n :vĆ­ duĆÆ :7.24 , -34.2 ,2E3 b. DƶƵ lieƤu kieĆ„u chuoĆ„i: vĆ­ duĆÆ: ā€ Helloā€ ā€™245ā€™ ā€œā€œ c. DƶƵ lieƤu kieĆ„u Boolean: KeĆ”t quaĆ» traĆ» veĆ  laĆø true hoaĆ«c false. JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 5
  • 6. d. DƶƵ lieƤu kieĆ„u null: TraĆ» veĆ  giaĆ¹ trĆ² roĆ£ng. e. DƶƵ lieƤu kieĆ„u vaĆŖn baĆ»n (gioĆ”ng nhƶ kieĆ„u chuoĆ„i) 10. TaĆÆo bieĆ”n trong JavaScript: Var example; Var example=ā€Helloā€; Ta coĆ¹ theĆ„ duĆøng document.write(example); ƱeĆ„ xuaĆ”t noƤi dung cuĆ»a bieĆ”n. VĆ­ duĆÆ 1: duĆøng tƶĆø khoĆ¹a var ƱeĆ„ khai baĆ¹o bieĆ”n <HTML> <BODY> <HEAD> <SCRIPT LANGUAGE="Javascript"> <TITLE>Example 3.1</TITLE> <!-- document.write('<IMG <SCRIPT LANGUAGE="Javascript"> SRC=/slideshow/tai-lieu-javascript/5089421/"Welcome.gif">& <!- - document.write("<H1>Greeting ," + var name=prompt("enter your name + " Welcome to netscape name:","name"); navigator 2.01 </H1>"); - -> --> </SCRIPT> </SCRIPT> </BODY> </HEAD> </HTML> VĆ­ du 2: taĆÆo laĆÆi moƤt giaĆ¹ trĆ² mĆ“Ć¹i cho bieĆ”n <HTML> </HEAD> <HEAD> <BODY> <TITLE>Example 3.2</TITLE> <SCRIPT LANGUAGE="Javascript"> <!-- <SCRIPT LANGUAGE="Javascript"> document.write('<IMG var name=prompt("enter your SRC=/slideshow/tai-lieu-javascript/5089421/"Welcome.gif">& name:","name"); document.write("<H1>Greeting ," + alert ("greeting " + name + " , "); name + " Welcome to netscape name=prompt("enter your friend's navigator 2.01 </H1>"); name:","friend's name"); --> </SCRIPT> </SCRIPT> </BODY> </HTML> 11. LaĆøm vieƤc vĆ“Ć¹i bieĆ”n vaĆø bieĆ„u thĆ¶Ć¹c: ā€¢ ThieĆ”t laƤp bieĆ„u thĆ¶Ć¹c: CuĆ¹ phaĆ¹p: <bieĆ”n> <toaĆ¹n tƶƻ > <bieĆ„u thĆ¶Ć¹c> * ToaĆ¹n tƶƻ: = ThieĆ”t laƤp giaĆ¹ trĆ² beĆ¢n phaĆ»i cho beĆ¢n traĆ¹i VĆ­ duĆÆ :x=5 += CoƤng traĆ¹i vaĆø phaĆ»i ,sau ƱoĆ¹ gaĆ¹n keĆ”t quaĆ» cho beĆ¢n traĆ¹i pheĆ¹p toaĆ¹n VĆ­ duĆÆ: cho x=10,y=5 x+=y => x=15 -= TrƶĆø beĆ¢n traĆ¹i cho beĆ¢n phaĆ»i ,gaĆ¹n keĆ”t quaĆ» laĆÆi cho beĆ¢n traĆ¹i x-=y => x=5 JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 6
  • 7. *= NhaĆ¢n beĆ¢n traĆ¹i cho beĆ¢n phaĆ»i,gaĆ¹n keĆ”t quaĆ» cho beĆ¢n traĆ¹i x*=y => x=50 /= Chia beĆ¢n traĆ¹i cho phaĆ»i ,gaĆ¹n keĆ”t quaĆ» laĆÆi cho beĆ¢n traĆ¹i x/=y => x=2 %= Chia beĆ¢n traĆ¹i cho beĆ¢n phaĆ»i vaĆø laĆ”y soĆ” dƶ gaĆ¹n laĆÆi cho beĆ¢n traĆ¹i x%=y => x=0 * CaĆ¹c toaĆ¹n tƶƻ khaĆ¹c: VĆ­ duĆÆ: x+=15+3 y=++x; (=> y=6 vƬ x taĆŖng leĆ¢n 6) => x=18 z=x++; (=> z=6 vƬ sau ƱoĆ¹ x gaĆ¹n cho z) 8+5 sau ƱoĆ¹ x taĆŖng 1 => x=7 32.5 * 72.3 Do ƱoĆ¹ ta coĆ¹ keĆ”t quaĆ» cuoĆ”i cuĆøng laĆø: 12 % 5 x=7;y=6;z=6; DaĆ”u ++ vaĆø daĆ”u - - vaĆø daĆ”u - VĆ­ duĆÆ: x=5; VĆ­ duĆÆ: x=-x => x=-5 x=5; ā€¢ PheĆ¹p toaĆ¹n Logic && : vaĆø ||: hoaĆ«c ! not VĆ­ duĆÆ: x=5 ,y=2 ,c=3 (x>y) && (x>c) false && anything is always false (x>y) || (c<x) => true !x ā€¢ ToaĆ¹n tƶƻ so saĆ¹nh trong JavaScript: == 1==1 => true != 3<1 =>false > 5 >=4 =>true < ā€œtheā€ != ā€œheā€ => true >= 4==ā€4ā€ =>true <= VĆ­ duĆÆ: ā€¢ ToaĆ¹n tƶƻ ƱieĆ u kieƤn: CuĆ¹ phaĆ¹p: (ƱieĆ u kieƤn ) ? giaĆ¹ trĆ² 1 : giaĆ¹ trĆ² 2 NeĆ”u ƱieĆ u kieƤn ƱuĆ¹ng thƬ traĆ» veĆ  giaĆ¹ trĆ² 1 NeĆ”u ƱieĆ u kieƤn sai thƬ traĆ» veĆ  giaĆ¹ trĆ² 2 VĆ­ duĆÆ: JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 7
  • 8. (day=ā€Saturdayā€) ? ā€œWeekendā€ : ā€œNot Saturdayā€ ā€¢ ToaĆ¹n tƶƻ chuoĆ„i: ā€œ Welcome to ā€œ + ā€œ Netscape Navigatorā€ VĆ­ duĆÆ: Var welcome=ā€Welcome toā€ Welcome += ā€œ Netscape Navigatorā€ ! welcome= ā€œWelcome to Netsacpe Navigatorā€ VĆ­ duĆÆ : Sƶƻ duĆÆng toaĆ¹n tƶƻ ƱieĆ u kieƤn ƱeĆ„ kieĆ„m tra ngoĆ» vaĆøo <HTML> var output = (response==answer) ? <HEAD> correct:incorrect; <TITLE>Example 3.3</TITLE> </SCRIPT> </HEAD> <SCRIPT LANGUAGE="Javascript"> var question="What is 10+10 ?"; <BODY> var answer=20; <SCRIPT LANGUAGE="Javascript"> var correct='<IMG <!-- SRC=/slideshow/tai-lieu-javascript/5089421/"correct.gif">& document.write(output); var incorrect='<IMG --> SRC="incorect.gif">'; </SCRIPT> var response=prompt(question,"0"); </BODY> </HTML> 12. CaĆ”u truĆ¹c ƱieĆ u kieƤn if ā€“ else if ƱieĆ u kieƤn leƤnh ; if ƱieĆ u kieƤn { MaƵ JavaScript } VĆ­ duĆÆ: If (day!=ā€Saturdayā€) { if (day==ā€Saturdayā€) { document.writeln(ā€œItā€˜s not Saturdayā€); document.writeln(ā€œItā€˜s the weekendā€); } alert(ā€œ Itā€™s the weekendā€); Sƶƻ duĆÆng caĆ”u truĆ¹c else ā€“ if cho vĆ­ duĆÆ Ć“Ć» treĆ¢n } If (day==ā€Saturdayā€) { VĆ­ duĆÆ: document.writeln(ā€œItā€˜s the weekendā€); If (day==ā€Saturdayā€) { } document.writeln(ā€œItā€˜s the weekendā€); else { } document.writeln(ā€œItā€˜s not Saturdayā€); } CaĆ”u truĆ¹c keĆ”t hĆ“ĆÆp : if ƱieĆ u kieƤn 1 { CaĆ¹c leƤnh JavaScript JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 8
  • 9. if ƱieĆ u kieƤn 2 { CaĆ¹c leƤnh JavaScript } else { caĆ¹c leƤnh khaĆ¹c } CaĆ¹c leƤnh JavaScript } else { CaĆ¹c leƤnh khaĆ¹c } VĆ­ duĆÆ 1 : Sƶƻ duĆÆng phƶƓng phaĆ¹p confirm() vĆ“Ć¹i phaĆ¹t bieĆ„u if <HTML> response=prompt(question,"0"); <HEAD> } <TITLE>Example 3.3</TITLE> var output = (response ==answer ) ? <SCRIPT LANGUAGE="Javascript"> correct:incorrect ; var question="What is 10+10 ?"; var answer=20; </SCRIPT> var correct='<IMG SRC=/slideshow/tai-lieu-javascript/5089421/"correct.gif">& </HEAD> var incorrect='<IMG <BODY> SRC="incorect.gif">'; <SCRIPT LANGUAGE="Javascript"> <!-- var response=prompt(question,"0"); document.write(output); --> if (response != answer) { </SCRIPT> if (confirm("Wrong ! press OK for </BODY> a second change")) </HTML> VĆ­ duĆÆ 2 : Sƶƻ duĆÆng phƶƓng phaĆ¹p confirm() vĆ“Ć¹i phaĆ¹t bieĆ„u if - else <HTML> question="What is 10*10"; <HEAD> answer=100; <TITLE>Example 3.3</TITLE> response=prompt(question,"0"); <SCRIPT LANGUAGE="Javascript"> } var question="What is 10+10 ?"; } var answer=20; var correct='<IMG var output = (response ==answer ) ? SRC=/slideshow/tai-lieu-javascript/5089421/"correct.gif">& correct:incorrect ; var incorrect='<IMG SRC="incorect.gif">'; </SCRIPT> var response=prompt(question,"0"); </HEAD> <BODY> if (response != answer) { <SCRIPT LANGUAGE="Javascript"> if (confirm("Wrong ! press OK for <!-- a second change")) document.write(output); response=prompt(question,"0"); --> }else { </SCRIPT> if (confirm("Correct ! press OK </BODY> for a second question")) </HTML> MTWRFSS { JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 9
  • 10. BAƘI 3: HAƘM VAƘ ƑOƁI TƖƔƏNG Trong kyƵ thuaƤt laƤp trƬnh caĆ¹c laƤp trƬnh vieĆ¢n thƶƓĆøng sƶƻ duĆÆng haĆøm ƱeĆ„ thƶĆÆc hieƤn moƤt ƱoaĆÆn chƶƓng trƬnh theĆ„ hieƤn cho moƤt module naĆøo ƱoĆ¹ ƱeĆ„ thƶĆÆc hieƤn moƤt coĆ¢ng vieƤc naĆøo ƱoĆ¹. Trong Javascript coĆ¹ caĆ¹c haĆøm ƱƶƓĆÆc xaĆ¢y dƶĆÆng saĆ¼n ƱeĆ„ giuĆ¹p baĆÆn thƶĆÆc hieƤn moƤt chĆ¶Ć¹c naĆŖng naĆøo ƱoĆ¹ vĆ­ duĆÆ nhƶ haĆøm alert(), document.write(), parseInt() vaĆø baĆÆn cuƵng coĆ¹ theĆ„ Ć±Ć²nh nghĆ³a ra caĆ¹c haĆøm khaĆ¹c cuĆ»a mƬnh ƱeĆ„ thƶĆÆc hieƤn moƤt coĆ¢ng vieƤc naĆøo ƱoĆ¹ cuĆ»a baĆÆn, ƱeĆ„ Ć±Ć²nh nghĆ³a haĆøm baĆÆn theo cuĆ¹ phaĆ¹p sau: function function_name(parameters, arguments) { command block } TruyeĆ n tham soĆ”: function printName(name) { document.write(ā€œ<HR>Your Name is <B><I>ā€); document.write(name); document.write(ā€œ</B></I><HR>ā€); } VĆ­ duĆÆ: GoĆÆi haĆøm printName()vĆ“Ć¹i leƤnh sau printName(ā€œBobā€); Khi haĆøm printName()ƱƶƓĆÆc thi haĆønh giaĆ¹ trĆ² cuĆ»a name laĆø "Bob" neĆ”u goĆÆi haĆøm printName()vĆ“Ć¹i ƱoĆ”i soĆ” laĆø moƤt bieĆ”n var user = ā€œJohnā€; printName(user); Khi ƱoĆ¹ name laĆø ā€œJohnā€. NeĆ”u baĆÆn muoĆ”n thay ƱoĆ„i giaĆ¹ trĆ² cuĆ»a name baĆÆn coĆ¹ theĆ„ laĆøm nhƶ sau : name = ā€œMr. ā€œ + name; PhaĆÆm vi cuĆ»a bieĆ”n: BieĆ”n toaĆøn cuĆÆc (Global variable) BieĆ”n cuĆÆc boƤ (Local variable) TraĆ» veĆ  caĆ¹c giaĆ¹ trĆ²: VĆ­ duĆÆ: DuĆøng return ƱeĆ„ traĆ» veĆ  giaĆ¹ trĆ² cuĆ»a bieĆ”n cube. MTWRFSS function cube(number) { var cube = number * number * number; return cube; } JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 10
  • 11. VĆ­ duĆÆ: MTWRFSS <HTML> //CHECK THE RESULT <HEAD> return (response == answer) ? correct <TITLE>Example 4.1</TITLE> : incorrect; <SCRIPT LANGUAGE=ā€JavaScriptā€> } <!-- HIDE FROM OTHER BROWSERS // STOP HIDING FROM OTHER BROWSERS -- //DEFINE FUNCTION testQuestion() > function testQuestion(question) { </SCRIPT> //DEFINE LOCAL VARIABLES FOR THE </HEAD< FUNCTION <BODY> var answer=eval(question); <SCRIPT LANGUAGE=ā€JavaScriptā€> var output=ā€What is ā€œ + question + <!-- HIDE FROM OTHER BROWSERS ā€œ?ā€; //ASK QUESTION AND OUTPUT RESULTS var correct=ā€™<IMG var result=testQuestion(ā€œ10 + 10ā€); SRC=/slideshow/tai-lieu-javascript/5089421/ā€correct.gifā€>ā€™; document.write(result); var incorrect=ā€™<IMG //STOP HIDING FROM OTHER BROWSERS --> SRC=/slideshow/tai-lieu-javascript/5089421/ā€incorrect.gifā€>ā€™; </SCRIPT> //ASK THE QUESTION </BODY> var response=prompt(output,ā€0"); </HTML> HaĆøm eval duĆøng chuyeĆ„n ƱoĆ„i giaĆ¹ trĆ² chuoĆ„i soĆ” thaĆønh giaĆ¹ trĆ² soĆ” eval(ā€œ10*10ā€)traĆ» veĆ  giaĆ¹ trĆ² laĆø 100 HaĆøm goĆÆi laĆÆi haĆøm: VĆ­ duĆÆ: <HTML> //CHECK THE RESULT <HEAD> return (response == answer) ? correct <TITLE>Example 4.2</TITLE> : testQuestion(question); <SCRIPT LANGUAGE=ā€JavaScriptā€> } <!-- HIDE FROM OTHER BROWSERS // STOP HIDING FROM OTHER BROWSERS -- //DEFINE FUNCTION testQuestion() > function testQuestion(question) { </SCRIPT> //DEFINE LOCAL VARIABLES FOR THE </HEAD< FUNCTION <BODY> var answer=eval(question); <SCRIPT LANGUAGE=ā€JavaScriptā€> var output=ā€What is ā€œ + question + <!-- HIDE FROM OTHER BROWSERS ā€œ?ā€; //ASK QUESTION AND OUTPUT RESULTS var correct=ā€™<IMG var result=testQuestion(ā€œ10 + 10ā€); SRC=/slideshow/tai-lieu-javascript/5089421/ā€correct.gifā€>ā€™; document.write(result); var incorrect=ā€™<IMG //STOP HIDING FROM OTHER BROWSERS --> SRC=/slideshow/tai-lieu-javascript/5089421/ā€incorrect.gifā€>ā€™; </SCRIPT> //ASK THE QUESTION </BODY> var response=prompt(output,ā€0"); </HTML> VĆ­ duĆÆ 2: <HTML> //DEFINE LOCAL VARIABLES FOR THE <HEAD> FUNCTION <TITLE>Example 4.2</TITLE> var answer=eval(question); <SCRIPT LANGUAGE=ā€JavaScriptā€> var output=ā€What is ā€œ + question + <!-- HIDE FROM OTHER BROWSERS ā€œ?ā€; //DEFINE FUNCTION testQuestion() var correct=ā€™<IMG function SRC=/slideshow/tai-lieu-javascript/5089421/ā€correct.gifā€>ā€™; testQuestion(question,chances) { JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 11
  • 12. var incorrect=ā€™<IMG // STOP HIDING FROM OTHER BROWSERS -- SRC=/slideshow/tai-lieu-javascript/5089421/ā€incorrect.gifā€>ā€™; > 4 </SCRIPT> //ASK THE QUESTION </HEAD> var response=prompt(output,ā€0"); <BODY> //CHECK THE RESULT <SCRIPT LANGUAGE=ā€JavaScriptā€> if (chances > 1) { <!-- HIDE FROM OTHER BROWSERS return (response == answer) ? correct //ASK QUESTION AND OUTPUT RESULTS : testQuestion(question,chances-1); var result=testQuestion(ā€œ10 + 10ā€,3); } else { document.write(result); return (response == answer) ? correct //STOP HIDING FROM OTHER BROWSERS --> : incorrect; </SCRIPT> } </BODY> } </HTML> BaĆøi 4: TAƏO ƑOƁI TƖƔƏNG TRONG JAVASCRIPT 1. Ć‘Ć²nh nghĆ³a thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng: function student(name,age, grade) { this.name = name; this.age = age; this.grade = grade; } ƑeĆ„ taĆÆo moƤt Object ta sƶƻ duĆÆng phaĆ¹t bieĆ„u new.VĆ­ duĆÆ Ć±eĆ„ taĆÆo ƱoĆ”i tƶƓĆÆng student1 student1 = new student(ā€œBobā€,10,75); 3 thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng student1 laĆø : student1.name student1.age student1.grade VĆ­ duĆÆ Ć±eĆ„ taĆÆo ƱoĆ”i tƶƓĆÆng student2 student2 = new student(ā€œJaneā€,9,82); ƑeĆ„ theĆ¢m thuoƤc tĆ­nh cho student1 baĆÆn coĆ¹ theĆ„ laĆøm nhƶ sau: student1.mother = ā€œSusanā€; hoaĆ«c baĆÆn coĆ¹ theĆ„ Ć±Ć²nh nghĆ³a laĆÆi haĆøm student MTWRFSS 4 function student(name, age, grade, mother) { this.name = name; this.age = age; this.grade = grade; this.mother = mother; } JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 12
  • 13. ƑoĆ”i tƶƓĆÆng laĆø thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng khaĆ¹c VĆ­ duĆÆ: function grade (math, english, science) { this.math = math; this.english = english; this.science = science; } bobGrade = new grade(75,80,77); janeGrade = new grade(82,88,75); student1 = new student(ā€œBobā€,10,bobGrade); student2 = new student(ā€œJaneā€,9,janeGrade); student1.grade.math:duĆøng ƱeĆ„ laĆ”y ƱieĆ„m ToaĆ¹n cuĆ»a student1 student2.grade.science: duĆøng laĆ”y ƱieĆ„m moĆ¢n Khoa hoĆÆc cuĆ»a student2 2. TheĆ¢m phƶƓng phaĆ¹p cho ƱoĆ”i tƶƓĆÆng: function displayProfile() { document.write(ā€œName: ā€œ + this.name + ā€œ<BR>ā€); document.write(ā€œAge: ā€œ + this.age + ā€œ<BR>ā€); document.write(ā€œMotherā€™s Name: ā€œ + this.mother + ā€œ<BR>ā€); document.write(ā€œMath Grade: ā€œ + this.grade.math + ā€œ<BR>ā€); document.write(ā€œEnglish Grade: ā€œ + this.grade.english + ā€œ<BR>ā€); document.write(ā€œScience Grade: ā€œ + this.grade.science + ā€œ<BR>ā€); } function student(name,age, grade) { this.name = name; this.age = age; this.grade = grade; this.mother = mother; this.displayProfile = displayProfile; } student1.displayProfile(); VĆ­ du: 4 <HTML> function displayInfo() { <HEAD> document.write(ā€œ<H1>Employee Profile: <TITLE>Example 4.3</TITLE> ā€œ + this.name + ā€œ</H1><HR><PRE>ā€); <SCRIPT LANGUAGE=ā€JavaScriptā€> document.writeln(ā€œEmployee Number: ā€œ <!-- HIDE FROM OTHER BROWSERS + this.number); //DEFINE METHOD JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 13
  • 14. document.writeln(ā€œSocial Security // STOP HIDING FROM OTHER BROWSERS -- Number: ā€œ + this.socsec); > document.writeln(ā€œAnnual Salary: ā€œ + </SCRIPT> this.salary); </HEAD> document.write(ā€œ</PRE>ā€); <BODY> } <SCRIPT LANGUAGE=ā€JavaScriptā€> //DEFINE OBJECT <!-- HIDE FROM OTHER BROWSERS function employee() { newEmployee.displayInfo(); this.name=prompt(ā€œEnter Employeeā€™s // STOP HIDING FROM OTHER BROWSERS -- Nameā€,ā€Nameā€); > this.number=prompt(ā€œEnter Employee </SCRIPT> Number for ā€œ + this.name,ā€000-000"); </BODY> this.socsec=prompt(ā€œEnter Social </HTML> Security Number for ā€œ + this.name,ā€000-00-0000"); this.salary=prompt(ā€œEnter Annual Salary for ā€œ + this.name,ā€$00,000"); this.displayInfo=displayInfo; } newEmployee=new employee(); Vi du: <script LANGUAGE="JavaScript"> myhours = mydate.getHours(); <!-- Begin ampmhour = (myhours > 12) ? myhours - var day=""; 12 : myhours; var month=""; ampm = (myhours >= 12) ? 'BuoĆ„ i ChieĆ  u ' var ampm=""; : ' BuoĆ„ i SaĆ¹ ng '; var ampmhour=""; mytime = mydate.getMinutes(); var myweekday=""; myminutes = ((mytime < 10) ? ':0' : var year=""; ':') + mytime; mydate = new Date(); if(myday == 0) myday = mydate.getDay(); day = " ChuĆ» NhaƤ t , "; mymonth = mydate.getMonth(); else if(myday == 1) myweekday= mydate.getDate(); day = " ThĆ¶Ć¹ hai, "; weekday= myweekday; else if(myday == 2) myyear= mydate.getYear(); day = " ThĆ¶Ć¹ ba, "; year = myyear; else if(myday == 3) JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 14
  • 15. day = " ThĆ¶Ć¹ tƶ, "; else if(mymonth ==5) else if(myday == 4) month = "thaĆ¹ ng saĆ¹ u "; day = " ThĆ¶Ć¹ naĆŖ m, "; else if(mymonth ==6) else if(myday == 5) month = "thaĆ¹ ng baĆ» y "; day = " ThĆ¶Ć¹ saĆ¹ u , "; else if(mymonth ==7) else if(myday == 6) month = "thaĆ¹ ng taĆ¹ m "; day = " ThĆ¶Ć¹ baĆ» y , "; else if(mymonth ==8) if(mymonth == 0) { month = "thaĆ¹ ng chĆ­n "; month = "thaĆ¹ ng moƤ t ";} else if(mymonth ==9) else if(mymonth ==1) month = "thaĆ¹ ng mƶƓĆø i "; month = "thaĆ¹ ng hai "; else if(mymonth ==10) else if(mymonth ==2) month = "thaĆ¹ ng mƶƓĆø i moƤ t "; month = "thaĆ¹ ng ba "; else if(mymonth ==11) else if(mymonth ==3) month = "thaĆ¹ ng mƶƓĆø i hai "; month = "thaĆ¹ ng tƶ "; // End --> else if(mymonth ==4) </script> month = "thaĆ¹ ng naĆŖ m, "; Trong phaĆ n body baĆÆn coĆ¹ theĆ„ xuaĆ”t ra daĆÆng nhƶ sau: <body> <script> document.write("<b><font color=#0000ff face='VNI- Times,helvetica,arial'>" + ampmhour + "" + myminutes + ampm) document.write(" - " + day + " ngaĆø y " + myweekday +" "); document.write( month + " , naĆŖ m " + year + "</font>"); </script> </body> BaĆøi 5: SƖƏ KIEƄN TRONG JAVASCRIPT CaĆ¹c sƶĆÆ kieƤn cung caĆ”p caĆ¹c tƶƓng taĆ¹c vĆ“Ć¹i cƶƻa soĆ„ trƬnh duyeƤt vaĆø taĆøi lieƤu hieƤn haĆønh Ʊang ƱƶƓĆÆc load trong trang web, caĆ¹c haĆønh ƱoƤng cuĆ»a user khi nhaƤp dƶƵ lieƤu vaĆøo form vaĆø khi click vaĆøo caĆ¹c button trong form. Khi sƶƻ duĆÆng boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn baĆÆn coĆ¹ theĆ„ vieĆ”t caĆ¹c haĆøm ƱeĆ„ bieĆ„u dieĆ£n cho caĆ¹c haĆønh ƱoƤng dƶĆÆa vaĆøo caĆ¹c sƶĆÆ kieƤn ƱƶĆÆoc choĆÆn BaĆ»ng sƶĆÆ kieƤn trong Javascript JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 15
  • 16. TeĆ¢n sƶĆÆ kieƤn MoĆ¢ taĆ» blur XaƵy ra khi ƱieĆ„m taƤp trungcuĆ»a ngoƵ vaĆøo ƱƶƓĆÆc di chuyeĆ„n ra khoĆ»i moƤt thaĆønh phaĆ n cuĆ»a Form (Khi user click ra ngoaĆøi moƤt trƶƓĆøng) click Khi user Click vaĆøo 1 link hoaĆ«c thaĆønh phaĆ n cuĆ»a Form. change XaƵy ra khi giaĆ¹ trĆ² cuĆ»a Form Field bĆ² thay ƱoĆ„i bĆ“Ć»i user. focus XaƵy ra khi ngoƵ vaĆøo taƤp trung vaĆøo thaĆønh phaĆ n cuĆ»a Form load XaƵy ra khi moƤt trang ƱƶƓĆÆc Load vaĆøo trong boƤ duyeƤt. mouseover XaƵy ra khi User di chuyeĆ„n mouse qua moƤt Hyperlink. select XaƵy ra khi User choĆÆn 1 trƶƓĆøng cuĆ»a thaĆønh phaĆ n Form. submit XaƵy ra khi User xaĆ¹c nhaƤn ƱaƵ nhaƤp xong dƶƵ lieƤu. unload XaƵy ra khi User rĆ“Ćøi khoĆ»i trang Web. BoƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn (Event Handler) ƑeĆ„ quaĆ»n lyĆ¹ caĆ¹c sƶĆÆ kieƤn trong javascript ta duĆøng caĆ¹c boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn. CuĆ¹ phaĆ¹p cuĆ»a moƤt boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn: <HTML_TAG OTHER_ATTRIBUTES eventHandler=ā€JavaScript Programā€> VĆ­ duĆÆ: <INPUT TYPE=ā€textā€ onChange=ā€checkField(this)ā€> VĆ­ duĆÆ: <INPUT TYPE=ā€textā€ onChange=ā€ if (parseInt(this.value) <= 5) { alert(ā€˜Please enter a number greater than 5.ā€™); } ā€œ> VĆ­ duĆÆ: <INPUT TYPE=ā€textā€ onChange=ā€ alert(ā€˜Thanks for the entry.ā€™); confirm(ā€˜Do you want to continue?ā€™); ā€œ> TƶĆø khoĆ¹a this: quy cho ƱoĆ”i tƶƓĆÆng hieƤn haĆønh.Trong Javascript Form laĆø moƤ ƱoĆ”i tƶƓĆÆng.CaĆ¹c thaĆønh phaĆ n cuĆ»a Form bao goĆ m text fields, checkboxes, radio buttons, buttons, vaĆø selection lists. VĆ­ duĆÆ: <INPUT TYPE=ā€textā€ onChange=ā€checkField(this)ā€> CaĆ¹c boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn trong Javascript ƑoĆ”i tƶƓĆÆng BoƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn tƶƓng Ć¶Ć¹ng. JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 16
  • 17. Selection list onBlur, onChange, onFocus Text element onBlur, onChange, onFocus, onSelect Textarea element onBlur, onChange, onFocus, onSelect Button element OnClick Checkbox onClick Radio button OnClick Hypertext link onClick, onMouseOver Reset button OnClick Submit button OnClick Document onLoad, onUnload Window onLoad, onUnload Form onSubmit 5 CaĆ¹ch duĆøng boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn onLoad & onUnload <HTML> <HEAD> <TITLE>Example 5.1</TITLE> </HEAD> <BODY onLoad=ā€alert(ā€˜Welcome to my page!ā€™);ā€ onUnload=ā€alert(ā€˜Goodbye! Sorry to see you go!ā€™);ā€> <IMG SRC=/slideshow/tai-lieu-javascript/5089421/ā€title.gifā€> </BODY> </HTML> MTWRFSS Vi du: <HTML> <HEAD> <TITLE>Example 5.1</TITLE> <SCRIPT LANGUAGE=ā€JavaScriptā€> <!-- HIDE FROM OTHER BROWSERS var name = ā€œā€; // STOP HIDING FROM OTHER BROWSERS --> </SCRIPT> </HEAD> <BODY onLoad=ā€ name = prompt(ā€˜Enter Your Name:ā€™,ā€™Nameā€™); alert(ā€˜Greetings ā€˜ + name + ā€˜, welcome to my page!ā€™);ā€ onUnload=ā€ alert(Goodbye ā€˜ + name + ā€˜, sorry to see you go!ā€™);ā€> <IMG SRC=/slideshow/tai-lieu-javascript/5089421/ā€title.gifā€> </BODY> </HTML> Vi du MTWRFSS <HTML> <HEAD> JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 17
  • 18. <TITLE>Example 5.1</TITLE> <SCRIPT LANGUAGE=ā€JavaScriptā€> <!-- HIDE FROM OTHER BROWSERS // DEFINE GLOBAL VARIABLE var name = ā€œā€; function hello() { name = prompt(ā€˜Enter Your Name:ā€™,ā€™Nameā€™); alert(ā€˜Greetings ā€˜ + name + ā€˜, welcome to my page!ā€™); } function goodbye() { alert(Goodbye ā€˜ + name + ā€˜, sorry to see you go!ā€™); } // STOP HIDING FROM OTHER BROWSERS --> </SCRIPT> </HEAD> <BODY onLoad=ā€hello();ā€ onUnload=ā€goodbye();ā€> <IMG SRC=/slideshow/tai-lieu-javascript/5089421/ā€title.gifā€> </BODY> </HTML> CaĆ¹c sƶĆÆ kieƤn vaĆø Form Cac sƶĆÆ kieƤn ƱƶƓĆÆc sƶƻ duĆÆng ƱeĆ„ truy xuaĆ”t Form nhƶ: OnClick, onSubmit, onFocus, onBlur, vaĆø onChange. VĆ­ duĆÆ: <INPUT TYPE=text NAME=ā€testā€ VALUE=ā€testā€ onBlur=ā€alert(ā€˜Thank You!ā€™);ā€ onChange=ā€check(this);ā€> Khi giaĆ¹ trĆ² thay ƱoĆ„i function check() seƵ ƱƶƓĆÆc goĆÆi. Ta duĆøng tƶĆø khoĆ¹a this ƱeĆ„ chuyeĆ„n ƱoĆ”i tƶƓĆÆng cuĆ»a trƶƓĆøng hieƤn haĆønh ƱeĆ”n haĆøm check() BaĆÆn cuƵng coĆ¹ theĆ„ dƶĆÆa vaĆøo caĆ¹c phƶƓng phaĆ¹p vaĆø caĆ¹c thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng baĆØng phaĆ¹t bieĆ„u sau: this.methodName() & this.propertyName. VĆ­ duĆÆ: <HTML> expressionā€,ā€ā€); <HEAD> calculate(form); <TITLE>Example 5.3</TITLE> } //STOP HIDING FROM OTHER BROWSERS --> <SCRIPT LANGUAGE=ā€JavaScriptā€> </SCRIPT> <!-- HIDE FROM OTHER BROWSERS </HEAD> function calculate(form) { <BODY> form.results.value = <FORM METHOD=POST> eval(form.entry.value); Enter a JavaScript mathematical } expression: function getExpression(form) { <INPUT TYPE=text NAME=ā€entryā€ form.entry.blur(); VALUE=ā€ā€ form.entry.value = prompt(ā€œPlease onFocus=ā€getExpression(this.form);ā€> enter a JavaScript mathematical <BR> JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 18
  • 19. The result of this expression is: </FORM> <INPUT TYPE=text NAME=ā€resultsā€ </BODY> VALUE=ā€ā€ </HTML> onFocus=ā€this.blur();ā€> MTWRFSS formObjectName.fieldname:DuĆøng ƱeĆ„ chƦ teĆ¢n trƶƓĆøng cuĆ»a hieƤn haĆønh trong Form. formObjectName.fieldname.value: duĆøng laĆ”y giaĆ¹ trĆ² cuĆ»a trƶƓĆøng form hieƤn haĆønh. Sƶƻ duĆÆng voĆøng laĆ«p trong JavaScript 1 . VoĆøng laĆ«p for : CuĆ¹ phaĆ¹p : for ( init value ; condition ; update expression ) VĆ­ duĆÆ : for (i = 0 ; i < 5 ; i++) { leƤnh ; } VĆ­ duĆÆ: <HTML> <HEAD> <TITLE> for loop Examle </TITLE> </HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript"> <!- - var name=prompt("What is your name?" ,"name"); var query= " " ; document.write("<H1>" + name + " 's 10 favorite foods </H1> "); for (var i=1 ;i<=10;i++) { JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 19
  • 20. document.write(i + " " + prompt('Enter food number ' + i, 'food' ) + '<BR>'); } - -> </SCRIPT> </BODY> </HTML> 2 . VoĆøng laĆ«p while : CuĆ¹ phaĆ¹p: While ( ƱieĆ u kieƤn) { leƤnh JavaScript ; } VĆ­ duĆÆ: var num=1; while(num<=10) { document.writeln(num); num++; } VĆ­ duĆÆ: var answer=ā€ ā€œ ; var correc=100; var question=ā€ what is 10*10 ?ā€ ; while(answer!=correct) { answer=prompt(question,ā€0ā€); } 3. TaĆÆo maĆ»ng vĆ“Ć¹i voĆøng laĆ«p for: function createArray(num) { this.length=num; for ( var j=0 ; j<num; j++) this[j]=0; } HaĆøm seƵ taĆÆo moƤt maĆ»ng coĆ¹ giaĆ¹ trĆ² index baĆ©t ƱaĆ u laĆø 0 vaĆø gaĆ¹n taĆ”t caĆ» caĆ¹c giaĆ¹ trĆ² cuĆ»a maĆ»ng veĆ  0 . ƑeĆ„ sƶƻ duĆÆng ƱoĆ”i tƶƓĆÆng maĆ»ng ta coĆ¹ theĆ„ laĆøm nhƶ sau: newArray= new createArray(4) SeƵ taĆÆo ra moƤt maĆ»ng goĆ m 4 thaĆønh phaĆ n newArray[0] ā€¦ NewArray[3] JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 20
  • 21. Sƶƻ duĆÆng ƱoĆ”i tƶƓĆÆng Windows Window laĆø ƱoĆ”i tƶƓĆÆng cuĆ»a moĆ¢i trƶƓĆøng Navigator,ngoaĆøi caĆ¹c thuoƤc tĆ­nh Window ƱoĆ”i tƶƓĆÆng window coĆøn giƶƵ caĆ¹c ƱoĆ”i tƶƓĆÆng khaĆ¹c maĆø coĆ¹ theĆ„ ƱƶƓĆÆc xem nhƶ laĆø caĆ¹c thaĆønh phaĆ n (member) cuĆ»a window, caĆ¹c ƱoĆ”i tƶƓĆÆng ƱoĆ¹ laĆø: ā€¢ CaĆ¹c frame ƱaƵ ƱƶƓĆÆc taĆÆo ā€¢ CaĆ¹c ƱoĆ”i tƶƓĆÆng location vaĆø histtory ā€¢ ƑoĆ”i tƶƓĆÆng document ƑoĆ”i tƶƓĆÆng document chĆ¶Ć¹a (encompasses) taĆ”t caĆ» caĆ¹c thaĆønh phaĆ n trong trang HTML.ƑaĆ¢y laĆø moƤt ƱoĆ”i tƶƓĆÆng hoaĆøn haĆ»o coĆ¹ caĆ¹c ƱoĆ”i tƶƓĆÆng khaĆ¹c cuĆ»a JavaScript gaĆ¹n (attached) vaĆøo noĆ¹ (nhƶ laĆø anchor,form,history,link).HaĆ u nhƶ moĆÆi chƶƓng trƬnh JavaScript ƱeĆ u coĆ¹ sƶƻ duĆÆng ƱoĆ”i tƶƓĆÆng naĆøy ƱeĆ„ tham khaĆ»o ƱeĆ”n caĆ¹c thaĆønh phaĆ n trong trang HTML. 1) CaĆ¹c thuoƤc tĆ­nh (properties) cuĆ»a ƱoĆ”i tƶƓĆÆng document a . alink b . anchor c . bgColor d . cookies e . fgColor f . form g . lastModified h . linkColor i . links j . location k . referrer l . title m . vlinkColor 2) CaĆ¹c haĆønh vi (Methods) cuĆ»a ƱoĆ”i tƶƓĆÆng document a . clear() b . close() c . open() d . write() e . writeln() 3) CaĆ¹c thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng Window a . defaultStatus : GiaĆ¹ trĆ² maĆ«t nhieĆ¢n ƱƶƓĆÆc hieĆ„n thĆ² Ć“Ć» thanh traĆÆng thaĆ¹i b . frames : MaĆ»ng caĆ¹c ƱoĆ”i tƶƓĆÆng chĆ¶Ć¹a ƱƶĆÆng moƤt muĆÆc cho moĆ£i frame con trong moƤt frame taĆøi lieƤu c . parent : ƑƶƓĆÆc sƶƻ duĆÆng trong FRAMSET d . self : Cƶƻa soĆ„ hieƤn haĆønh , duĆøng ƱeĆ„ phaĆ¢n bieƤt giƶƵa caĆ¹c cƶƻa soĆ„ hieƤn haĆønh vaĆø caĆ¹c forms coĆ¹ cuĆøng teĆ¢n . e . status : GiaĆ¹ trĆ² cuĆ»a chuoĆ£i vaĆŖn baĆ»n ƱƶƓĆÆc hieĆ„n thĆ² taĆÆi thanh status bar.DuĆøng ƱeĆ„ hieĆ„n thi caĆ¹c thoĆ¢ng baĆ¹o cho ngƶƓĆøi sƶƻ duĆÆng . f . top : ƑƦnh cao nhaĆ”t cuĆ»a cƶƻa soĆ„ cha JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 21
  • 22. g. window 4) CaĆ¹c haĆønh vi (Methods) cuĆ»a ƱoĆ”i tƶƓĆÆng window a. alert() : HieƤn 1 thoĆ¢ng baĆ¹o trong hoƤp thoaĆÆi vĆ“Ć¹i OK button. b. close() : ƑoĆ¹ng cƶƻa soĆ„ hieƤn haĆønh. c. open() : MĆ“Ć» moƤt cƶƻa soĆ„ mĆ“Ć¹i vĆ“Ć¹i 1 taĆøi lieƤu ƱƶƓĆÆc chƦ ra hoaĆ«c mĆ“Ć» moƤt taĆøi lieƤu trong moƤt teĆ¢n cƶƻa soĆ„ ƱƶƓĆÆc chƦ Ć±Ć²nh. d . prompt() : HieƤn moƤt hoƤp thoĆ¢ng baĆ¹o e . setTimeout() : f . clearTimeout() : HaĆønh vi naĆøy cung caĆ”p caĆ¹ch goĆÆi phaĆ¹t bieĆ„u JavaScript sau moƤt khoaĆ»ng thĆ“Ćøi gian troĆ¢i qua .NgoaĆøi ra ƱoĆ”i tƶƓĆÆng window coĆ¹ theĆ„ thƶĆÆc hieƤn event handler : onLoad=statement LaĆøm vieƤc vĆ“Ć¹i status bar Khi user di chuyeĆ„n qua moƤt hyperlink ta coĆ¹ theĆ„ hieƤn ra moƤt thoĆ¢ng baĆ¹o taĆÆi thanh status bar cuĆ»a bowser dƶĆÆa vaĆøo event handler onMouseOver vaĆø baĆØng caĆ¹ch ƱaĆ«t self.status laĆø moƤt chuoĆ„i (hoaĆ«c window.status). VĆ­ duĆÆ: <HTML> <HEAD> <TITLE>Status Example</TITLE> <BODY> <A href=ā€plc.htmā€ onMouseOver=ā€self.status=ā€™Chuyen de PLCā€™ ;return true ; ā€œ >Lop chuyen deĆ  PLC </A> <A href=ā€tkweb.htmā€ onMouseOver=ā€self.status=ā€™Thiet Ke Trang Webā€™ ;return true ; ā€œ >Thiet Ke Web</A> </BODY> </HTML> MĆ“Ć» vaĆø ƱoĆ¹ng caĆ¹c cƶƻa soĆ„ Sƶƻ duĆÆng phƶƓng phaĆ¹p open() vaĆø close() ta coĆ¹ theĆ„ ƱieĆ u khieĆ„n vieƤc mĆ“Ć» vaĆø ƱoĆ¹ng cƶƻa soĆ„ chĆ¶Ć¹a taĆøi lieƤu. open (ā€œURLā€ , ā€œWindowNameā€ , ā€œfeatureListā€) ; CaĆ¹c ƱaĆ«c ƱieĆ„m trong phƶƓng phaĆ¹p open() goĆ m coĆ¹: ā€¢ toolbar : taĆÆo moƤt toolbar chuaĆ„n ā€¢ location: taĆÆo moƤt vuĆøng location ā€¢ directories: taĆÆo caĆ¹c button thƶ muĆÆc chuaĆ„n ā€¢ status: taĆÆo thanh traĆÆng thaĆ¹i. ā€¢ menubar : taĆÆo thanh menu taĆÆi ƱƦnh cuĆ»a cƶƻa soĆ„ ā€¢ scrollbars: taĆÆo thanh scroll bar ā€¢ resizable: cho pheĆ¹p user thay ƱoĆ„i kĆ­ch thĆ¶Ć“Ć¹c cƶƻa soĆ„ ā€¢ width : chƦ Ć±Ć²nh chieĆ u roƤng cƶƻa soĆ„ theo ƱƓn vĆ² pixel ā€¢ height : chƦ Ć±Ć²nh chieĆ u cao cƶƻa soĆ„ theo ƱƓn vĆ² pixel VĆ­ duĆÆ: window.open( ā€œplc.htmā€,ā€newWindowā€,ā€toolbar=yes,location=1,directories=yes,status=yes, JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 22
  • 23. menubar=1,scroolbar=yes,resizable=0,copyhistory=1,width=200,height=200ā€); VĆ­ duĆÆ: <HTML> <HEAD> <TITLE>WINDOWS</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function openWindow(url,name) { popupWin = window.open(url, name, "scrollbars=yes,width=800, heigth=200 "); } --> </SCRIPT> </HEAD> <BODY> <a href="javascript:openWindow('../chuyende/plc.htm','Win')">PLC</a>, <a href="javascript:openWindow('../chuyende/suachuaw.htm','stoogeWin')">Sua chua</a>, <a href="javascript:openWindow('../chuyende/tkweb.htm','stoogeWin')">Thiet ke web</a> </BODY> </HTML> ƑeĆ„ ƱoĆ¹ng cƶƻa soĆ„ ta coĆ¹ theĆ„ duĆøng phƶƓng phaĆ¹p close() VĆ­ duĆÆ: <HTML> <HEAD> <TITLE>Close Example</TITLE> </HEAD> <BODY > <A href="#" ONCLICK="self.close();return false"><IMG ALIGN="middle" SRC=/slideshow/tai-lieu-javascript/5089421/"../demo.gif" WIDTH="16" HEIGHT="16" BORDER="0"></A> <A href="#" ONCLICK="self.close();return false">Close This Sample</A> </BODY > </HTML> Sƶƻ duĆÆng ƱoĆ”i tƶƓĆÆng string String laĆø moƤt ƱoĆ”i tƶƓĆÆng cuĆ»a JavaScript,khi duĆøng ƱoĆ”i tƶƓĆÆng string chuĆ¹ng ta khoĆ¢ng caĆ n caĆ¹c phaĆ¹t bieĆ„u ƱeĆ„ taĆÆo moƤt instance (theĆ„ nghieƤm) cuĆ»a ƱoĆ”i tƶƓĆÆng ,baĆ”t kyĆø luĆ¹c naĆøo ta ƱaĆ«t text giƶƵa hai daĆ”u ngoaĆ«c keĆ¹p vaĆø gaĆ¹n noĆ¹ ƱeĆ”n moƤt bieĆ”n hoaĆ«c moƤt thuoƤc tĆ­nh thƬ ta ƱaƵ taĆÆo moƤt ƱoĆ”i tƶƓĆÆng string. 1. CaĆ¹c thuoƤc tĆ­nh cuĆ»a ƱoĆ”i tƶƓĆÆng string ThuoƤc tĆ­nh length giƶƵ soĆ” kĆ­ tƶĆÆ cuĆ»a string. 2. CaĆ¹c haĆønh vi (Methods) cuĆ»a ƱoĆ”i tƶƓĆÆng string a . Anchor (nameAttribute) b . big() c . blink() d . bold() e . charAt(index) f . fixed() g . fontcolor(color) JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 23
  • 24. h . fontsize(size) i . indexOf(character,[fromIndex]) j . italics() k . lastIndexOf(character,[fromIndex]) l . link(URL) m . small() n . strike() o . sub() p . substring(startIndex,endIndex) q . sup() r . toLowerCase() s . toUpperCase() ---------------------------- BieĆ¢n soaĆÆn: PhaĆÆm phuĆ¹ TaĆøi Huu Thanh Design JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 24