简体   繁体   中英

Detect scrollbars within iFrame JS

My webpage has an embedded iFrame which is using a 3rd party tool within my webpage which means the URL from the iFrame is coming from a different location.

I need to detect the presentation of a scroll bar within the iFrame window when resizing the page, then carrying out a task once it has been detected.

I have tried a variety of different solutions all which have not been successful.

Is this possible?

Many Thanks!

This is the first thing that comes to my mind: http://jsfiddle.net/matias/hhcKn/

just sample code!

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;
}
<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM