HWG Resources FAQs JavaScript

JavaScript FAQ

For this FAQ, prefixes that are italicized and in braces are either optional or arbitrary, depending on the situation. Have a question about this FAQ, or do you have a FAQ that you'd like for me to address? Feel free to contact me, Ryan Fischer, at <fischer@email.unc.edu>.

Table of Contents

  1. Why doesn't my image rollover work (in IE3)?
  2. How can I cause my page to break out of frames?
  3. How do I change the page of two frames at once?
  4. How do I communicate between a popup window, and the window that opened it?

  1. Why doesn't my image rollover work (in IE3)?

    In IE3's implementation of JavaScript (called JScript) the Image object/array does not exist. To get around this problem and prevent any error messages that may result from manipulation of the Image object, you need to use object detection like so:

    if(document.images){
    	// manipulate Image(s) here
    	}

    Table of Contents

  2. How can I cause my page to break out of frames?

    Place the following in the HEAD section of your HTML document:

    <SCRIPT TYPE="text/javascript">
    <!--
    if(self != top) top.location = self.location;
    //-->
    </SCRIPT>

    To accomodate for browsers that don't support JavaScript or have scripting disabled, you can place the following HTML in the HEAD section of your HTML document so that when a link is clicked, it will clear the frames.

    <BASE TARGET='_top'>

    Table of Contents

  3. How do I change the page of two frames at once?

    There are so many different ways to do this; I will just discuss the general syntax. What you need to do is, via the ONCLICK event handler of a link, call a script that changes the non-essential frame, while referencing the essential frame via a link.

    <SCRIPT TYPE="text/javascript">
    <!--
    function change(uri){
    parent.myFrame.location.href = uri;
    }
    //-->
    </SCRIPT>
    
    <A ONCLICK="change('bar.html');" HREF="foo.html">Link</A>
    

    myFrame is the name of the frame which you want to change. I say it should be non-essential because not all browsers support JavaScript and you should consider that when designing your site. bar.html is the name of the file you want to be displayed in the frame. As the script is set up right now, when the link is clicked, the frame the link is in will change. This can be altered via the TARGET attribute of the A tag.

    <A HREF="foo.html" TARGET='myOtherFrame'>

    Table of Contents

  4. How do I communicate between a popup window, and the window that opened it?

    Any time you spawn a new window, in order to "talk" to the old window (from the new window) you MUST name it! So, in the old window, you want to add this:

    <SCRIPT TYPE="text/javascript">
    <!--
    window.name='main';
    //-->
    </SCRIPT>

    Then in the new window, you can reference the old one with a prefix that is the name of the old window. In this case:

    main.[property_or_method];

    property_or_method is a property or method of the window object.

    Another option is to use the opener property of the window object. opener directly references the window the current window was opened with. This eliminates the need to rename your windows, but it isn't as reliable, as it is only existant in browsers that support JavaScript 1.1 or greater. For instance:

    [window.]opener.location.href = 'foo.html';

    ...will change the page displayed in the window that was opened by the window where this script resides to 'foo.html'.

    Table of Contents


[Valid HTML 4.0!]
This page is maintained by fischer@email.unc.edu. Last updated on 23 October 1998.
Copyright © 1998 by the HTML Writers Guild, Inc.