221Chapter 16 .Window and Frame Objects Top versus (Michigan web site)
221Chapter 16 .Window and Frame Objects Top versus parent After seeing the previous object maps and reference examples, you may be wondering, Why not use topas the leading object in all trans-frame references? From an object model point of view, you ll have no problem doing that: A parent in a two-generation scenario is also the top window. What you can t count on, however, is your framesetting document always being the top window object in someone s browser. Take the instance where a Web site loads other Web sites into one of its frames. At that instant, the top window object belongs to someone else. If you always specify top in references intended just for your parent window, your references won t work and will probably lead to script errors for the user. My advice, then, is to use parentin references whenever you mean one generation above the current document. Preventing framing You can use your knowledge of top and parentreferences to prevent your pages from being displayed inside another Web site s frameset. Your top-level document must check whether it is loaded into its own top or parent window. When a document is in its own top window, a reference to the top property of the current window is equal to a reference to the current window (the windowsynonym self seems most grammatically fitting here). If the two values are not equal, you can script your document to reload itself as a top-level document. When it is critical that your document be a top-level document, include the script in Listing 16-1 in the head portion of your document: Listing 16-1: Prevention from Getting Framed Your document may appear momentarily inside the other site s frameset, but then the slate is wiped clean, and your top-level document rules the browser window. Ensuring framing When you design a Web application around a frameset, you may want to make sure that a page always loads the complete frameset. Consider the possibility that a visitor adds only one of your frames to a bookmarks list. On the next visit, only the bookmarked page appears in the browser, without your frameset, which may contain valuable navigation aids to the site. A script can make sure that a page always loads into its frameset by comparing the URLs of the top and selfwindows. If the URLs are the same, it means that the page needs to load the frameset. Listing 16-2 shows the simplest version of this
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.