Home / Web Builder Channel / Untangled Web
 LOOK FOR...   WITH KEYWORDS:  

Consumer Watch
On The Money
Career Track
Health Quest
Business
Small Office
Web Builder
Marketing
Classifieds
Credit & Debt
Biz Finance
IR Journal
Legal Forms
Letter Templates
Archives
HOME

S U B S C R I B E

Good To Know

What You Need To Know To Register Your Domain Name & Choose A Web Host
Building Your Own Banners!

Monday, December 06, 2004

Some Simple JavaScript Functions

JavaScript can be one of the most useful additions to any web page. It comes bundled with Microsoft Internet Explorer and Netscape Navigator and it allows us to perform field validations, mouse-over images, open popup windows, and a slew of other things.

If you have any experience with C, C++, PHP or Java, then you'll be glad to know that the syntax of JavaScript is very similar.

In this article I will show you how to:

  • Display the browser name and version number
  • Change the text in the status bar of the browser
  • Use an input box to get text from the user
  • Use a message box to display text to the user
  • Change the title of the browser window

JavaScript Functions

Before we can implement the functions i'm about to describe, we need to know how to setup our web page so that it can run the JavaScript. JavaScript code is inserted between opening and closing script tags like this:

<

// JavaScript code goes here

These script tags can be placed anywhere on the page, however it's common practice to place them between the and tags. A basic HTML page that contains some JavaScript looks like this:

My Test Page

function testfunc()

{

var x = 1;

}

Hello

For the examples in this article, you should use the basic document format I have just shown you, inserting the JavaScript code between the and tags. When you load the page in your browser, the JavaScript code will be executed automatically.

Displaying the browsers name and version number: The "navigator" object in JavaScript contains the details of the users browser, including its name and version number. We can display them in our browser using the document.write function:

document.write("Your browser is: " + navigator.appName); document.write("
Its version is: " + navigator.appVersion);
"

Changing the text in the status bar of the browser: To change the text in the status bar of a browser window, we just change the "status" member of the "window" object, which represents our entire browser window:

window.status = "This is some text";

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.

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";

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.

Conclusion

As you can see from the examples in this article, JavaScript is a powerful client-side scripting language that we can use to enhance our visitors experience with our site. You shouldn't use JavaScript too much, however, because in some cases it can annoy your visitors and send them packing before your site even loads!

Mitchell Harper is part of the development team that created SiteWorks Professional. SiteWorks is a web site and admin suite that allows you to run your own content driven web site in under 5 minutes.
Full Author Profile -->


PRINT THIS
RECENT STORIES
Some Simple JavaScript Functions
Stratospheric Broadband Coming Soon
Creating Your Own Web Site Free
Meta Tags Myths
Testing Your Web Application - Part 2 of 2
Business Credit
The Layperson's Crash Course in Business Credit
Street-Smart Financing
How to Start or Expand Your Business with Street-Smart Financing
Attract the Perfect Investor
How to Attract the Perfect Investor for Your Business
Federal Help For Your Business
How to Obtain Local, State and Federal Help For Your Business

 

 

InsiderReports

Home  | Affiliate Login  | Search  | Advertise  | Classifieds  | Contact Us  | About Us  | Index
 

The Horizons Unlimited Group

Copyright © 1996-2010 Horizons Unlimited Group. All Rights Reserved.     Privacy Policy | Terms of Use
 


Click to verify BBB accreditation and to see a BBB report.