题
自动删除页面框架的最佳方法是什么?
我之前使用过这种类型的代码:
<script language="JavaScript">
setTimeout ("changePage()", 3000);
function changePage() {
if (self.parent.frames.length != 0)
self.parent.location="http://www.example.com";
}
</script>
解决方案
你的意思是说有人在你的内容周围放了一个框架吗?如果是这样,您需要在HTML页面中的任何位置跳出iframe:
<script type="text/javascript">
if (window.top.location != window.location) {
window.top.location = window.location;
}
</script>
其他提示
这是一个更通用的替代方案,因为它没有命名父URL,也没有使用单独的函数调用:
// 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
}
如果您希望破帧步骤不出现在历史记录
中,请执行此操作if ( self.location !== top.location )
{
top.location.replace( self.location );
}
不隶属于 StackOverflow