Jim Hanson's | ![]() |
Here are a few examples of JavaScript that show application of JavaScript object features. The samples used here can be used as building blocks for more complex JavaScript implementations.
|
Using JavaScript to Work With Cookies
The above text (showing the viewer's browser name and version) was created with the following JavaScript (embedded in the HTML at the point it was displayed):
<SCRIPT LANGUAGE="JavaScript"> <!--Hide JavaScript from Java-Impaired Browsers document.write("<B>"+navigator.appName+" " + navigator.appVersion + "</B><P>"); // End Hiding --> </SCRIPT>The appName property is just the browser name. The appVersion property includes the browser version number and the operating system ID as well.
The JavaScript input box that popped up when you entered this page created the name after Welcome below. That name will be reread from the cookie each subsequent time you enter this page.
Insert following code in your document, and you will have the welcome message (in orange) in the viewers browser.
<SCRIPT LANGUAGE="JavaScript">
<!--Hide JavaScript from Java-Impaired Browsers
welcome();
// -->
</SCRIPT>
Here is the Welcome JAVASCRIPT Source for this example. The JavaScript source for this sample is all placed in the HEAD section of this page (as you can see in the source) to insure it will be loaded before the BODY code tries to use it.
By the way, if you try this JavaScript, you will find that the pop-up asking for your name will only come up only once, the first time the JavaScript is executed.
A solution to this (for example, while you are testing the message in the pop-up) is to get a tool like ZDNet's Cookiemaster.
This FREE Windows 95 program will allow you to delete the cookies you create locally so you can re-test the "first time" logic on your page (works for both Netscape and IE).
Using JavaScript to Change Images in Links
onMouseOver is a predefined JavaScript event handler that allows you to change the image in an A HREF= tag. This is an example of using JavaScript in the BODY of your HTML document (which you can do because you don't have to worry about waiting for the function to load). The predefined functions are already part of the browser.
Note: This feature will only work for Netscape 3+ and Internet Explorer 4+. The earlier versions of the browsers will ignore the onMouseOver effect and will only show the first image (the one in the IMG tag).
Here is an example of onMouseOver that changes images that you can use.
Another use of onMouseOver can be to automatically take a user to a new page without having to select the image. Here is the automatic link example.
Using JavaScript to Create New Browser Windows
Normally when a new window is desired, the TARGET="_blank" parameter is included in the <A HREF= ...> tag. But this will open a window of the same size as the current window, obscuring it. JavaScript allows creation of a new window that can be smaller than the original, if desired. See the JavaScript New Browser Window Example for details on this.
Using JavaScript to automatically break your page out of a frame.
The JavaScript object property frames.length tells how many frames are on the current page. By interrogating this count, which is equal to 0 for a page without frames, you can force a reload of your page. If you look at the source of my index.html page, you can view source and see the following JavaScript:
<SCRIPT LANGUAGE="JavaScript">
setTimeout ("changePage()", 3000);
function changePage() {
// end hiding contents -->
<!-- Hide script from older browsers
if (self.parent.frames.length != 0)
self.parent.location='index.html';
}
</SCRIPT>
NOTE: A new browser window was opened for viewing these pages. To return, you can just select another browser (from the task bar in Windows 95 or click your mouse somewhere on another window). You can also close (or File, Exit) from this window when done, leaving only the calling window. |
These samples came from various sources, where you can go to find lots more of the same. These include:
![]() Virtual_Max's Cafe |
![]() cutNpaste JavaScript |
![]() WebSite Abstraction |
Member of the Internet Link Exchange | Free Home Pages at GeoCities |