2010-07-30 108 views
1

我有一个asp页面有一个iframe。我需要将自动高度设置为iframe。我在iframe sizing - cross browser issue 找到一篇文章,但它不能解决我的问题。 我的iframe是自动iframe高度

<iframe id="content" src=http://www.bc.com.au/news_manager/templates/?a=<%=request.QueryString("a")%>&z=<%=request.QueryString("z")%> width="908px" height="1000px" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" style="margin-left:auto; margin-right:auto;" ></iframe> 

和ASP页上的代码是:

<script type="text/javascript"> 
    window.onresize=resizeContentFrame; 
      resizeContentFrame(); 

function resizeContentFrame(){ 
    setFrameHeight(documenent.getElementById('content')); 
} 

function setFrameHeight(f){ 
    if(isDefined(f)){ 
    alert('executing'); 
     var h=document.documentElement.scrollHeight; 
     h-=(HEADER_HEIGHT+CONTENT_PADDING+5); 
     f.style.height=h+'px'; 
    } 
} 
</script> 

如果我不给iframe中一个固定的高度它只是切断iframe中的内容。

我在做什么错,而上面的脚本被标记为答案?

帮助...

+0

是否在与包含文档不同的域上使用iframe内容? – 2010-07-30 09:58:45

+0

是安迪... iframe内容来自不同的域 – Anil 2010-07-30 10:01:50

回答

0

Could'nt你只需要使用HTML属性 “高度”,并将其设置到例如 “90%”?或者布局更复杂?在这种情况下,您可以使用css-attribute并使用jQuery动态更改。

+0

谢谢奥拉夫。但情况有点复杂。我不知道什么时候身高会在高峰期或什么时候。所以我需要可靠的解决方案。 – Anil 2010-07-30 10:12:23

0
var h=document.documentElement.scrollHeight; 

这是文档的高度,而不是窗口的高度。你可能想要clientHeight而不是scrollHeight。此外,请确保您使用的是标准模式<!DOCTYPE,否则您将得到Quirks模式,您必须改为查看document.body。 (以及许多其他不良特性。)

在IE6-7以外的浏览器中,您可以在不使用脚本的情况下执行此操作。请参阅this question

?a=<%=request.QueryString("a")%> 

HTML注入。像?a=><script>alert(document.cookie)</script>这样的查询可以将脚本内容注入到您的页面中,从而为您提供潜在的XSS安全漏洞。在任何情况下,您也应该使用URL编码您将要插入到URL中的任何查询参数:

url= "http://www.bc.com.au/news_manager/templates/?a="&Server.UrlEncode(Request.QueryString("a"))&"&z="&Server.UrlEncode(Request.QueryString("z")) 

<iframe id="content" src="<%= Server.HtmlEncode(url) %>">