操作iframe 的方法与兼容性

摘要:
首先创建两个页面//iframe1.html单击调用父框架varbtn=document。按ID获取元素;variframe1=文档。按ID获取元素;btn.onclick=function(){iframe1.contentWindow.document.getElementById.style.background=“red”;//iframe1.contentDocument.getElementById.style.background=“red”;}iframe1.contentWindow获取src设置页面的窗口对象,然后在其中操作DOM。该方法与IE678和其他主流浏览器(如FFChrome)兼容。但Chrome只能在服务器端用于安全保护。您可以使用phpstudy测试iframe1.contentDocumentIE。较低版本不支持调用父框架窗口。top以与Windows相同的方式调用top框架。Chrome//ifram2.html中的父级˂=窗口。top){//当前页面必须是最高级别的页面window.top.location.href=window.location.href;}更改帧高度˂!

首先创建两个页面

//iframe1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
          <p id="content">帅哥天下9</p>
<script>
         console.log( window.parent.document.getElementById("testParent").innerHTML);
//调用父框架
         </script>

         
</body>
</html>
//demo1.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
 <button id="btn">click</button>
<div id="testParent">调用父框架</div>
 <iframe src="iframe1.html" id="iframe1" frameborder="1"></iframe>

<script> var btn=document.getElementById("btn"); var iframe1=document.getElementById("ifram1"); btn.onclick=function(){ iframe1.contentWindow.document.getElementById("content"). style.background="red";

//iframe1.contentDocument.getElementById("content")
.style.background="red"; }
</script> </body> </html>

iframe1.contentWindow 获取 src设置页面的window对象然后操作里面的DOM

这个方法兼容IE 678 和其他主流浏览器 比如 FF Chrome 但是 Chrome对安全有保护

只可以在服务器端使用 可以用phpstudy测试

iframe1.contentDocument  IE低版本不支持

在Chrome同理

window.parent 调用父框架

window.top 调用顶层框架

//ifram2.html
<!
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body>

<button id="changeTopDiv">changeTopDiv</button> <iframe src="iframe2.html" frameborder="1" ></iframe>
<script>
var ctd=documet.getElementById("changeTopDiv");
var topDiv=window.top.document.getElementById("topIframe");
ctd.onclick=funtion(){
                topDiv.style.background="red";
}
</script>
</body> </html>
//demo2.html
<!
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <iframe src="iframe2.html" frameborder="1"></iframe> </body> </html>
//demo3.html
<!
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body>

<div id="topIframe">topIframe</div> <iframe src="demo2.html" frameborder="1"></iframe> </body> </html>

还有一个就是防止钓鱼

有的网站会把别的网站iframe进来 然后欺骗用户去操作一些东西 谋利

code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <iframe src="http://t.zoukankan.com/test.html" frameborder="1"></iframe>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    if(window !=window.top){
//必须让当前页面为最高级别页面
window.top.location.href=window.location.href;

} </body> </html>

改变框架高度

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
          html,body{
           
             padding: 0;
             margin: 0;
       }
       
       .box{
          
          width:200px;
          height:200px;
          background: red;

       }

       </style>
</head>
<body>
     <div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
       html,body{
             
             padding: 0;
             margin: 0;
       }

       .box{
          
          width:200px;
          height:400px;
          background: green;

       }
    </style>
</head>
<body>
     <div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
     <iframe src="iframe5.html" frameborder="1" id="show" scrolling="no"></iframe>
     <button id="btn1">btn1</button>
     <button id="btn2">btn2</button>
     <script>
         var btn1=document.getElementById("btn1");
         var btn2=document.getElementById("btn2");
         var show=document.getElementById("show");
function changeHeight(){ setTimeout(function(){ // 添加一个定时器 让他执行慢一点
//不然src刚执行完 html 还没刷新完
// 就改变宽度 还是之前的宽度 show.height
=show.contentWindow.document.body.offsetHeight; }, 200); } changeHeight(); btn1.onclick=function(){ show.src="iframe5.html"; changeHeight(); } btn2.onclick=function(){ show.src="iframe6.html"; changeHeight(); } </script> </body> </html>

写到这里 累死我了

最后一个就是iframe 的load事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
     <iframe src="iframe8.html" frameborder="1" id="show" scrolling="no"></iframe>
       
<script>
           var show=document.getElementById("show");
show.onload=function(){
alert("加载完毕!");
}

//ie 也支持这个事件 但是 IE事件不能这么用
//得需要事件绑定才可以
//show.attachEvent("click",function(){

alert("加载完毕");

});
        </script>
 
     </script>
</body>
</html>

 

免责声明:文章转载自《操作iframe 的方法与兼容性》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇(考研)(精华)二叉树的知识结构图以及各种特殊的二叉树PyCharm3.0默认快捷键简单总结下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

防止 XSS 攻击 解决方案

1、 XSS又叫CSS英文缩写为Cross Site Script中文意思为跨站脚本攻击具体内容指的是恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意用户的特殊目的. 解决方案 第一。过滤 过滤 标签 等字符 ,但是这样 对用户是不公平的。 第二。用asii 码替换 如 <  &...

WebGL 内嵌网页的一种解决方案

  之前使用的 ZFBrowser 嵌入方案可以发布到 Win, OS, Linux 上, 可是其它的就不行, 因为它用的谷歌内核嘛, 在 WebGL 上照理来说应该是最方便的啊, 因为它本身就是运行在浏览器内核上的, 前面的 BrowserInput 已经研究过向网页注入代码以及调用了, 现在试试在 WebGL 环境下来注入代码, 来创建简单的内嵌网页看...

点击劫持(click jacking)

什么是点击劫持劫持原理劫持案例代码示例优酷频道刷粉的POC腾讯微博刷粉防御 什么是点击劫持 点击劫持,clickjacking,也被称为UI-覆盖攻击。这个词首次出现在2008年,是由互联网安全专家罗伯特·汉森和耶利米·格劳斯曼首创的。 它是通过覆盖不可见的框架误导受害者点击。 虽然受害者点击的是他所看到的网页,但其实他所点击的是被黑客精心构建...

前端常见跨域解决方案(全)

什么是跨域? 跨域是指一个域下的文档或脚本试图去请求另一个域下的资源,这里跨域是广义的。 跨域的根本原因就是浏览器,所以在后台是不存去前端一样上的跨域的,后台调后台是不会跨域的,但是必须网络是通的 广义的跨域: 1.) 资源跳转: A链接、重定向、表单提交 2.) 资源嵌入: <link>、<script>、<img>...

VUE 页面缓存2

=============================转载================================== 每天进步一点点~~~ 第一步 在app中设置需要缓存的div //缓存的页面 1 <keep-alive> 2 <router-view v-if="$route.meta.keepAlive">&...

Web自动化学习(4)

1、Selenium中有哪些不同类型的定位器? 定位器可以看作一个地址,用于在网页中唯一标识一个页面元素,为了准确地识别Web元素 Selenium中有8种不同的定位方式:ID;ClassName;Name;TagName;LinkText;PartialLinkText;Xpath;CSS Selector。 2、什么是XPath? 元素定位方式的一种...