2011-05-04 85 views
0

我正在写图像嵌入服务的后端。我们正在考虑使用类似于YouTube的iframe来支持嵌入。有没有办法在后端获得“<iframe>”的“宽度”和“高度”?

如:

<iframe type="text/html" src="http://example.com/photos/embed/abc/" width="640px" height="480px" frameborder="1"> </iframe>

使用上述任何地方的iframe嵌入应在iframe的图像(如标题一些额外的东西一起,等等)。但是,我想返回一个与iframe中指定的“宽度”和“高度”大小相同的图像。我想象应该在后台进行图片大小调整以实现此功能。

在这种情况下,我需要在后端访问iframe的宽度和高度。有什么方法可以在后端解决这个问题吗?例如,这些是HTTP请求的一部分还是这种类型的东西?

PS:我使用的Django的后端(是否有帮助)

回答

0

不,后端的iframe大小不可用。

当您在iframe中显示页面而不是直接显示图像时,该页面可以使用客户端脚本获取iframe的大小,并将该信息一起发送到图像标记中的URL中。

喜欢的东西:

window.onload = function(){ 
    var b = document.body; 
    if (window.opera) b = b.parentNode; 
    var img = document.createElement('img'); 
    img.src = 'http://example.com/photos/embed/abc/?w=' + b.clientWidth + '&h=' + b.clientHeight; 
    img.alt = ''; 
    document.body.appendChild(img); 
} 

您可能需要这种风格的页面也,使机体元素占据了iframe的全高:

html,body { height: 100%; } 
+0

谢谢,这似乎是该地段最好的方法。 – 2011-05-04 07:12:15

2

在你的榜样页/脚本[http://example.com/photos/embed/abc/]不能得到宽度/高度的值。 但是你可以通过宽度/高度变量在URL像

<iframe type="text/html" src="http://example.com/photos/embed/abc/?width=640&height=480" width="640px" height="480px" frameborder="1"> </iframe>

+0

谢谢你,是的,我有已经考虑过这种方法。唯一的问题是,如果用户在URL和iframe属性中指定了不同的值,这可能会导致图像大小和iframe大小不同步。 任何方法来解决这个问题? – 2011-05-04 06:48:28

+0

如果您坚持使用iframe,那么我担心这是与/ abc/page进行通信的唯一方法。 – 0xAli 2011-05-04 07:05:51

0

你可以使用JavaScript(演示是在jQuery的跨浏览器易于实现的)检查iframe元素并复制它的大小属性为iframe请求的网址:以下内容将通过延迟iframe的网址设置直到宽度和高度确定为止。

<script type="text/javascript"> 

$(document).ready(function() { 
    var frame = $('#myiframeid'); 
    var width = frame.attr('width'); 
    var height = frame.attr('height'); 
    var url = 'http://example.com/photos/embed/abc/?w=' + width + '&h=' + height; 

    frame.attr('src', url); 
}); 

</script> 

<iframe id="myiframeid" type="text/html" src="myemptyblankpage.html" width="640px" height="480px" frameborder="1"> </iframe> 

希望有帮助。

+0

这也是一个好方法,但我觉得这将是太多的代码供用户嵌入。给用户只是“