正文 1160字数 107,699阅读

security="restricted" sandbox=""
Run code
Cut to clipboard


    今天做了个网页,要在网页里设置一个iframe,然后套用其他的网站。使用

    http://luanqi-cat.blogbus.com
    Run code
    Cut to clipboard


      这个网址的时候,出现了莫名其妙的问题,我的网页居然会强制自动跳转到这个网页上。搜索了一番,才知道原来这个网页用了如下的一段代码:

      if (top.location != self.location) {top.location=self.location;}
      Run code
      Cut to clipboard


        使用里这段代码之后,会自动判断当前的location是否是顶层的,即是否被嵌套到iframe里面了,如果是,则强制跳转。

        一时间搜索无果,网上的很多做法都不怎么行了。然后找到了这两篇文章:
        http://www.codinghorror.com/blog/2009/06/we-done-been-framed.html
        Run code
        Cut to clipboard


          以及:
          http://seclab.stanford.edu/websec/framebusting/framebust.pdf
          Run code
          Cut to clipboard
            特别是第二篇文章,介绍了很多基于iframe的攻防技术。

            双重iframe的确可以阻止强制跳转。但是,第一层的iframe就覆盖了第二层的。所以要把第一层的做成透明的,然后第二层嵌套博客大巴这个网页。对我来说比较复杂。

            后来我的做法如下:
            <iframe src="http://www.shahuwang.com/"http://luanqi-cat.blogbus.com/" class="t-iframe" scrolling="no" security="restricted" sandbox="">
            Run code
            Cut to clipboard

              即增加两个:
              security="restricted" sandbox=""
              Run code
              Cut to clipboard
                ,前者是IE的禁止js的功能,后者是HTML5的功能。刚好就可以让IE,Chrome,Firefox这三大浏览器都实现了禁止iframe的自动跳转
                http://ju.outofmemory.cn/entry/23029
                Run code
                Cut to clipboard