说在前面的话,iframe是可以做很多事情的。

例如:

a>通过iframe实现跨域;

b>使用iframe解决IE6下select遮挡不住的问题

c>通过iframe解决Ajax的前进后退问题

d>通过iframe实现异步上传。(Easyui中form组件就是用的iframe,实现表单提交时,可以提交上传域)

下面就一些问题一一论述。

1、iframe基本知识:

iframe 元素会创建包含另外一个文档的内联框架(即行内框架)。

在 HTML 4.1 Strict DTD 和 XHTML 1.0 Strict DTD 中,不支持 iframe 元素。

提示:您可以把需要的文本放置在 和 之间,这样就可以应对无法理解 iframe 的浏览器。

可选属性:

标准属性:

2、操作iframe:注:测试环境IE:8.0,FF:23.0.1

a>隐藏iframe表框

i>标签中设置:frameborder="0",

ii>DOM操作:

var myiframe = document.getElementById("myiframe");

myiframe.style.border="none";//FF下有效,IE下无效

myiframe.setAttribute("frameborder",0);//FF下有效,IE下无效

myiframe.frameBorder = 0;//FF下有效,IE下无效

b>动态创建iframe

var newFrame = document.createElement("iframe");

newFrame.src ="http://blog.csdn.net/cuew1987";

newFrame.frameBorder = 0;//FF、IE隐藏边框有效

newFrame.width = "400px";

newFrame.height = "400px";

newFrame.scrolling = "no";

document.body.appendChild(newFrame);

c>获取iframe

i>var obj = document.getElementById("iframeID");

获取iframe对象,可直接操作iframe标签属性,如只想改变iframe的 src 或者 border ,scrolling 等attributes

ii>var dom = frames["iframeName"];

获取iframe的DOM对象,此对象可用来操作对象,比如想操作iframe页面中的元素。

d>获取iframe中的window对象

function getIframeWindow(obj) {

//IE || w3c

return obj.contentWindow || obj.contentDocument.parentWindow;

//parentWindow 是 parent window object

}

document.getElementById取到的iframe是不能直接操作里面的document的,只能这样取:

IE:frames[id].document或obj.contentWindow.document;

FF:dom.contentDocument或obj.contentDocument;不绑定任何事件.

e>获取iframe页面高度

function getIframeHeight(obj){

var idoc = getIframeWindow(obj).document;

if(idoc.body){

return Math.max(idoc.body.scrollHeight,idoc.body.offsetHeight);

}else if(idoc.documentElement){

return Math.max(idoc.documentElement.scrollHeight,idoc.documentElement.offsetHeight);

}

}

f>父子页面互访

i>子访问父:

parent.html:

等到的信息:

son.html:

function setMsg(){

var msg = window.parent.document.getElementById("msg");

msg.innerHTML= "Hello world!!";

}

ii>父访问子:

parent.html:

等到的信息:

function setMsg(){

var obj = document.getElementById("myiframe");

var msg = getIframeWindow(obj).document.getElementById("msg");

document.getElementById("setMsg").innerHTML = msg.innerHTML;

}

son.html:

Hello world!!!

3.iframe高度自适应和跨域:实际使用iframe中,会遇到iframe高度的问题,由于被嵌套的页面长度不固定而显示出来的滚动条,不仅影响美观,还会对用户操作带来不便

a>同域下的高度自适应

parent.html:

function getIframeWindow(obj) {

return obj.contentWindow || obj.contentDocument.parentWindow;

}

function getIframeHeight(obj){

var idoc = getIframeWindow(obj).document;

if(idoc.body){

return Math.max(idoc.body.scrollHeight,idoc.body.offsetHeight);

}else if(idoc.documentElement){

return Math.max(idoc.documentElement.scrollHeight,idoc.documentElement.offsetHeight);

}

}

function setHeight(){

var myiframe = document.getElementById("myiframe");

myiframe.height = getIframeHeight(myiframe);

}

另:document.documentElement与document.body相关说明(W3C DOM2.0规范)

document.doucmentElement:

documentElement of type Element, readonly,This is a convenience attribute that allows direct access to the

child node that is the root element of the document. For HTML documents, this is the element with the tagName "HTML".

document.body:

document.body is the element that contains the content for the document. In documents with

contents, returns the element,

and in frameset documents, this returns the outermost

element.

Though body is settable, setting a new body on a document will effectively remove all the current children of the existing

element.

IE在怪异模型(Quicks Mode)下document.documentElement无法正确取到clietHeight scrollHeight等值,比如clientHeight=0。

获取scrollTop:

var sTop=Math.max(

(document.body?document.body.scrollTop:0),

(document.documentElement?document.documentElement.scrollTop:0),

(window.pageYOffset?window.pageYOffset:0)

);

b>跨域下高度自适应

页面:

