![]() |
|
|
|
|
|
|||
![]() |
|
|
|
In this article I will show you how to:
- Display the browser name and version number
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: <script> and </script>, like this: <script language="JavaScript"> --> JavaScript code goes here <-- </script> These script tags can be placed anywhere on the page, however, it's common practice to place them between the <head>and </head> tags. A basic HTML page that contains some JavaScript looks like this:
<html>
function testfunc()
</script> For the examples in this article, you should use the basic document format I have just shown you, inserting the JavaScript code between the <script> and </script>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 user's browser, including its name and version number. They can be displayed in a browser using the document.write function:
document.write("Your browser is: " + navigator.appName); I run Windows 2000 and Internet Explorer version 6, so the output from the code above looks like this in my browser window:
Your browser is: Microsoft Internet Explorer
To change the text in the status bar of a browser window, just change the "status" member of the "window" object, which represents the 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, you can use an input box to get some text input from the user. The "prompt" function is all you need:
var name = prompt("What is your 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, you get the users name and store it in the "name" variable. You then use the "document.write" function to output their name into the browser window. Using a message box to display text to the user You 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. You can use a message box to display the "name" variable from our previous example:
var name = prompt("What is your 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 a web browser's window, simply modify the "document.title" variable, like this: document.title = "My new title"; One bad thing about the "document.title" variable is that it can only be manipulated in Microsoft Internet Explorer. Netscape's implementation of JavaScript doesn't allow for modification. In Closing As you can see from the examples in this article, JavaScript is a powerful scripting language that can be used to enhance a visitor's experience with our site. However, you shouldn't use JavaScript too much because in some cases it can annoy visitors and send them packing before your site even loads!
About The Author
Psychological Triggers
The Insider's Guide To Dominating the Search Engines
EzineAnnouncer - The Ultimate Ezine Promotion Tool
|
|
|
|
|
| SiteProNews now reaches over 6 million Subscribers | |
|
|