我写了一个函数,弹出一个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变量用一个常量代替,则去掉红色字体部分也能正确运行。请大家帮忙看看是怎么回事?谢谢。我都弄了一天了。分不多,请大家包涵!

解决方案 »

  1.   

    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);
        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));
    }朋友们可以运行一下,看看把红色字体部分去掉以后还能正常运行吗
      

  2.   

    只有你自己回答,分就给您了!谢谢您!找着原因了,是因为如果没有那个alert的话,接着就这行下一句了,此时iframe还没加载完毕,当然找不到cotentDocument对象了!
    解决方法:
      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));}