|
Departments Info Home Page Articles Software GoodiesExplanations How do they do that? The Web in FocusResources Web Site Stuff Tech Books E-Books Tech Toys Web Hosting LinksWomen Opinion Tech Women Women's StudiesTech News Computer Security Databases Java Linux MP3 PC Software Robotics Site Owner Tech Latest Web Development XMLSpread the Word Newsletter
Recommend this Page
Site Info Legal Disclaimer
Privacy
Contact
Lighter Side Crazy for Life crazy for romance Crazy for Kitties Crazy for Dogs Crazy for CowsCopyright 2000-2001, hertechnology.com |
First Program Page 2 Something unknown So far, what we have isn't terribly exciting. You could just as easily type Hello World in the text of the HTML document without bothering with a script. But a script is necessary if you want the text to change after the page is loaded or if you don't know what the text should be ahead of time. For example, you will be able to view the HTML file with either NN or IE and the script won't know when we type it in which one you use. Therefore, the code must determine which browser is being used -- we cannot just type the right browser version in the text ahead of time as we can with the fixed statement "Hello World!". JavaScript has a language statement that can determine which browser is being used -- navigator.appName. We can also identify the version with a similar statement -- navigator.appVersion. These statements work for both NN and IE. We use another document.writeln statement to print these statements to the screen. Important: What you see will not be the text navigator.appName. Instead, the browser will replace that with the name of the browser you are using. Let's add the whole writeln statement to the script.
<html> Click here to see an example. Use the browser's back button to return to this page. Create a file for the HTML and JavaScript What you need to do is create a text file called focus1.html. (Actually, you can call it anything you like, but you need to have .html on the end of the filename.) Type in the text you see above and be sure to type it in EXACTLY as you see it. Don't forget ALL the double quotes and commas!!!! (Remember, the computer is like the pickiest English teacher.) Or, if you don't like typing, copy the code on the screen and paste into your file. When you copy and paste, you will get extra spaces in your text file. You don't have to remove them because the browser will ignore extra spaces. Save your file. To quickly create a text file if you are using Windows 95/98/NT -- bring up the WINDOWS Explorer (an easy way to do that is to press the key with the window on it (next to the Ctrl key) and the letter E at the same time). Move to the directory where you want to create the file. In the right side of the Windows Explorer window, click with the right mouse button. You will get a menu. Click New, then Text Document. Now you can type in a name. Type in something like focus1.txt and hit return. Double click on the file name after it is created to open it. Paste in the code from this window. Save the file and exit Notepad. Now you can right click on focus1.txt and rename it to focus1.html. Windows will warn you about changing the file extension, but that's ok; click Yes. There is one potential gotcha here. If you do not display file extensions by default in Windows Explorer, you won't be able to change them. Windows will tack on .txt for you automatically when you try to change the name. In Windows Explorer, go to the View menu, then pick Options. You will see a little item that says "Hide file extensions for known file types". Uncheck this box. Load the HTML file in your browser Now that you have the file created, either double-click on the file to bring it up in your browser, or bring up your browser and go into the File menu. For NN, pick Open Page..., click Choose File, then select your file, click Open, then Open again. For IE, Click Open (under the File menu item), click Browse to select your file, then Open, then OK. The browser will read in the HTML file, get the script, execute it, get the browser name and version and display that after the Hello World! You can open the file with the other browser and see the different output. Congratulations! If you have followed along you have written a genuine program. If you didn't follow along, then please do so if you know how to create files. Even if you mess up, you won't do any damage with this program. The worst that can happen is your web page will be blank or you will only get part of the message on the screen. Written May 14, 2000 |
|