質問するログイン新規登録

質問編集履歴

2

親ページのサンプルコードが「cotentWindow」になっていたので修正しました。

2017/06/21 07:37

投稿

nao84
nao84

スコア23

title CHANGED
File without changes
body CHANGED
@@ -13,7 +13,7 @@
13
13
  (親)
14
14
  ```javascript
15
15
  var message = 'hogefuga';
16
- cotentWindow.postMessage(message,"https://B.jp");
16
+ contentWindow.postMessage(message,"https://B.jp");
17
17
  ```
18
18
 
19
19
  (子)
@@ -51,6 +51,6 @@
51
51
  //念のためjQueryがあることを確認しつつ実行
52
52
  if(typeof jQuery !== "undifined"){
53
53
  var message = 'hogefuga';
54
- cotentWindow.postMessage(message,"https://B.jp");
54
+ contentWindow.postMessage(message,"https://B.jp");
55
55
  }
56
56
  ```

1

ご質問のご返答をどこに書いたらいいのかわからないため、メインの投稿を修正いたしました。

2017/06/21 07:37

投稿

nao84
nao84

スコア23

title CHANGED
File without changes
body CHANGED
@@ -27,4 +27,30 @@
27
27
  alert('e.data:'+e.data);//hogefuga がアラートされることを期待
28
28
  }
29
29
  });
30
+ ```
31
+
32
+ ★追記★
33
+
34
+ 親ページのconsoleには、
35
+
36
+ > Uncaught TypeError: Cannot read property 'postMessage' of undefined
37
+ > at <anonymous>:1:311
38
+ > at dispatch (jquery.min.js:3)
39
+ > at i (jquery.min.js:3)
40
+
41
+ と出ています。
42
+ (jQueryが読まれる前に処理が走ってしまっているからかと思い、
43
+ 念のため下記の様に修正してみましたが、やはりエラーが生じます。)
44
+
45
+ 親ページ
46
+ ```javascript
47
+ //jQuery呼び出し前に処理することを避ける。
48
+ if(typeof jQuery == "undifined"){
49
+ document.write("script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'><\/script>");
50
+ }
51
+ //念のためjQueryがあることを確認しつつ実行
52
+ if(typeof jQuery !== "undifined"){
53
+ var message = 'hogefuga';
54
+ cotentWindow.postMessage(message,"https://B.jp");
55
+ }
30
56
  ```