index.html:(http://www.csdn.net)

son.html:

function getHeight(){

var idoc = document;

if(idoc.body){

return Math.max(idoc.body.scrollHeight,idoc.body.offsetHeight);

}else if(idoc.documentElement){

return Math.max(idoc.documentElement.scrollHeight,idoc.documentElement.offsetHeight);

}

}

window.onload = function(){

var h = getHeight();

document.getElementById("agentIframe").src="http://www.csdn.net#"+h;

}

agent.html:(http://www.csdn.net)

(function(){

var con = parent.parent.document.getElementById('frame_content');

var href = parent.parent.frames["frame_content"].frames["iframeC"].location.hash;

con.style.height = href.split("#")[1]+"px";

})();

4.iframe背景透明:

在ie6/7/8下引入iframe的时候,它的背景默认是白色,即使设置了style=”background-color:transparent;”也无效,

但是其他浏览器(firefox,chrome,opera,ie9)都正常显示,要解决这个兼容性问题,必须用到一个属性。

下面来看看现象:index.html:

style="background-color:transparent;">

结果如下图:(FF中有滚动条是因为在index.html中设置了有滚动条)

解决:

给iframe设置属性:allowTransparency=”true” //设置为true允许透明

scrolling="yes" id="myiframe">

备注:iframe不设置此属性时,可使用iframe解决在IE6、7环境中遮住select

5.判断页面中是否有iframe:a>首先来看看window.frameElement这个属性。

返回嵌入当前window对象的元素(比如 或者 ),即为包含本页面的iframe或frame对象。如果当前window对象已经是顶层窗口,则返回null.

看看一个例子:

parent.html:

son.html:(注意frameElement用在son.html中,如果用在parent.html中,则返回null)

Hello world!!!

var iframe = window.frameElement;

if(iframe){

iframe.src = "http://blog.csdn.net/cuew1987";

}

备注:虽然该属性名为frameElement,但该属性也会返回其他类型比如 或者其他可嵌入窗口的元素.

b>兼容性如下图:

c>定义函数:

i>判断父页面中是否含有iframe

function hasIframe(){

return document.getElementsByTagName("iframe").length > 0;

}

ii>判断某个页面是否在iframe标签中

function innerIframe(){

var iframe = window.frameElement;

if(iframe){

return typeof iframe !== "undefined";

}

}

6、HTML5中iframe:

HTML 4.01 与 HTML 5 之间的差异在 HTML 5 中,仅仅支持 src 属性

HTML5中全局属性:

7、easyui中form组件提交(包括上传域):function submitForm(target, options) {

options = options || {};

if (options.onSubmit) {

if (options.onSubmit.call(target) == false) {

return;

}

}

var form = $(target);

if (options.url) {

form.attr("action", options.url);

}

var frameId = "easyui_frame_" + (new Date().getTime());

var frame = $("").attr(

"src",

window.ActiveXObject ? "javascript:false" : "about:blank").css(

{

position : "absolute",

top : -1000,

left : -1000

});

var t = form.attr("target"), a = form.attr("action");

form.attr("target", frameId);//在iframe中提交表单

try {

frame.appendTo("body");

frame.bind("load", cb);

form[0].submit();

} finally {

form.attr("action", a);

t ? form.attr("target", t) : form.removeAttr("target");

}

var checkCount = 10;

function cb() {

frame.unbind();

var body = $("#" + frameId).contents().find("body");

//contents()查找匹配元素内部所有的子节点(包括文本节点)。如果元素是一个iframe,则查找文档内容

var data = body.html();

if (data == "") {

if (--checkCount) {

setTimeout(cb, 100);

return;

}

return;

}

var ta = body.find(">textarea");

if (ta.length) {

data = ta.val();

} else {

var pre = body.find(">pre");

if (pre.length) {

data = pre.html();

}

}

if (options.success) {

options.success(data);

}

setTimeout(function() {

frame.unbind();

frame.remove();

}, 100);

};

};

另:form 的target属性:

a>HTML4中:

定义和用法:target 属性规定在何处打开 action URL。

兼容性:在 HTML 4.01 中,不赞成使用 form 元素的 target 属性;在 XHTML 1.0 Strict DTD 中,不支持该属性。

属性值:

_blank 新窗口中打开

_self 默认,在相同的框架中打开

_parent 父框架中打开

_top 整个窗口中打开

framename 指定的frame name属性值的框架中打开

b>HTML5中:

HTML 4.01 与 HTML 5 之间的差异

在 HTML5 中 target 属性不再是被废弃的属性。不再支持 frame 和 frameset。

现在,parent, top 和 framename 值大多用于 iframe。

8、网上问题收集:

a>window.frameElement在chrome下undefined?

问题描述:

今天在重新编写我的日历组件的时候,由于用到使用iframe自定义属性传值,

将父页面的值写在iframe 自定义属性上,然后在iframe页面中使用 window.frameElement.getAttribute() 获取,

奇怪的是之前编写的日历控件代码一直都这样写,没有发生过错误,但是今天在chrome里面 window.frameElement 竟然是 undefined,

在firefox 甚至IE6下都没有问题,后来百度没有答案, 再google 也,没有答案。

解决:

最后自己根据以往经验想到或许是本地调试权限问题,于是打开apache使用域名的形式访问,果然可以了,呵呵!

html iframe php,html iframe使用的实战总结分享相关推荐

  1. php 刷新iframe,js刷新iframe

    JS实现刷新iframe的方法 方案一:用iframe的name属性定位 οnclick="document.frames('ifrmname').location.reload()&quo ...

  2. IFrame语法:IFrame实例应用集

    Frame可以在网页内嵌入另一个页面,类似"画中画"形式. 标记的使用格式是:  <Iframe src="URL" width="x" ...

  3. html iframe自动高度,iframe高度自适应撑开

    iframe在div中如何自适应高度 div中的iframe高度和宽度自适应的问题 我用bootstrap布局,上边部分为导航菜单,左边为一级菜单. DIV里嵌套Iframe,让Iframe及DIV高 ...

  4. html中的<iframe></iframe>标签

    什么是iframe标签? iframe标签是一个双标签/围堵式标签<iframe></iframe>,是一个内联框架 iframe标签的作用? 用于在当前的HTML页面中嵌入另 ...

  5. iframe简单使用 、获取iframe 、获取iframe 元素值 、iframe获取父页面的信息

    文章目录 1.iframe简单使用 2.获取iframe 3.获取iframe 元素值 4.iframe获取父页面的信息: 1.iframe简单使用 <iframe> 标签规定一个内联框架 ...

  6. html5 iframe 无法滚动条,iframe嵌入的页面没有滚动条

    怎么样iframe没有滚动条 页面有滚动条 iframe没有滚动条 scrolling="No"这个去掉了但是页面超过了屏幕,应该有不是到你只什么意思哦frameborder=&q ...

  7. 动态生成 iframe;销毁 iframe,释放内存

    动态生成 iframe function createIframe(){var iframe = document.createElement("iframe");iframe.s ...

  8. 【实战经验分享】一劳永逸的解决网线随意热插拔问题

    [实战经验分享]一劳永逸的解决网线随意热插拔问题 参考文章: (1)[实战经验分享]一劳永逸的解决网线随意热插拔问题 (2)https://www.cnblogs.com/armfly/p/11818 ...

  9. 学习《Flask Web开发:基于Python的Web应用开发实战》分享

    学习<Flask Web开发:基于Python的Web应用开发实战>分享一直在说学习Python,对同事,对朋友,都说我正在学习Python,这无形给自己一定的压力,促使自己要去学习,进步 ...

最新文章

  1. LeetCode Linked List Cycle II
  2. Spring.NET学习笔记——前言
  3. python中有哪些赋值_python中的赋值,什么时候是传值什么时候是传址?
  4. 基于PyTorch+YOLOv4的口罩佩戴检测,附数据集
  5. Linux 下 Tomcat Https
  6. C# 小票打印机 直接打印 无需驱动
  7. Unity3D工程源码目录
  8. 吉林大学邮箱smtp服务器,吉林大学邮件系统成功案例-彩讯Richmail邮件系统,致力于互联网行业软件的开发和应用12年....
  9. Atom配置markdown
  10. 导入Maven项目右下角蓝色小框消失解决办法
  11. android 系统隐藏和显示虚拟按键的几种方法
  12. java jmf变成_java JMF 多媒体
  13. 考心理健康教育教师资格证-初出茅庐
  14. 博物馆360全景展厅无空间限制延伸会展空间
  15. unity webgl打包 苹果12以上机型打开连接后模型黑屏卡帧问题
  16. C语言将0xea转换为字符ea,eA第10章 c语言程序设计初步.ppt
  17. 样本均值的分布及中心极限定理
  18. Echarts可视化 之custom自定义系列
  19. 模式对象和用户权限管理
  20. wps2016向程序发送命令_Excel:向程序发送命令时出现问题

热门文章

  1. Windows 中自定义Error Codes
  2. 2017蓝桥杯c语言C组承压计算,蓝桥杯2017Java B组---分巧克力and承压计算
  3. springboot多环境加载yml和logback配置
  4. 使用run-rs启动mongodb
  5. docker 报错 Container is not running
  6. .net core linux安装
  7. Hadoop3 Hive Spark完整安装与实践
  8. 1024电商项目的邮箱验证码与图形验证码功能模块
  9. c语言计算据标准时间多少天,C语言系列--时间处理
  10. 面向对象封装之无参无返,无参有返