logo

Tổng quan - ngôn ngữ lập trình Javascript

Tổng quan ngôn ngữ lập trình kịch bản Java script.
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: JavaScript Program 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: JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 2 8. Söû duïng taäp tin JavaScript beân ngoaøi : 9. Theâm chöông trình vaøo taäp tin HTML: Listing 2.1 Here is result: 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"); 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: Outputting Text document.write("This is text bold "); - -> This is text plain Ví duï 2: document.write(''); Example 2.4 document.write("WELCOME TO NETSCAPE 2.1"); - -> 5. Söû duïng phöông phaùp writeln() vôùi theû PRE: document.writeln("Two,"); document.write("Three"); Outputting Text document.write("..."); - -> 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ï: alert("Welcome to Netscape Navigator 21"); Example 2.5 document.write(''); - -> 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: document.write("Your favorite color is:"); Listing 2.6 document.writeln(prompt("enter your favorite color:","Blue")); - -> Ví duï 2: document.write("Greeting ,"); document.writeln(prompt("enter your Listing 2.6 name:","name")); document.write("Welcome to netscape navigator 2.01 "); - -> document.write(''); Söû duïng daáu + ñeå coäng 2 chuoåi ñôn laïi: Ví duï 3: document.write("Greeting ," + prompt("enter your name:","name") + " Listing 2.6 Welcome to netscape navigator 2.01 "); - -> document.write(''); 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 Example 3.1 Ví du 2: taïo laïi moät giaù trò môùi cho bieán Example 3.2 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: * 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 true !x • Toaùn töû so saùnh trong JavaScript: == 1==1 => true != 3false > 5 >=4 =>true < “the” != “he” => true >= 4==”4” =>true (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 var output = (response==answer) ? correct:incorrect; Example 3.3 var question="What is 10+10 ?"; var answer=20; var correct=' SRC="incorect.gif">'; var response=prompt(question,"0"); 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 response=prompt(question,"0"); } Example 3.3 var output = (response ==answer ) ? correct:incorrect ; var question="What is 10+10 ?"; var answer=20; var correct=''; var incorrect=''; if (response != answer) { if (confirm("Wrong ! press OK for a second change")) Ví duï 2 : Söû duïng phöông phaùp confirm() vôùi phaùt bieåu if - else question="What is 10*10"; answer=100; Example 3.3 response=prompt(question,"0"); } var question="What is 10+10 ?"; } var answer=20; var correct=''; correct:incorrect ; var incorrect=''; var response=prompt(question,"0"); if (response != answer) { if (confirm("Wrong ! press OK for }else { if (confirm("Correct ! press OK for a second question")) 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(“Your Name is ”); document.write(name); document.write(“”); } 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 //CHECK THE RESULT return (response == answer) ? correct Example 4.1 : incorrect; } SRC=”incorrect.gif”>’; //ASK THE QUESTION var response=prompt(output,”0"); 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ï: //CHECK THE RESULT return (response == answer) ? correct Example 4.2 : testQuestion(question); } SRC=”incorrect.gif”>’; //ASK THE QUESTION var response=prompt(output,”0"); Ví duï 2: //DEFINE LOCAL VARIABLES FOR THE FUNCTION Example 4.2 var answer=eval(question); var output=”What is “ + question + var incorrect=’’; > 4 //ASK THE QUESTION var response=prompt(output,”0"); //CHECK THE RESULT if (chances > 1) { : incorrect; } } 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 + “”); document.write(“Age: “ + this.age + “”); document.write(“Mother’s Name: “ + this.mother + “”); document.write(“Math Grade: “ + this.grade.math + “”); document.write(“English Grade: “ + this.grade.english + “”); document.write(“Science Grade: “ + this.grade.science + “”); } 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 function displayInfo() { document.write(“Employee Profile: Example 4.3 “ + this.name + “”); document.writeln(“Employee Number: “ document.writeln(“Social Security // STOP HIDING FROM OTHER BROWSERS -- Number: “ + this.socsec); > document.writeln(“Annual Salary: “ + this.salary); document.write(“”); } //DEFINE OBJECT 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) month = "thaù ng naê m, "; Trong phaàn body baïn coù theå xuaát ra daïng nhö sau: document.write("" + ampmhour + "" + myminutes + ampm) document.write(" - " + day + " ngaø y " + myweekday +" "); document.write( month + " , naê m " + year + ""); 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: Ví duï: Ví duï: 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 Example 5.1 MTWRFSS Vi du: Example 5.1 Vi du MTWRFSS JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 17 Example 5.1 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ï: 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ï: expression”,””); calculate(form); Example 5.3 } //STOP HIDING FROM OTHER BROWSERS --> The result of this expression is: 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ï: for loop Examle document.write(i + " " + prompt('Enter food number ' + i, 'food' ) + ''); } - -> 2 . Voøng laëp while : Cuù phaùp: While ( ñieàu kieän) { leänh JavaScript ; } Ví duï: var num=1; while(num
DMCA.com Protection Status Copyright by webtailieu.net