js对iframe的操作

1、获取iframe中页面中的window对象

  contentWindow  <iframe src="iframe1.htm" id="iframe1"></iframe>  

  contentWindow属性。(在chrome中必须在服务器环境下)  

  oIframe.contentWindow.document.getElementById('iframe1-div');  

2、操作document对象 contentDocument IE8+  

  oIframe.contentDocument.getElementById('iframe-div');  

3、iframe中操作父级内容

  window.parent.document.getElementById

4、获取最顶层的window对象:

   window.top最顶层

5、iframe加载事件

  onload  oIframe.onload=function()  {     }  

   在IE中要绑定才行  oIframe.attachEvent('onload',function(){     })  

6、防止被嵌套(防钓鱼)  

  if(window!=window.top)  {   window.top.location.href=location.href;  }  

7、获取iframe中内容的高度宽度  

  oIframe.contentWindow.document.body.offsetHeight;    

    function changeHeight()  {   setTimeout(function(){    oIframe.height=oIframe.contentWindow.document.body.offsetHeight;   },100);  }  

8、调用iframe中的js方法  

  document.getElementById('iframeid').contentWindow.subFun();

posted @ 2012-11-22 14:47  hello*boy  阅读(792)  评论(0编辑  收藏  举报