当前位置:首页 > JavaScript

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

canca8年前 (2018-07-24)JavaScript1254

需求:现在在做一个项目,要求一个页面内嵌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的自动跳转.

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/3.html

标签: JavaScript
分享给朋友:

“如何阻止iframe里引用的网页自动跳转” 的相关文章

javascript 中面向对象編程 (类的继承)

// 人的基類var Person=new ( function(){  var sex;  var name;  this.getSex=function() &nbs…

javascrip 事件追加方法

基本方法:attachEvent(IE)/detachEvent;addEventListener( Mozilla, Netscape, Firefox)/removeEventListener在之前的邏輯判斷式的基礎上,在設計javascript的時候,可以針對瀏覽器的不同,寫出適合不同種類瀏覽…

javascript在IE和Firefox中的兼容考虑

1.document.formName.item("itemName") 问题说明:IE下,可以使用document.formName.item("itemName")或document.formName.elements["elementName"];Firefox下,只能使用docum…

event事件兼容mozilla firefox的问题

本来一个写好了的脚本在IE下正常,可是在mozilla firefox中我们发现程序报类似如下的错误: event is not defined obj has no properties 原因是Firefox中使用了不同的事件对象模型,不同于IE Dom,用的是W3C Dom. 下是我在网上找到的…

FCKeditor获取内容以及赋值

1、获取编辑器中HTML内容 function getEditorHTMLContents(EditorName) { var   oEditor = FCKeditorAPI.GetInstance(EditorName); return(oEditor.GetXHTML(tr…

父窗口为Iframe添加事件(跨浏览器)

父窗口为iframe添加事件.多浏览器兼容.  function addIframeScript(){  if(document.frames){   document.frames["iframe1"].document.attachEv…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。