我写了一个函数,弹出一个iframe,
代码如下:
//判断当前页中是否有iframe
var ParentObj=window;
var parentLocation=window.parent.location;
var nowLocation=window.location;
var ParentIframe;
while(parentLocation!=nowLocation)
{
ParentObj=ParentObj.window.parent;
nowLocation=ParentObj.window.location;
parentLocation=ParentObj.parent.location;
}
var aaa=ParentObj.document.createElement("iframe");
aaa.src=WebString;
// aaa.setAttribute("style",styleString);
aaa.setAttribute("id",YourID);
// aaa.id=YourID;
// aaa.setAttribute("width","500px");
// aaa.setAttribute("heigth","500px");
ParentObj.window.document.body.appendChild(aaa);
var bbbbbb=ParentObj.document.getElementById(YourID);
bbbbbb.style.position="absolute"
bbbbbb.style.zIndex="10000";
alert(bbbbbb.id);
//得到包含的网页的高度和宽度
var bHeight = bbbbbb.contentWindow.document.body.scrollHeight;
var dHeight = bbbbbb.contentWindow.document.documentElement.scrollHeight;
var bWidth=bbbbbb.contentWindow.document.body.scrollWidth;
var dWidth=bbbbbb.contentWindow.document.documentElement.scrollWidth;
var height = Math.max(bHeight, dHeight);
var width=Math.max(bWidth,dWidth);
//得到浏览器屏幕可见区域的高度和宽度
var ScreenHeigth=window.top.document.body.offsetHeight;
var ScreenWidth=window.top.document.body.offsetWidth;
var top;
var left;
var bottom="60px";
var right;
if(width>ScreenWidth)
{
bbbbbb.style.width=ScreenWidth;
left="0px";
right="0px";
}
else
{
left=(ScreenWidth-width)/2+"px";
right=left;
}
if(height>ScreenHeigth)
{
top=window.top.document.body.scrollTop+"px";
bottom="0px";
}
else
{
top=(window.top.document.body.scrollTop+ScreenHeigth-height)/2+"px";
bottom=top;
}
bbbbbb.style.top=top;
bbbbbb.style.left=left;
bbbbbb.style.height=Math.min(height,ScreenHeigth);
bbbbbb.style.width=Math.min(width,ScreenWidth); alert(window.top.document.body.clientHeight);
alert(window.top.document.body.offsetHeight);
var shadeHieght=window.top.document.body.scrollHeight;
var shadeWidth=window.top.document.body.scrollWidth;
window.top.document.body.appendChild(CreateShade("Name",shadeWidth,shadeHieght,1000));请注意红色字体部分,如果不加,那么var bHeight = bbbbbb.contentWindow.document.body.scrollHeight;
这句就会出现 Microsoft JScript 运行时错误: 缺少对象 错误。另外,如果把YourID变量用一个常量代替,则去掉红色字体部分也能正确运行。请大家帮忙看看是怎么回事?谢谢。我都弄了一天了。分不多,请大家包涵!
代码如下:
//判断当前页中是否有iframe
var ParentObj=window;
var parentLocation=window.parent.location;
var nowLocation=window.location;
var ParentIframe;
while(parentLocation!=nowLocation)
{
ParentObj=ParentObj.window.parent;
nowLocation=ParentObj.window.location;
parentLocation=ParentObj.parent.location;
}
var aaa=ParentObj.document.createElement("iframe");
aaa.src=WebString;
// aaa.setAttribute("style",styleString);
aaa.setAttribute("id",YourID);
// aaa.id=YourID;
// aaa.setAttribute("width","500px");
// aaa.setAttribute("heigth","500px");
ParentObj.window.document.body.appendChild(aaa);
var bbbbbb=ParentObj.document.getElementById(YourID);
bbbbbb.style.position="absolute"
bbbbbb.style.zIndex="10000";
alert(bbbbbb.id);
//得到包含的网页的高度和宽度
var bHeight = bbbbbb.contentWindow.document.body.scrollHeight;
var dHeight = bbbbbb.contentWindow.document.documentElement.scrollHeight;
var bWidth=bbbbbb.contentWindow.document.body.scrollWidth;
var dWidth=bbbbbb.contentWindow.document.documentElement.scrollWidth;
var height = Math.max(bHeight, dHeight);
var width=Math.max(bWidth,dWidth);
//得到浏览器屏幕可见区域的高度和宽度
var ScreenHeigth=window.top.document.body.offsetHeight;
var ScreenWidth=window.top.document.body.offsetWidth;
var top;
var left;
var bottom="60px";
var right;
if(width>ScreenWidth)
{
bbbbbb.style.width=ScreenWidth;
left="0px";
right="0px";
}
else
{
left=(ScreenWidth-width)/2+"px";
right=left;
}
if(height>ScreenHeigth)
{
top=window.top.document.body.scrollTop+"px";
bottom="0px";
}
else
{
top=(window.top.document.body.scrollTop+ScreenHeigth-height)/2+"px";
bottom=top;
}
bbbbbb.style.top=top;
bbbbbb.style.left=left;
bbbbbb.style.height=Math.min(height,ScreenHeigth);
bbbbbb.style.width=Math.min(width,ScreenWidth); alert(window.top.document.body.clientHeight);
alert(window.top.document.body.offsetHeight);
var shadeHieght=window.top.document.body.scrollHeight;
var shadeWidth=window.top.document.body.scrollWidth;
window.top.document.body.appendChild(CreateShade("Name",shadeWidth,shadeHieght,1000));请注意红色字体部分,如果不加,那么var bHeight = bbbbbb.contentWindow.document.body.scrollHeight;
这句就会出现 Microsoft JScript 运行时错误: 缺少对象 错误。另外,如果把YourID变量用一个常量代替,则去掉红色字体部分也能正确运行。请大家帮忙看看是怎么回事?谢谢。我都弄了一天了。分不多,请大家包涵!
解决方案 »
- 求一段JavaScript,让表格一部分显示或不显示
- 郁闷~~~谁能告诉我这是咋回事?
- 除尽
- 如何让一个iframe中的连接打开的页面在另一个IFRAME中显示
- JS获取页面doctype,有木有大侠知道,并且可以兼容IE,firefox的
- 如何获取一个page里面所有元素的computed style,并将获取到的computed style的值赋给该元素的style属性
- 新手求救-javascript怎么入门啊??
- 如和在打开一个页面时候,关闭另外一个页面.
- 菜鸟向各位高人请教几个简单问题
- 当用浏览器打开一个新页面(用document.open()这个方法)时……
- document中监听keydown,但鼠标放在textbox上,事件是监听了,但textbox上会留下用户输入的字母。有没有全局的方法暂时屏蔽input输入
- 用JS代码拼接图片问题
{
//判断当前页中是否有iframe
var ParentObj=window;
var parentLocation=window.parent.location;
var nowLocation=window.location;
var ParentIframe;
while(parentLocation!=nowLocation)
{
ParentObj=ParentObj.window.parent;
nowLocation=ParentObj.window.location;
parentLocation=ParentObj.parent.location;
}
var aaa=ParentObj.document.createElement("iframe");
aaa.src=WebString;
aaa.setAttribute("id","aaaaaa"); ParentObj.window.document.body.appendChild(aaa);
var bbbbbb=ParentObj.document.getElementById("aaaaaa");
bbbbbb.style.position="absolute"
bbbbbb.style.zIndex="10000";
alert(bbbbbb.id);
//得到包含的网页的高度和宽度
var bHeight = bbbbbb.contentWindow.document.body.scrollHeight;
var dHeight = bbbbbb.contentWindow.document.documentElement.scrollHeight;
var bWidth=bbbbbb.contentWindow.document.body.scrollWidth;
var dWidth=bbbbbb.contentWindow.document.documentElement.scrollWidth;
var height = Math.max(bHeight, dHeight);
var width=Math.max(bWidth,dWidth);
//得到浏览器屏幕可见区域的高度和宽度
var ScreenHeigth=window.top.document.body.offsetHeight;
var ScreenWidth=window.top.document.body.offsetWidth;
var top;
var left;
var bottom="60px";
var right;
if(width>ScreenWidth)
{
bbbbbb.style.width=ScreenWidth;
left="0px";
right="0px";
}
else
{
left=(ScreenWidth-width)/2+"px";
right=left;
}
if(height>ScreenHeigth)
{
top=window.top.document.body.scrollTop+"px";
bottom="0px";
}
else
{
top=(window.top.document.body.scrollTop+ScreenHeigth-height)/2+"px";
bottom=top;
}
bbbbbb.style.top=top;
bbbbbb.style.left=left;
bbbbbb.style.height=Math.min(height,ScreenHeigth);
bbbbbb.style.width=Math.min(width,ScreenWidth); alert(window.top.document.body.clientHeight);
alert(window.top.document.body.offsetHeight);
var shadeHieght=window.top.document.body.scrollHeight;
var shadeWidth=window.top.document.body.scrollWidth;
window.top.document.body.appendChild(CreateShade("Name",shadeWidth,shadeHieght,1000));
}朋友们可以运行一下,看看把红色字体部分去掉以后还能正常运行吗
解决方法:
if(aaa.attachEvent)
{
aaa.attachEvent("onload",function(){shia()});
}
else
{
aaa.onload=function(){
alert("第二个加载完成");
shia();
}
}在onload事件中再调用哪些方法,完整代码,自己随便写的,可能有点乱。function PopupContent(WebString)
{
//判断当前页中是否有iframe
var ParentObj=window;
var parentLocation=window.parent.location;
var nowLocation=window.location;
var ParentIframe;
while(parentLocation!=nowLocation)
{
ParentObj=ParentObj.window.parent;
nowLocation=ParentObj.window.location;
parentLocation=ParentObj.parent.location;
}
var aaa=ParentObj.document.createElement("iframe");
aaa.src=WebString;
aaa.setAttribute("id","aaaaaa");
ParentObj.window.document.body.appendChild(aaa);
if(aaa.attachEvent)
{
aaa.attachEvent("onload",function(){shia()});
}
else
{
aaa.onload=function(){
alert("第二个加载完成");
shia();
}
}
function shia()
{
var bbbbbb=window.top.document.getElementById("aaaaaa");
bbbbbb.style.position="absolute"
bbbbbb.style.zIndex="10000";
//得到包含的网页的高度和宽度
var bHeight = bbbbbb.contentWindow.document.body.scrollHeight;
var dHeight = bbbbbb.contentWindow.document.documentElement.scrollHeight;
var bWidth=bbbbbb.contentWindow.document.body.scrollWidth;
var dWidth=bbbbbb.contentWindow.document.documentElement.scrollWidth;
var height = Math.max(bHeight, dHeight);
var width=Math.max(bWidth,dWidth);
//得到浏览器屏幕可见区域的高度和宽度
var ScreenHeigth=window.top.document.body.offsetHeight;
var ScreenWidth=window.top.document.body.offsetWidth;
var top;
var left;
var bottom="60px";
var right;
if(width>ScreenWidth)
{
bbbbbb.style.width=ScreenWidth;
left="0px";
right="0px";
}
else
{
left=(ScreenWidth-width)/2+"px";
right=left;
}
if(height>ScreenHeigth)
{
top=window.top.document.body.scrollTop+"px";
bottom="0px";
}
else
{
top=(window.top.document.body.scrollTop+ScreenHeigth-height)/2+"px";
bottom=top;
}
bbbbbb.style.top=top;
bbbbbb.style.left=left;
bbbbbb.style.height=Math.min(height,ScreenHeigth);
bbbbbb.style.width=Math.min(width,ScreenWidth);
var shadeHieght=window.top.document.body.scrollHeight;
var shadeWidth=window.top.document.body.scrollWidth;
window.top.document.body.appendChild(CreateShade("Name",shadeWidth,shadeHieght,1000));}