JavaScript ืืกืคืงืช ืคืื ืงืฆืืืช ืืืื ืืช ืคืฉืืืืช ืฉืืืคืฉืจืืช ืืืคืชืืื ืืชืงืฉืจ ืขื ืืืฉืชืืฉืื ืืจื ืชืืืืช ืืืืืื ืืืคืืคื.
ืคืื ืงืฆืืืช ืืื ืืืืืืืืืช ืืืืืื ืฉื ืคืขืืืืช ืงืื/ืคืื ืืกืืกืืืช, ืื ืืชื ืืืฉืชืืฉ ืืื ืืืืืื ืืคืืืงืฆืืืช ืืื ืืจื ื ืืฆืืจื ืืชืจืืืช, ืืืฉืืจืื ืื ืงืืืช ืืืืข ืืืืฉืชืืฉ.
ืชืืืืช ืืืืืืื ืขืืฆืจืืช ืืช ืืืฆืืข ืืกืงืจืืคื ืขื ืฉืืืฉืชืืฉ ืกืืืจ ืืืชื. ืืืืืจ ืืืืื ืืช ืืืืืืืื (Modal Windows).
1. ()alert – ืืฆืืช ืืืืขื
ืืคืื ืงืฆืื alert() ืืฆืืื ืืืืขื ืคืฉืืื ืืืฉืชืืฉ. ืืจืื ืืฉืชืืฉืื ืื ืืฆืืจืื ืืืืงื ืื ืืฆืืช ืืืืขืืช ืืืืจืืช.
alert("Welcome to the JavaScript interaction tutorial!");
ืืืฉืจ ืืงืื ืืื ืืจืืฅ, ืืืคืืคื ืืฆืื ืืืืขื ืงืืคืฆืช ืขื ืืคืชืืจ ืืืฉืืจ (OK).
ืืืค: ืืืื ืขื ืืฉืืืืฉ ื-
alert()ื production. ืืฉืชืืฉื ืื ืืขืืงืจ ืืฆืืจืื ืืืืงืืช ืืืืืื.
2. ()prompt – ืงืืืช ืงืื ืืืืฉืชืืฉ
ืืคืื ืงืฆืื prompt() ืืืงืฉืช ืืืืฉืชืืฉ ืืืืื ืขืจื ืืืืืืจื ืืช ืืงืื ืืืืจืืืช. ืื ืืืฉืชืืฉ ืืืื, ืืคืื ืงืฆืื ืืืืืจื null.
const name = prompt("What is your name?");
alert("Hello, " + name + "!");
ืงืื ืื ืืืงืฉ ืืืืฉืชืืฉ ืืช ืฉืื, ืืืฆืื ืื ืืืืขืช ืืจืื ืขื ืืขืจื ืฉืืืื.
ืืืืื ืขื ืขืจื ืืจืืจืช ืืืื
const color = prompt("What's your favorite color?", "Blue");
console.log("User's favorite color is:", color);
ืืืคืื ืืขืจื ืืืืืืจ
ืืฉืื ืืืืืจ ืฉ-prompt() ืชืืื ืืืืืจื ืืืจืืืช (string) – ืื ืืฉืืืฉืชืืฉ ืืงืืื ืืกืคืจ. ืื ืืืฉืชืืฉ ืืืืฅ ืขื Cancel, ืืคืื ืงืฆืื ืืืืืจื null, ืฉืื ืฉืื ื ืืืืจืืืช ืจืืงื (ืืืืฆื ืขื OK ืืื ืืืงืืื ืืืื).
const age = prompt("How old are you?");
if (age === null) {
console.log("User cancelled the dialog.");
} else if (age === "") {
console.log("User clicked OK but entered nothing.");
} else {
const ageNum = Number(age);
console.log("User's age as a number:", ageNum);
}
ืืืืืื ืฉ-prompt() ืืืืืจื ืืืจืืืช, ืขืืืื ืืืฉืชืืฉ ื-Number(), parseInt() ืื parseFloat() ืืื ืืืืืจ ืืช ืืขืจื ืืฉืืชื ืืฆืคืื ืืงืื ืืกืคืจื.
3. ()confirm – ืืงืฉืช ืืืฉืืจ
ืืคืื ืงืฆืื confirm() ืืฆืืื ืืืื ืขื ืืคืชืืจื ืืืฉืืจ ืืืืืื. ืืื ืืืืืจื true ืื ืืืฉืชืืฉ ืืืฉืจ, ืื false ืื ืืืจ ืืืื.
const isSure = confirm("Are you sure you want to delete this item?");
if (isSure) {
console.log("Item deleted.");
} else {
console.log("Action cancelled.");
}
ืืืืื ืืืขืืื ืืืืืชื: ืฉืืืื ืฉื ืฉืืืฉ ืืคืื ืงืฆืืืช
ืืืืืื ืืื ื ืขืฉื ืฉืืืืฉ ื-alert, prompt ื-confirm ืืื ืืื ืืืืืช ืชืืืื ืคืฉืื ืฉื ืืจืฉืื ืื ืืืืืืจ. ืฉืืื ืื ืืฉืืืืฉ ื-template literals (`${}`) ืืืงืื ืฉืจืฉืืจ ืืืจืืืืช ืืงืื ื ืงื ืืืชืจ.
alert("Welcome to our website!");
const user = prompt("What's your username?");
if (user !== null && user !== "") {
alert(`Hello, ${user}!`);
const wantsNewsletter = confirm("Would you like to subscribe to our newsletter?");
if (wantsNewsletter) {
alert("Thank you for subscribing!");
} else {
alert("No problem. Enjoy your visit!");
}
} else {
alert("You need to enter a username to continue.");
}
ืืขืจืืช ืืฉืืืืช
- ืืฉืืืืช ืืืื ืขืืฆืจืืช ืืช ืืืฉืง ืืืฉืชืืฉ ืืืคืืคื ืขื ืฉืืืฉืชืืฉ ืืืื. ืฉืื ืงืื JavaScript ืืืจ ืื ืืจืืฅ ืื ืขืื ืืืืืืื ืคืชืื.
- ืื ื ืืชื ืืืชืืื ืืช ืืขืืฆืื ืฉืืื. ืืืืืืืืื ืืขืืฆืืื, ืืฉืชืืฉื ืืืืืืืื ืืืชืืืื ืืืฉืืช ืฉื ืื ืื ืขื HTML, CSS ื-JavaScript.
- ืจืื ืืืคืืคื ืื ืืืืืจื ืืื ืืืคืฉืจืื ืืืฉืชืืฉืื ืืืกืื ืชืืืืช ืืืืืื ืืืืจืืช. ืื ืืงืื ืฉืืื ืืคืขืื ืืจืื alerts ืืจืฆืฃ, ืืืคืืคื ืขืฉืื ืืืฆืืข ืชืืืช ืกืืืื "prevent this page from creating additional dialogs".
- ืฉืืืฉ ืืคืื ืงืฆืืืช ืื methods ืฉื ืืืืืืืงื
window. ืืชืืืชalert()ืืื ืงืืฆืืจ ืฉืwindow.alert().
ืชืืืืืช ืืืคืืคื ืื: ืื ืืืคืืคื ืื ืืืืืจื ืืื ืชืืืืื ื-
alert,promptื-confirm. ืขื ืืืช, ืื ืขืฉืืืืช ืืืืืช ืืืืืืืช ืืืงืฉืจืื ืืกืืืืื ืืื iframes ื-origin ืฉืื ื ืื service workers.
ืฉืืืืช ื ืคืืฆืืช
alert() ืืฆืืื ืืืืขื ืขื ืืคืชืืจ OK. prompt() ืืืงืฉืช ืืืืฉืชืืฉ ืงืื ืืงืกื ืืืืืืจื ืืืจืืืช (ืื null ืื ืืืื). confirm() ืฉืืืืช ืฉืืืช ืื/ืื ืืืืืืจื true (OK) ืื false (Cancel).
prompt() ืืืืืจื null ืืฉืืืฉืชืืฉ ืืืืฅ ืขื Cancel. ืื ืฉืื ื ืืืืจืืืช ืจืืงื (""), ืฉืืืืืจืช ืืฉืืืฉืชืืฉ ืืืืฅ ืขื OK ืืื ืืืงืืื ืืืื. ืชืืื ืืืงื null ืืคื ื ืขืืืื ืืชืืฆืื.
prompt() ืชืืื ืืืืืจื ืืช ืืงืื ืืืืจืืืช. ืื ืืงืืืืื "42", ืืื ืืืืืจื ืืช ืืืืจืืืช "42" ืืื ืืช ืืืกืคืจ 42. ืืฉืชืืฉื ื-Number(), parseInt() ืื parseFloat() ืืื ืืืืืจ ืืช ืืขืจื ืืฉืฆืจืื ืืกืคืจ ืืืืฉืืืื.
<dialog> ืฉื HTML.
ืกืืืื
ืฉืืืืฉ ืืคืื ืงืฆืืืช alert(), prompt() ื-confirm() ืืืคืฉืจ ืืื ืืืืกืืฃ ืืื ืืจืืงืฆืื ืืืืจื ืืื ืืืฉืชืืฉ ืืืฃ ืืืื ืืื ืืช ืืืฉืง ืืกืืื. ืืืจืืช ืืคืฉืืืช, ืื ืืืืืช ืืกืืก ืืฉืื ืืืื ืช ื ืืืื ืงืื ืืืืฉืชืืฉ.
ืืื ืฉืชืชืงืืื, ืชืขืืจื ืืืืืืืื ืืืชืืืื ืืืฉืืช ืขื HTML, CSS ื-JavaScript ืืืืืืืช ืืฉืชืืฉ ืืืื ืืืชืจ. ืืื ื ืฉื ืื ืืคืืืฆืืืช DOM ืืฉื ืคืื ืงืฆืืืช ื-JavaScript ืชืขืืืจ ืืื ืืื ืืช ืืช ืืคืชืจืื ืืช ืืืืชืืืื ืืืื.