Escaping Frames with JavaScript

Some Web sites may try and display your pages in one of their own frames, so they can bombard the user with adverts as they try to surf your site.

Moreover, by putting your page in a frame users will be unable to bookmark your pages properly.

We can use a very simple piece of JavaScript to ensure that a page is not shown inside a frame.

<script type="text/javascript">
    <!--
    if (top.frames.length > 0) {
        top.location.href = location.href;
    }
    // -->
</script>

Put the JavaScript code above into the head section of your Web page and it will automatically kill any frames that contain it. All it does is check to see if the main window has any frames, and if so sets its location to that of the trapped page so that your Web page fills the whole browser window.

Of course, this will only work for Web browsers with JavaScript enabled.