Introduction
A TypeScript promt window ("prompt box") is used to obtain a value from the user. You can use this to prompt a user for their password before continuing with a secure action.
A Prompt Box also has "Ok" and "Cancel" buttons. If the user clicks on the "Ok" button the window method prompt() will return the value from the textbox entered by the user. If the user clicks on the "Cancel" button then the window method prompt() returns null.
The TypeScript Prompt Box takes two parameters. The first parameter is the message you want the user to read describing the value they are to supply, and the second parameter is an optional default value that will be pre-filled into the text field of the prompt box.
The following example shows how to use and show a prompt box in TypeScript.
Step 1
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. In this window, click "HTML Application for TypeScript" under Visual C#.
Give the name of your application as "Promt_Box" and then click "Ok".
Step 2
After this session the project has been created; A new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file and css files.
Coding
PromptBox.ts
class Prompt_Box
{
show()
{
var promptValue = prompt('Enter any value in below','');
if (promptValue != null) {
document.getElementById("Status").innerHTML = "Prompt Box value ->" + promptValue;
}
else
{
document.getElementById("Status").innerText = "You cancel Prompt Box";
}
}
}
window.onload = () =>
{
var bttn = <HTMLButtonElement>document.getElementById("Button1");
bttn.onclick = function ()
{
var obj = new Prompt_Box();
obj.show();
}
}; |
PromptBox_Demo.htm
<!DOCTYPEhtml>
<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">
<head>
<metacharset="utf-8"/>
<title>TypeScript HTML App</title>
<linkrel="stylesheet"href="app.css"type="text/css"/>
<scriptsrc="app.js"></script>
</head>
<body>
<h2>Prompt Box in TypeScript</h2>
<divstyle="color:#0026ff"id="Status"/>
<inputid="Button1"type="button"value="Click For Enter Value"/>
</body>
</html> |
app.js
var Prompt_Box = (function () {
function Prompt_Box() { }
Prompt_Box.prototype.show = function () {
var promptValue = prompt('Enter any value in below','');
if(promptValue != null) {
document.getElementById("Status").innerHTML = "Prompt Box value ->" + promptValue;
} else {
document.getElementById("Status").innerText = "You cancel Prompt Box";
}
};
return Prompt_Box;
})();
window.onload = function () {
var bttn = document.getElementById("Button1");
bttn.onclick = function () {
var obj = new Prompt_Box();
obj.show();
};
};
//@ sourceMappingURL=app.js.map |
Output 1
Click on the button:
Output 2
Enter the value, then click on "Ok":
Output 3
If the prompt window is canceled:
Reference By
http://www.typescriptlang.org/