|
Some Useful JavaScript Tricks
|
By Mitchell Harper
[Hits: 15625]
|
|
JavaScript can be one of the most useful additions to any webpage. It comes bundled with Microsoft Internet Explorer andNetscape Navigator and it allows us to perform fieldvalidations, mouse-overs images, open popup windows, and a slewof other things.
In this article I will show you how to:
- Display the browser name and version number - Change the textin the status bar of the browser - Use an input box to get textfrom the user - Use a message box to display text to the user -Change the title of the browser window
Before that, however, we need to know how to setup our web pageso that it can run the JavaScript. JavaScript code is insertedbetween opening and closing script tags: ,like this:
These script tags can be placed anywhere on the page, howeverit's common practice to place them between the and tags. A basic HTML page that contains some JavaScriptlooks like this:
My Test Page < itle>
function testfunc() { var x = 1; }
Hello
For the examples in this article, you should use the basicdocument format I have just shown you, inserting the JavaScriptcode between the tags. When you load thepage in your browser, the JavaScript code will be executedautomatically.
----------------------------------------------- Displaying thebrowsers name and version number-----------------------------------------------
The "navigator" object in JavaScript contains the details of theusers browser, including its name and version number. We candisplay them in our browser using the document.write function:
document.write("Your browser is: " + navigator.appName);document.write(" Its version is: " + navigator.appVersion);
I run Windows 2000 and Internet Explorer version 6, so theoutput from the code above looks like this in my browser window:
Your browser is: Microsoft Internet Explorer Its version is:4.0 (compatible; MSIE 6.0b; Windows NT 5.0)
----------------------------------------------- Changing thetext in the status bar of the browser-----------------------------------------------
To change the text in the status bar of a browser window, wejust change the "status" member of the "window" object, whichrepresents our entire browser window:
window.status = "This is some text";
----------------------------------------------- Using an inputbox to get text from the user-----------------------------------------------
Just like in traditional windows applications, we can use aninput 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 theinput box), and returns the value entered into the text box. Inthe example above, we get the users name and store it in the"name" variable. We then use the "document.write" function tooutput their name into the browser window.
----------------------------------------------- Using a messagebox to display text to the user-----------------------------------------------
We can display a message box containing an OK button. These aregreat when you want to let the user know what is happeningduring their time on a particular page. We can use a message boxto 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 todisplay inside of the message box.
----------------------------------------------- Changing thetitle of the browser window-----------------------------------------------
To change the title of our web browser's window, we simplymodify the "document.title" variable, like this:
document.title = "My new title";
One bad thing about the "document.title" variable is that we canonly manipulate it in Microsoft Internet Explorer. Netscape'simplementation of JavaScript doesn't allow us to modify it.
----------------------------------------------- In Closing-----------------------------------------------
As you can see from the examples in this article, JavaScript isa powerful scripting language that we can use to enhance ourvisitors experience with our site. You shouldn't use JavaScripttoo much, however, because in some cases it can annoy yourvisitors and send them packing before your site even loads!
|
|
|
|
|
|