The <FRAME> ElementLike the <FRAMESET> element, the <FRAME> element, which you use to create individual frames , shares JavaScript's core HTML properties, methods , and events that we covered in Chapters 5 and 6. And like the <FRAMESET> element, the <FRAME> element also has some additional properties beyond the core ones, and you can see them in overview in Table 7.6, and in detail in Table 7.7. We'll see methods and events for this element in the next chapter. Table 7.6. Overview of the Properties of the <FRAME> Element (See Chapter 5 and 6 for the JavaScript core HTML properties, methods, and events that also apply to this element.)
Table 7.7. The Properties of the <FRAME> Element (See Chapter 5 and 6 for the JavaScript core HTML properties, methods, and events that also apply to this element.)
As you can see in Table 7.7, you have a lot of control over what happens in a frame using JavaScript. Here's one thing to note:You can use the src property to set the source URL for the frame's document, but this JavaScript property is still relatively new, and it's probably a better idea to use the location.href property for compatibility with old browsers. Using Frame PropertiesWhen you're working with frames, it's important to know your way aroundwhen you're in a <FRAMESET> window, for example, how do you access the child frames using JavaScript? When you're in a frame, how do you access the parent <FRAMESET> window? Here are the window object properties that enable you to move from frame to parent and back again:
If you're in one frame, for example, how do you refer to the document in another frame? You can use syntax like this: parent.frames[2].document . What about reading the name of the topmost window? You can use top.name . How about the value of a text field in a child frame? You can use syntax like frames[1].document.form1.text1.value . Here's an example that takes a look at the properties in the preceding list in a browser (note that I'm explicitly setting the name property of the top window hereotherwise, the name property of that window would be empty): (Listing 07-11.html on the web site) <HTML> <HEAD> <TITLE>Frame Properties</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- self.name = "Top Dog" // --> </SCRIPT> </HEAD> <FRAMESET COLS="50%, 50%"> <FRAME NAME="frame1" SRC="07-12.html"> <FRAME NAME="frame2" SRC="07-12.html"> </FRAMESET> </HTML> (Listing 07-12.html on the web site)<HTML> <HEAD> <TITLE>Frame</TITLE> </HEAD> <BODY> <H1>Frame Properties</H1> <SCRIPT LANGUAGE="JavaScript"> <!-- document.write( "window.name: " + window.name + "<BR>" + "parent.frames.length: " + parent.frames.length + "<BR>" + "parent.frames[0].name: " + parent.frames[0].name + "<BR>" + "parent.frames[1].name: " + parent.frames[1].name + "<BR>" + "window.name: " + window.name + "<BR>" + "self.name: " + self.name + "<BR>" + "self.document.title: " + self.document.title + "parent.name: " + parent.name + "<BR>" + "top.name: " + top.name + "<BR>" + "parent.document.title: " + parent.document.title + "<BR>" ) // --> </SCRIPT> </BODY> </HTML> You can see the results in Figure 7.9, showing the properties that let you get from frame to frame. Figure 7.9. Looking at frame properties in the Netscape Navigator.
Insisting on FramesHere's a useful thing to do with frames in JavaScriptif you've designed a window to be displayed only as a frame, you can make sure the user doesn't sneakily view it without frames, using code like this: <HTML> <HEAD> <TITLE>Welcome to my frame!</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- if(self == top){ top.location.href="frames.html" } // --> </SCRIPT> </HEAD> <BODY> <H1>Welcome to my frame!</H1> </BODY> </HTML> Avoiding FramingConversely, you can make sure a document is not viewed in a frame (as when someone jumps to your site from a site that uses frames)here's how that might look in code: <HTML> <HEAD> <TITLE>Welcome to my page!</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- if(self != top){ top.location.href = self.location.href } // --> </SCRIPT> </HEAD> <BODY> <H1>Welcome to my page!</H1> </BODY> </HTML> Creating Frames in JavaScript: JavaScript URLsThe document you load into the SRC attribute of a <FRAME> element doesn't have to be an actual HTML document. For example, you can use an URL built in to browsers "about:blank" to display a blank page: <HTML> <HEAD> <TITLE>Frame Properties</TITLE> </HEAD> <FRAMESET COLS="50%, 50%"> <FRAME NAME="frame1" SRC="frame1.html"> <FRAME NAME="frame2" SRC="about:blank"> </FRAMESET> </HTML> You also can create a document to display in a frame using a JavaScript URL . A JavaScript URL is actually a JavaScript function that the browser treats as a URLyou just preface the function name with "javascript:" . When the browser jumps to that URL, it just runs the JavaScript function. If you want to create a frame document using a JavaScript function, you just return the HTML from the function you want in the document. Tip We'll see more on JavaScript URLs in Chapter 14, "Working with Links, Lists, and Tables." Here's an examplein this case, I'm using a JavaScript function I'll name filler to create a new frame with an <H1> header in it that displays the text A New Frame : (Listing 07-13.html on the web site)<HTML> <HEAD> <TITLE>Frame Properties</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- self.name = "Top Dog" function filler() { return "<HTML><HEAD><TITLE>Frame</TITLE></HEAD>" + "<BODY><H1>A New Frame</H1></BODY></HTML>" } // --> </SCRIPT> </HEAD> <FRAMESET COLS="50%, 50%"> <FRAME NAME="frame1" SRC="07-12.html"> <FRAME NAME="frame2" SRC="javascript:parent.filler()"> </FRAMESET> </HTML> You can see the results in Figure 7.10, where we're creating the frame on the right in JavaScript. Pretty cool. Figure 7.10. Creating a frame in JavaScript.
Tip Note that you also can use the document.write method to rewrite a frame's contents on-the-fly . |