当前位置:首页 > JavaScript

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

canca8年前 (2018-07-24)JavaScript1179

需求:现在在做一个项目,要求一个页面内嵌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里引用的网页自动跳转” 的相关文章

《Dom Scripting》读书笔记

  《Dom Scripting》读书笔记 Canca         最近看了一本《Jeremy Keith: DOM Scripting, Web design with JavaScript and the DOM. Apr…

javascript 弹出式窗体详解

1>window.prompt(text, value) 簡單的基與模態窗體的對話框,(返回你輸入)   var v=window.prompt("提示","請輸入你的名字")2>window.confirm(text,mess)  模態確認框(返回"是/否…

Javascript 逻辑表达式

1>逻辑表达式    看例子:     var cc;      var  dd=new Object();      var false1="false…

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. 下是我在网上找到的…

JavaScript对粘贴板的全操作

<html>    <head>        <script language="javascript">function copy_clip(copyObjectName…

发表评论

访客

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