js読み込みの際はnullになるが、devツールのコンソールではnullにならない
解決ソース
"use strict"; (function () { let iframes = document.querySelectorAll("iframe"); iframes[0].onload = function(){ let dropzoneFrame = document.querySelector('#clientframe'); let dFWindow = dropzoneFrame.contentWindow; let allElements = dFWindow.document.body.querySelectorAll('*'); console.log(dFWindow); } })();
解決後の追記
新たに質問を立てました.なぜ即実行関数とhtmlタグ指定の組み合わせの時だけ動くのか
上記のベストアンサーで気づいたことを追記します.
この質問の該当ソースに載せ忘れたiframeで読み込んでいるsandbox.html
でもapplication.js
を読み込んでいました.エラーの正体はこいつでした.
実現したいこと
ファイルの読み込みの際も下記のエラーが出ないようにしたいです。
どうすれば良いかご教示お願いいたします。
Cannot read property 'contentWindow' of null
発生している問題
idがclientframeのiframeを読み込むために、contentWindow
を使ったが、Cannot read property 'contentWindow' of null
エラーが発生する。
ブラウザのコンソールでdocument.querySelector('#clientframe').contentWindow;
を試してみるとしっかり値が返ってくる。
該当のソース
- application.js
lang:js
1"use strict"; 2window.onload = function () { 3 let dropzoneFrame = document.querySelector('#clientframe'); 4 let dFWindow = dropzoneFrame.contentWindow; 5 let allElements = dFWindow.document.body.querySelectorAll('*'); 6}
- index.html
lang:html
1<!DOCTYPE html> 2<html lang="ja"> 3 <head> 4 <meta charset="utf-8"> 5 <title>dnd</title> 6 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" 7 crossorigin="anonymous"> 8 </head> 9 <body> 10 <div class="container"> 11 <div class="row"> 12 <div class="col-md-3"></div> 13 <div class="col-md-6"> 14 <iframe id="clientframe" disablehistory="true" src="./sandbox.html" style="visibility: visible; width: 700px; height: 700px; transform-origin: 0px 0px 0px; transform: scale(0.535172);"></iframe> 15 </div> 16 </div> 17 </div> 18 <script src="./application.js"></script> 19 </body> 20</html>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/03/08 03:38