js如何关闭弹出的iframe

JavaScript030

js如何关闭弹出的iframe,第1张

首先iframe中的页面应该与父页面是同域页面,否则跨域的话js无法控制iframe中的内容。

如果父页面与框架中的页面时同域的,可以通过改变a标签的href属性来实现:

1

<a href="javascript:return false" onclick="window.open('http://zhidao.baidu.com/','_self')" target="_blank">猛戳这里</a>

这样实际是把原来链接的作用取消,然后给a标签绑定js的window.open方法打开页面,target也就不起作用了。

允许。根据查询iframe软件官网得知该软件是允许弹窗的,也可以在设置中关闭。iframe是HTML标签,作用是文档中的文档,或者浮动的框架(FRAME),iframe元素会创建包含另外一个文档的内联框架(即行内框架)。

js加载iframe框架 弹出框

<、script language="JavaScript" type="text/JavaScript">

function showIframe(url,w,h){

//添加iframe

var if_w = w

var if_h = h

//allowTransparency='true' 设置背景透明

$(" + if_w + "' height='" + if_h + "' id='YuFrame1' name='YuFrame1' style='position:absolutez-index:4' frameborder='no' marginheight='0' marginwidth='0' allowTransparency='true'>").prependTo('body')

var st=document.documentElement.scrollTop|| document.body.scrollTop//滚动条距顶部的距离

var sl=document.documentElement.scrollLeft|| document.body.scrollLeft//滚动条距左边的距离

var ch=document.documentElement.clientHeight//屏幕的高度

var cw=document.documentElement.clientWidth//屏幕的宽度

var objH=$("#YuFrame1").height()//浮动对象的高度

var objW=$("#YuFrame1").width()//浮动对象的宽度

var objT=Number(st)+(Number(ch)-Number(objH))/2

var objL=Number(sl)+(Number(cw)-Number(objW))/2

$("#YuFrame1").css('left',objL)

$("#YuFrame1").css('top',objT)

$("#YuFrame1").attr("src", url)

//添加背景遮罩

$("").prependTo('body')

var bgWidth = Math.max($("body").width(),cw)

var bgHeight = Math.max($("body").height(),ch)

$("#YuFrame1Bg").css({width:bgWidth,height:bgHeight})

//点击背景遮罩移除iframe和背景

$("#YuFrame1Bg").click(function() {

$("#YuFrame1").remove()

$("#YuFrame1Bg").remove()

})

}

<、input type="button" onClick="showIframe('http://baidu.com',460,280)" value="加载"/>