JavaScript yog deb ntawm cov lus scripting uas nrov tshaj plaws rau cov nplooj ntawv web thiab de facto cov qauv rau lub vev xaib. Txog hnub tim, ntau tus naj npawb ntawm JavaScript scripts tau tsim, feem ntau ntawm cov dawb thiab dawb. Ua tsaug rau qhov no, txhua tus webmaster tuaj yeem nrhiav cov ntawv sau uas haum rau nws cov kev xav tau tam sim no. Thiab txhua yam nws xav paub yog yuav ua li cas txuas cov ntawv Java rau lub vev xaib.
Tsim nyog
muaj peev xwm los hloov cov ntawv cim
Cov Lus Qhia
Kauj ruam 1
Xws li cov ntawv sau los ntawm lwm qhov chaw sab nraud, hais los ntawm URI uas paub zoo, rau daim ntawv. Siv SCRIPT HTML yam nrog tus nqi kev ntaus cim meej. Kho cov ntaub ntawv los ntawm kev ntxiv kev tsim kho ntawm daim ntawv rau nws:
Ntawm no, tus nqi ntawm script_URI yuav tsum yog URI uas txheeb xyuas cov khoom siv los ntawm cov ntawv sau cov ntawv sau.
Cov cim kev hais lus ntawm tsab ntawv yuav tsum tau sau tseg raws li tus nqi ntawm lub cim xeeb cwj pwm, yog tias nws txawv ntawm cov cim encoding ntawm daim ntawv. Lub encoding ntawm daim ntawv yog txiav txim siab raws li cov ntaub ntawv hauv Cov Lus Qhia-yam teb ntawm HTTP teb header ntawm tus neeg rau zaub mov lossis tus nqi ntawm cov ntsiab lus ntaus cim ntawm META keeb nrog kev ntaus nqi ntawm http-equiv teeb tsa rau Content-Type.
Siv cov ntawv sau rau hauv txoj kev no feem ntau ua rau hauv cov ntawv sau (Cov ntsiab lus SCRIPT muaj nyob sab hauv HEAD lub caij) thiab lawv yuav tsum tau ua tiav tam sim ntawd. Nyob rau hauv cov ntaub ntawv no, nws ua rau kev nkag siab siv tus cwj pwm defer rau kev txhais lub ntsiab lus ntawm cov ntawv sau.
Kauj ruam 2
Xws li cov ntawv los ntawm kev nqus nws ncaj qha rau hauv cov ntawv. Ntxiv SCRIPT caij rau koj daim ntawv sau cim uas muaj cov ntawv JavaScript. Siv ib qhov kev tsim kho xws li:
// tsab ntawv cim kab lus
// tsab ntawv cim kab lus
// tsab ntawv cim kab lus
Ua tib zoo saib rau HTML cov lus nyob ib puag ncig tus ntawv code. Lawv xav tau rau kev sib xyaw nrog cov qauv browser qub.
Kauj ruam 3
Xws li cov ntawv sau los ntawm kev qhia "javascript:" raws li tus qauv piav qhia URI ntawm lub hom phiaj thauj tog rau nkoj ntawm cov khoom A. Tsim kev txuas rau hauv cov ntaub ntawv nrog tus nqi ntawm href attribute ntawm daim ntawv:
javascript:
Ntawm no, tus nqi yuav tsum yog tus xaj kev sau hauv JavaScript. Hauv qhov xwm txheej no, ob peb kab lus ntawm cov lus tuaj yeem ua ke rau hauv ib qho kev hais tawm uas siv tus neeg siv khoom ua ke, piv txwv:
ntawv nyeem
Thaum xws li qhov txuas tau qhib rau hauv txhua txoj kev (tus neeg siv lossis programmatically), qhov kos tsab ntawv code yuav raug tua.
Kauj ruam 4
Ntxiv cov ntawv sau rau koj cov ntawv ua cov cai rau inline element event handlers. Ua li no, txhais cov tsim teeb meem tsim rau cov xwm txheej xav tau ntawm cov khoom raug xaiv los ntawm kev ntxiv cov cwj pwm tsim nyog rau cov hauv no. Sau JavaScript snippets rau tus cwj pwm muaj nuj nqis. Piv txwv li, koj tuaj yeem ntxiv cov cai ua tus tes tuav rau lub nias nkaus xwb ntawm DIV caij zoo li no:
Cov ntsiab lus ntsiab lus
Rau ib daim ntawv teev cov txheej xwm uas yuav tsum tau txais kev txhawb nqa los ntawm cov neeg siv khoom siv tau sib txawv, saib Cov Txheej Txheem Txheej ntawm Theem 2 thiab 3 DOM Cov Khoom Siv (DOM2 thiab DOM3) ntawm W3C's w3c.org lub vev xaib.