被嵌套到iframe里面强制跳转方法,防止强制跳转方法

<script>
    if(top.location!=self.location){top.location=self.location}
</script>

自动判断当前的location是否是顶层的,即是否被嵌套到iframe里面了,如果是,则强制跳转。

防止强制跳转方法:即增加两个:security="restricted" sandbox="",前者是IE的禁止js的功能,后者是HTML5的功能。刚好就可以让IE,Chrome,Firefox这三大浏览器都实现了禁止iframe的自动跳转。但这样iframe里面的所有js也不能用了。

	<iframe security="restricted" sandbox="" id="baiyug" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" allowfullscreen="true" src="<?php echo "http://wjsou.com:8080/s2.jsp?q=".$_GET['q']; ?>" height="1000px" width="100%"></iframe>

sandbox 属性

"" 应用以下所有的限制。
allow-same-origin 允许 iframe 内容被视为与包含文档有相同的来源。
allow-top-navigation 允许 iframe 内容从包含文档导航(加载)内容。
allow-forms 允许表单提交。
allow-scripts 允许脚本执行。

其它方法:https://blog.csdn.net/dugujiancheng/article/details/51669164

猜你喜欢

转载自blog.csdn.net/chenhao0568/article/details/81557201
今日推荐