admin管理员组文章数量:1022452
I got an iframe in my page. Like this :
<iframe width="600px">
<html>
<head>
</head>
<body>
<p>Some text</p>
<img src="/foo/bar/test.png" />
</body>
</html>
</iframe>
I want to be able to detect if the iframe contents is larger than 600px. I tried this one :
var iframe = $('iframe');
if (iframe.width() < iframe.contents().width()) {
console.log('contents larger than the iframe');
}
It works on Firefox and Internet Explorer. But not on Chrome. On Chrome, iframe.contents().width()
is 600.
I tried iframe.contents().find('body').width()
but the result is also 600.
How can i detect the iframe contents real width in Chrome ?
I got an iframe in my page. Like this :
<iframe width="600px">
<html>
<head>
</head>
<body>
<p>Some text</p>
<img src="/foo/bar/test.png" />
</body>
</html>
</iframe>
I want to be able to detect if the iframe contents is larger than 600px. I tried this one :
var iframe = $('iframe');
if (iframe.width() < iframe.contents().width()) {
console.log('contents larger than the iframe');
}
It works on Firefox and Internet Explorer. But not on Chrome. On Chrome, iframe.contents().width()
is 600.
I tried iframe.contents().find('body').width()
but the result is also 600.
How can i detect the iframe contents real width in Chrome ?
Share Improve this question asked Feb 4, 2013 at 14:44 MagusMagus 15.1k4 gold badges39 silver badges52 bronze badges 1- what does $('iframe').outerWidth() show? – Thariama Commented Feb 4, 2013 at 15:36
3 Answers
Reset to default 0I guess you want to get the iframe width of a tinymce iframe. You could give this code snippet a try. What still needs to be done is to set the caret to the outer right position. The idea is to measure the caret location.
var ed = tinymce.editors[0]; // or tinymce.get('your_editor_id')
var rng = ed.selection.getRng();
rng.collapse(true);
var bm = ed.selection.getBookmark();
var $marker = $(ed.getBody()).find('#'+bm.id);
var elem = ed.getDoc().getElementById(bm.id+'_start');
try {
box = elem.getBoundingClientRect();
}
catch(e)
{
console.log('adjustIframePosition (irbasics) error creating box: ' + e);
}
var doc = ed.getDoc(),
docElem = doc.documentElement,
body = ed.getBody(),
win = ed.getWin(),
clientTop = docElem.clientTop || body.clientTop || 0,
clientLeft = docElem.clientLeft || body.clientLeft || 0,
scrollTop = win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop,
scrollLeft = win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft,
top = box.top + scrollTop - clientTop,
left = box.left + scrollLeft - clientLeft;
console.log('iframe width: ' + (box.left + scrollLeft) );
Try adding width checking after loading iframe, like suggested here
it did the trick for me.
I don't use jQuery, but maybe can help this:
var iframe = document.getElementById("myiframe");
var doc = iframe.contentDocument || iframe.contentWindow.document;
alert(doc.body.scrollWidth); // use this property for check
I got an iframe in my page. Like this :
<iframe width="600px">
<html>
<head>
</head>
<body>
<p>Some text</p>
<img src="/foo/bar/test.png" />
</body>
</html>
</iframe>
I want to be able to detect if the iframe contents is larger than 600px. I tried this one :
var iframe = $('iframe');
if (iframe.width() < iframe.contents().width()) {
console.log('contents larger than the iframe');
}
It works on Firefox and Internet Explorer. But not on Chrome. On Chrome, iframe.contents().width()
is 600.
I tried iframe.contents().find('body').width()
but the result is also 600.
How can i detect the iframe contents real width in Chrome ?
I got an iframe in my page. Like this :
<iframe width="600px">
<html>
<head>
</head>
<body>
<p>Some text</p>
<img src="/foo/bar/test.png" />
</body>
</html>
</iframe>
I want to be able to detect if the iframe contents is larger than 600px. I tried this one :
var iframe = $('iframe');
if (iframe.width() < iframe.contents().width()) {
console.log('contents larger than the iframe');
}
It works on Firefox and Internet Explorer. But not on Chrome. On Chrome, iframe.contents().width()
is 600.
I tried iframe.contents().find('body').width()
but the result is also 600.
How can i detect the iframe contents real width in Chrome ?
Share Improve this question asked Feb 4, 2013 at 14:44 MagusMagus 15.1k4 gold badges39 silver badges52 bronze badges 1- what does $('iframe').outerWidth() show? – Thariama Commented Feb 4, 2013 at 15:36
3 Answers
Reset to default 0I guess you want to get the iframe width of a tinymce iframe. You could give this code snippet a try. What still needs to be done is to set the caret to the outer right position. The idea is to measure the caret location.
var ed = tinymce.editors[0]; // or tinymce.get('your_editor_id')
var rng = ed.selection.getRng();
rng.collapse(true);
var bm = ed.selection.getBookmark();
var $marker = $(ed.getBody()).find('#'+bm.id);
var elem = ed.getDoc().getElementById(bm.id+'_start');
try {
box = elem.getBoundingClientRect();
}
catch(e)
{
console.log('adjustIframePosition (irbasics) error creating box: ' + e);
}
var doc = ed.getDoc(),
docElem = doc.documentElement,
body = ed.getBody(),
win = ed.getWin(),
clientTop = docElem.clientTop || body.clientTop || 0,
clientLeft = docElem.clientLeft || body.clientLeft || 0,
scrollTop = win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop,
scrollLeft = win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft,
top = box.top + scrollTop - clientTop,
left = box.left + scrollLeft - clientLeft;
console.log('iframe width: ' + (box.left + scrollLeft) );
Try adding width checking after loading iframe, like suggested here
it did the trick for me.
I don't use jQuery, but maybe can help this:
var iframe = document.getElementById("myiframe");
var doc = iframe.contentDocument || iframe.contentWindow.document;
alert(doc.body.scrollWidth); // use this property for check
本文标签: javascriptHow to get an iframe contents widthStack Overflow
版权声明:本文标题:javascript - How to get an iframe contents width? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745573652a2156888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论