为什么无法访问设置了src或srcdoc属性的iframe文档?

时间:2019-03-15 16:12:16

标签: javascript html dom iframe

<html>
    <body>
    <!-- <iframe id="iff" srcdoc="<div>ok</div>"></iframe> -->
    <iframe id="iff" src="data:text/html;charset=utf-8,%3Cdiv%3Eok%3C%2Fdiv%3E"></iframe>
    <script>
        console.log(document.getElementById('iff').contentWindow.document)
    </script>
    </body>
</html>
  1. 我设置了srcdoc的{​​{1}}(也尝试过src),但是无法访问<iframe>的真实文档(也尝试过iframe)。 Chrome输出(也尝试使用Firefox): result

我在Get IFrame's document, from JavaScript in main document中看到我无法获得跨域iframe的文档,并且还从Which is the difference between srcdoc="..." and src="data:text/html,..." in an <iframe>?了解到iframeElement.contentDocumentsrc在跨域i行为上有所不同域行为。但是我都尝试了,都没有用。

2。当我将子项附加到iframe(设置了srcdoc属性的 )的文档正文中时,DOM会更新,但不会显示在屏幕上。

1 个答案:

答案 0 :(得分:0)

尝试类似的东西:

2019-03-16T06:17:44.493Z WARN    :HLFConnection            :_validatePeerResponses()  Response from attempted peer comms was an error: Error: REQUEST_TIMEOUT {}$
2019-03-16T06:17:44.493Z ERROR   :HLFConnection            :start()                   {"message":"No valid responses from any peers.\nResponse from attempted peer comms was an error: Error: REQUEST_TIMEOUT","stack":"Error: No valid responses from any peers.\nResponse from attempted peer comms was an error: Error: REQUEST_TIMEOUT\n    at HLFConnection._validatePeerResponses (/home/pavan/.nvm/versions/node/v8.15.1/lib/node_modules/composer-playground/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:855:19)\n    at HLFConnection._sendTransaction (/home/pavan/.nvm/versions/node/v8.15.1/lib/node_modules/composer-playground/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:778:37)\n    at HLFConnection.start (/home/pavan/.nvm/versions/node/v8.15.1/lib/node_modules/composer-playground/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:756:24)\n    at <anonymous>\n    at process._tickCallback (internal/process/next_tick.js:189:7)"}$
2019-03-16T06:17:44.494Z ERROR   :ConnectorServer          :connectionStart()         {"message":"Error trying to start business network. Error: No valid responses from any peers.\nResponse from attempted peer comms was an error: Error: REQUEST_TIMEOUT","stack":"Error: Error trying to start business network. Error: No valid responses from any peers.\nResponse from attempted peer comms was an error: Error: REQUEST_TIMEOUT\n    at HLFConnection.start (/home/pavan/.nvm/versions/node/v8.15.1/lib/node_modules/composer-playground/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:758:30)\n    at <anonymous>\n    at process._tickCallback (internal/process/next_tick.js:189:7)"}$

如果您使用网址查找iframe,这可能会很有用:

window.frames['iff'].document.myvar;