How to break out of frames with JavaScript - a Frame Breakout Script
May 19th, 2006
To prevent a Web page from loading in someone else’s frame a simple JavaScript can be linked to - or inserted within - the <head> section of a Web page.
This JavaScript make clients/browsers break out of the frame of the foreign website’s Web page and land on your website’s Web page instead.
If you want to have the same breakout script on several Web pages without having to write it over and over again, a good practice is to put it in an external file, and link to that file within the <head> section or right after the opening <body> tag.
If your concern is one page only you can have the script within the <script> tags included in the <head> section.
Frame Breakout script in an external file
This is what the external frame breakout- script looks like:
/*This is how to break out of a forreign website's frame with JavaScript*/
/*Does the browser support DOM?*/
ie = (document.all) ? true:false; // IE4+
dom = ((document.getElementById) && (!ie)) ? true:false; // Mozilla
/*Calls the help function to set an event*/
setEventByObject(window,"load", breakingOutOfFrames);
/*Help function*/
function setEventByObject(ob, ev, fu) {
if(dom) {
ob.addEventListener(ev, fu, false);
}
if(ie) {
ob.attachEvent('on' + ev, fu);
}
}
/*Breaks out of frames*/
function breakingOutOfFrames() {
if (top.location != location) {
top.location.href = document.location.href;
}
}
You can link to this script in the <head> section like this:
<script type="text/javascript" src="/yourJavaScriptFolder/breakingOutOfFrames.js"></script>
Internal Frame Breakout script within <script> tags in the <head> section of a Web page
This is what the internal frame breakout - script looks like:
<script type="text/javascript">
<!--
function breakingOutOfFrames() {
if (top.location != location)
top.location.href = document.location.href;
} //-->
</script>
You include this script directly in the <head> section of the Web page and call it by using the onload attribute in the <body> tag like this:
<body onload="breakingOutOfFrames()">
Web page content goes here...
</body>
Entry Filed under: JavaScript
Bookmark with del.icio.us





2 Comments Add your own
1 | May 26th, 2006 at 11:44 pm by Paul
Nice and simple script. Just what I needed.
Thanks
2 | October 30th, 2007 at 12:33 am by seestededrync
cheat codes for fear ps3
http://www.zloyonline.info - best deals on notebook computers
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed