• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 4. Frames, Frames, and More Frames > Keeping a Page out of a Frame

Keeping a Page out of a Frame

Other people can put one of your pages inside a frame on their site, making it appear that your page is part of their content. In JavaScript, windows appear in a hierarchy, with the parent window at the top of the heap. When someone hijacks your page, they are forcing it to be a child frame to their parent window. Figure 4.2 shows how the page would appear as part of someone else's site. With script 4.1, you can prevent this page hijacking and force your page to always be in a browser window by itself.

Figure 4.2. Your page, buried in someone else's frameset.


Script 4.1. JavaScript provides a way to force your page to always appear on a separate page.


To isolate your page:

1.
								if (top.location != self.location) {
							



First, check to see if the location of the current page (self) is the top-most in the browser window hierarchy. If it is, there's no need to do anything.

2.
								top.location.replace(self.location)
							



If the current page isn't at the top, replace the top page with the location of the current page. This will force the current window to be your page and your page only. Figure 4.3 shows your page, as you designed it.

Figure 4.3. Your page, after escaping from the evil hijacking frameset.


Tip

We could just set top.location to self.location, but this has one nasty side effect: the user can no longer use their browser's back button. If they try to, going back to the previous page aumatically jumps them back to the current page. Using the replace() method shown above replaces the current page in the history, which allows the back button to display the previous page.


  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint