如何阻止iframe里引用的网页自动跳转

  需求现在在做一个项目,要求一个页面内嵌Bing和Baidu俩个iframe,载入页面后,从url中拿数据进行搜索。

    问题完全载入页面后,会跳转到百度页面。

    问题来源百度代码中有 if (top.location != self.location) {top.location=self.location;} 使用里这段代码之后,会自动判断当前的location是否是顶层的,即是否被嵌套到iframe里面了,如果是,则强制跳转。

    解决办法

<iframe src="" class="iframe" scrolling="no" security="restricted" sandbox="">

  即增加两个:security="restricted" sandbox=""(内嵌百度时要允许弹框,即sandbox="allow-popups"),前者是IE的禁止js的功能,后者是HTML5的功能。刚好就可以让IE,Chrome,Firefox这三大浏览器都实现了禁止iframe的自动跳转.

  

原文地址:https://www.cnblogs.com/chenwenhao/p/8268753.html