简体   繁体   中英

Getting iframe document in Chrome

I am trying to dynamically change the heigh of a iframe depending on it's contents.

However it's not working in the latest version of chrome.

doc is 'undefined' in chrome.

It works fine in Firefox.

What am I doing wrong?

<script>
    $(function() {

        $('#iframeid')
                .load(
                        function() {

                            try {
                                var doc = this.contentDocument ? this.contentDocument
                                        : this.contentWindow.document;
                                alert(doc);
                            } catch (e) {
                                alert(e.message);
                            }
                        });

    });
</script>
<iframe frameborder="0" id="iframeid" src="iframesource2.html"
    height="1px"> </iframe>

Seems to work for me, though. Have a look at this fiddle .

Some little changes which should not really change anything:

$(function () {
    $('#iframeid').load(function () {
        try {
            var doc = this.contentDocument || this.contentWindow.document;
            alert(doc);
        } catch (e) {
            alert(e.message);
        }
    });
});

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