Some Useful JavaScript Tricks

Feb 12
22:00

2002

Mitchell Harper

Mitchell Harper

  • Share this article on Facebook
  • Share this article on Twitter
  • Share this article on Linkedin

... can be one of the most useful ... to any webpage. It comes bundled with ... Internet Explorer ... ... and it allows us to perform field ... ima

mediaimage

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-overs images,Some Useful JavaScript Tricks Articles open popup windows, and a slew of other
things.

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

Before that, however, 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:



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 < itle><br><script language="JavaScript"><br><br> function testfunc()<br> {<br> var x = 1;<br> }<br><br></script><br></head><br><body><br> <h1>Hello</h1><br></body><br></html><br><br>For the examples in this article, you should use the basic<br>document format I have just shown you, inserting the JavaScript<br>code between the <script> and </script> tags. When you load the<br>page in your browser, the JavaScript code will be executed<br>automatically.<br><br>-----------------------------------------------<br>Displaying the browsers name and version number<br>-----------------------------------------------<br><br>The "navigator" object in JavaScript contains the details of the<br>users browser, including its name and version number. We can<br>display them in our browser using the document.write function:<br><br> document.write("Your browser is: " + navigator.appName);<br> document.write("<br>Its version is: " + navigator.appVersion);<br><br>I run Windows 2000 and Internet Explorer version 6, so the output<br>from the code above looks like this in my browser window:<br><br> Your browser is: Microsoft Internet Explorer<br> Its version is: 4.0 (compatible; MSIE 6.0b; Windows NT 5.0)<br><br>-----------------------------------------------<br>Changing the text in the status bar of the browser<br>-----------------------------------------------<br><br>To change the text in the status bar of a browser window, we just<br>change the "status" member of the "window" object, which<br>represents our entire browser window:<br><br> window.status = "This is some text";<br><br>-----------------------------------------------<br>Using an input box to get text from the user<br>-----------------------------------------------<br><br>Just like in traditional windows applications, we can use an<br>input box to get some text input from the user. The "prompt"<br>function is all we need:<br><br> var name = prompt("What is your name?");<br> document.write("Hello " + name);<br><br>The prompt function accepts just one argument (the title of the<br>input box), and returns the value entered into the text box. In<br>the example above, we get the users name and store it in the<br>"name" variable. We then use the "document.write" function to<br>output their name into the browser window.<br><br>-----------------------------------------------<br>Using a message box to display text to the user<br>-----------------------------------------------<br><br>We can display a message box containing an OK button. These<br>are great when you want to let the user know what is happening<br>during their time on a particular page. We can use a message box<br>to display the "name" variable from our previous example:<br><br> var name = prompt("What is your name?");<br> alert("Your name is: " + name);<br><br>The "alert" function takes one argument, which is the text to<br>display inside of the message box.<br><br>-----------------------------------------------<br>Changing the title of the browser window<br>-----------------------------------------------<br><br>To change the title of our web browser's window, we simply modify<br>the "document.title" variable, like this:<br><br> document.title = "My new title";<br><br>One bad thing about the "document.title" variable is that we can<br>only manipulate it in Microsoft Internet Explorer. Netscape's<br>implementation of JavaScript doesn't allow us to modify it.<br><br>-----------------------------------------------<br>In Closing<br>-----------------------------------------------<br><br>As you can see from the examples in this article, JavaScript is a<br>powerful scripting language that we can use to enhance our<br>visitors experience with our site. You shouldn't use JavaScript<br>too much, however, because in some cases it can annoy your<br>visitors and send them packing before your site even loads!