How do I automatically remove an HTML page frame?
-
03-07-2019 - |
Question
What's the best way to remove a page frame automatically?
I've used this type of code before:
<script language="JavaScript">
setTimeout ("changePage()", 3000);
function changePage() {
if (self.parent.frames.length != 0)
self.parent.location="http://www.example.com";
}
</script>
Solution
Do you mean if someone has put a frame around your content? If so, you need the following any where in your HTML page to jump out of the iframe:
<script type="text/javascript">
if (window.top.location != window.location) {
window.top.location = window.location;
}
</script>
OTHER TIPS
Here's an alternative that's more generic in that it doesn't name the parent URL, nor use the separate function call:
// is the current page at the top of the browser window hierarchy?
if (top.location != self.location)
{
// it isn't, so force this page to be at
// the top of the hierarchy, in its own window
top.location = self.location
}
Do it this way if you want the frame-breaking step to not appear in the history
if ( self.location !== top.location )
{
top.location.replace( self.location );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow