在Chrome中获取iframe文档

Getting iframe document in Chrome

本文关键字:iframe 文档 获取 Chrome      更新时间:2023-09-26

我正试图根据iframe的内容动态更改它的高度。

然而,它在最新版本的chrome中不起作用。

doc是铬的'undefined'

它在Firefox中运行良好。

我做错了什么?

<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>
不过,

似乎对我有用。看看这把小提琴。

一些不应该真正改变任何事情的小变化:

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