The prompt() method

The prompt() method is used to prompt viewer to enter information. You can use the information the viewer enters in the prompt dialog box to do different things since the information the viewer enters is assigned to a variable.

var YourName = window.prompt("Your text" , "Default entry");

For example, you can use this method to get the viewer’s name and send an alert while the page is loading.

<body>

<div id="greeting">
<h1>Hello and welcome to my page!</h1>
</div>

<div id="content_page">
This page is about JavaScript.
</div>

<script type="text/javascript" >
var greetMe = document.getElementById("greeting");
var YourName = window.prompt("What is your name?" , "");

if (YourName.length < 1) {
	YourName = "Anonymous";
}

greetMe.innerHTML = "<h1>Hello " + YourName + "! Welcome to my page!</h1>"
</script>

</body>

Leave a Reply