同源iframe内容返回undefined

时间:2016-01-26 16:05:43

标签: javascript html iframe undefined

我试图访问我通过用户脚本创建的同源iframe。生成iframe后,HTML看起来像(截断src):

<table id="addUsers">
    <tr id="auBody">
        <td id="iframeCell">
            <iframe id="searchArea" src="/webapps/blackboard/execute/userManager?..."></iframe>
        </td>        
    </tr>
</table>

无论我做什么,它的内容总是返回null或undefined。我甚至创建了一个测试按钮,使用以下监听器:

btn.addEventListener('click', function() {    
    var iframe = document.querySelector('#searchArea');
    console.log('--TESTING IFRAME--');
    console.log(':: SRC: ' + iframe.getAttribute('src').substring(0, 40) + '...');
    iframe.contentWindow ?
        console.log(':: iframe.contentWindow exists with length: ' + iframe.contentWindow.length) :
        console.error(':: iframe.contentWindow does not exist.')
    ;
    iframe.contentWindow.document ?
        console.log(':: iframe.contentWindow.document exists with length: ' + iframe.contentWindow.document.length) :
        console.error(':: iframe.contentWindow.document does not exist.')
    ;
    iframe.contentDocument ?
        console.log(':: iframe.contentDocument exists with length: ' + iframe.contentDocument.length) :
        console.error(':: iframe.contentDocument does not exist.')
    ;
});

加载iframe后,我点击按钮并获得此输出:

--TESTING IFRAME--
:: SRC: /webapps/blackboard/execute/userManager?...
:: iframe.contentWindow exists with length: 0
:: iframe.contentWindow.document exists with length: undefined
:: iframe.contentDocument exists with length: undefined

我做错了什么?我总是能够毫不费力地在jQuery中做到这一点,但我仍然试图更加重视香草JS。

0 个答案:

没有答案
相关问题