Some Simple JavaScript Functions - JavaScript functions (contd.)
(Page 3 of 4 )
Using an input box to get text from the user Just like in traditional windows applications, we can use an input box to get some text input from the user. The "prompt" function is all we need:
var name = prompt("What is your name?");
document.write("Hello " + name);The prompt function accepts just one argument (the title of the input box), and returns the value entered into the text box. In the example above, we get the users name and store it in the "name" variable. We then use the "document.write" function to output their name into the browser window:
Using a message box to display text to the user We can display a message box containing an OK button. These are great when you want to let the user know what is happening during their time on a particular page. We can use a message box to display the "name" variable from our previous example:
var name = prompt("What is your name?");
alert("Your name is: " + name);The "alert" function takes one argument, which is the text to display inside of the message box. Here's the results in my browser:
Changing the title of the browser window To change the title of our web browser's window, we simply modify the "document.title" variable, like this:
document.title = "My new title";Here's how it looked in my browser:

One bad thing about the "document.title" variable is that we can only manipulate it in Microsoft Internet Explorer. Netscape's implementation of JavaScript doesn't allow us to modify it.
Next: Conclusion >>
More JavaScript Articles
More By Annette Tennison