最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - auto height for the <object> element with the embedded content - Stack Overflow

programmeradmin0浏览0评论

I am trying to get the auto height for the <object/> element according to the embedded content inside. Is this possible?

An example of the issue is here: JSFiddle. Here In Chrome, it seems the auto is ignored. How can I fix this?

HTML

<body>
    <object id="my-object" data=".txt"></object>
</body>

CSS

#my-object {
    height : auto;
    width : 300px;
}

EDIT I want to make the object as big as its content, so as to avoid the scroll of object element. It should be page scroll instrad of object elements scroll.

I am trying to get the auto height for the <object/> element according to the embedded content inside. Is this possible?

An example of the issue is here: JSFiddle. Here In Chrome, it seems the auto is ignored. How can I fix this?

HTML

<body>
    <object id="my-object" data="http://www.rfc-editor/rfc/rfc1.txt"></object>
</body>

CSS

#my-object {
    height : auto;
    width : 300px;
}

EDIT I want to make the object as big as its content, so as to avoid the scroll of object element. It should be page scroll instrad of object elements scroll.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked May 22, 2014 at 7:27 Manu K MohanManu K Mohan 8334 gold badges9 silver badges29 bronze badges 6
  • 1 btw, you don't have to declaire height for auto. default value of height IS auto. you try to make the object as big as the content ? so there is nothing to scroll ??? – Dwza Commented May 22, 2014 at 7:32
  • @Dwza That is my problem, I want to make the object as big as its content, so as to avoid the scroll. – Manu K Mohan Commented May 22, 2014 at 7:48
  • Well, this isn't a good way but it works. If you inspect the puted style of the object, you'll see that it has height:16155px and so you can add some fixed height to the object like 17000px to remove the scrollbar - JSFiddle – Vucko Commented May 22, 2014 at 7:59
  • @Vucko Can we make it dynamic? I mean if I want to dynamically change the data-url. How will I get the height? – Manu K Mohan Commented May 22, 2014 at 8:39
  • Vucko just gave the answer for you if you can use JS you need to get puted height please check jQuery outerHeight if you cannot use jQuery look at Window.getComputedStyle() also look browser patibility. The only problem is when you should get it check if object has onload/success event if it has you are pretty much done. – Onur Topal Commented May 22, 2014 at 9:11
 |  Show 1 more ment

3 Answers 3

Reset to default 0

it must be run under a server

with "contentDocument()"

<object id="my-object" type="text/plain" data="newfile.txt">
</object>
<script>
    var object = document.getElementById("my-object");
    object.onload = function () {
        var objectPre = object.contentDocument.body.childNodes[0];
        object.style.height = (objectPre.offsetHeight+20) + "px";
    };
</script>

with ajax do a search on loading the txt file using ajax

working fiddle (100% height, min-height and overflow hidden to body and html elements )

http://jsfiddle/DmF6B/2/

html, body{
   overflow:hidden;
   height: 100%;
   min-height: 100%;
}
    #my-object {
        height : 100%;
        width : 100%;
        overflow : hidden;
    }

html

<body scroll="no">
    <object id="my-object" data="http://www.rfc-editor/rfc/rfc1.txt">
    </object>
</body>

if it is in the same domain use the javascript function "contentDocument()"

try this instead : ajax

发布评论

评论列表(0)

  1. 暂无评论