检测iFrame JS中的滚动条

时间:2013-04-03 11:01:40

标签: javascript jquery html iframe

我的网页有一个嵌入式iFrame,它在我的网页中使用了第三方工具,这意味着来自iFrame的网址来自不同的位置。

我需要在调整页面大小时检测iFrame窗口中滚动条的显示,然后在检测到任务后执行任务。

我尝试了各种不同的解决方案,但都没有成功。

这可能吗?

非常感谢!

2 个答案:

答案 0 :(得分:1)

这是我想到的第一件事:http://jsfiddle.net/matias/hhcKn/

只是示例代码!

HTML:

<div id="body"></div>

JS:

var $iframe = $("<iframe></iframe>").appendTo("#body");
var iframe = $iframe[0];
var doc = iframe.document;

//test this
var content = "<h1>Hello world</h1><br><p>more content!</p>";

//and then this
//var content = "<h1>Hello world</h1><br><br><br><br><br><p>more content!</p>";

if(iframe.contentDocument){
    doc = iframe.contentDocument;
}
else if(iframe.contentWindow){
    doc = iframe.contentWindow.document;
}
doc.open();
doc.writeln(content);
doc.close();


//this is the part that might interest you
var div_height = $("#body").height();
var iframe_height = $("iframe").contents().height();

if(iframe_height > div_height) alert("scrollbars present");

CSS:

body{
    border: 1px solid red;
}

iframe{
    border: none;
}

答案 1 :(得分:1)

<iframe src="111.html" id="iframeid" height="300" width="800"></iframe>

<script type="text/javascript">
function resizeIFrame(){
    $("#iframeid").attr("height", $("#iframeid").contents().height());
}
setInterval("resizeIFrame()", 500)
</script>