Home arrow JavaScript arrow Page 3 - Some Simple JavaScript Functions

Some Simple JavaScript Functions

JavaScript can be one of the most useful additions to any web page. In this article, Annette shows us five quick 'n' easy ways that we can use JavaScript to add a touch of functionality to our web pages.

Author Info:
By: Annette Tennison
Rating: 4 stars4 stars4 stars4 stars4 stars / 31
January 31, 2002
  1. · Some Simple JavaScript Functions
  2. · JavaScript functions
  3. · JavaScript functions (contd.)
  4. · Conclusion

print this article

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 the prompt funciton to enter my name

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:

Using the alert function to display a message box

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:

The changed title in my web 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.
blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials