jqueryで親ウィンドウに値を渡す

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jqueryで親ウィンドウに値を渡す</title>
  6. <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
  7. <script type="text/javascript">
  8. // サブウィンドウを開く処理
  9. function disp(url){
  10.   window.open(url, "window_name", "width=250,height=350,scrollbars=yes,resizable=yes,status=yes");
  11. }
  12. </script>
  13. </head>
  14. <body>
  15. <h1>「jqueryで親ウィンドウに値を渡す」の親ウィンドウ側</h1>
  16. <form name="hoge_form">
  17. <textarea name="hoge_textarea" class="hoge_textarea"></TEXTAREA>
  18. </form>
  19. <p><a href="jquery006_passing_value_child.html" target="window_name" onClick="disp('jquery006_passing_value_child.html')">子ウィンドウを開く</a></p>
  20. <hr>
  21. ちなみに子ウィンドウのHTMLソースは下記。
  22. <div style="border:1px solid #ccc;margin-top:1em;padding:0.5em;">
  23. <pre style="margin:0;">
  24. &lt;!DOCTYPE html>
  25. &lt;html lang="ja">
  26. &lt;head>
  27. &lt;meta charset="UTF-8">
  28. &lt;title>「jqueryで親ウィンドウに値を渡す」の子ウィンドウ側&lt;/title>
  29. &lt;script type="text/javascript" src="jquery-1.5.1.min.js">&lt;/script>
  30. &lt;script type="text/javascript">
  31.   function disp(MsgText){
  32.     if(!window.opener || window.opener.closed)
  33.     {
  34.       // メインウィンドウの存在をチェック
  35.       // 存在しない場合は警告ダイアログを表示
  36.       window.alert('メインウィンドウがありません');
  37.     }
  38.     else
  39.     {
  40.       window.opener.$(".hoge_textarea").val(MsgText);
  41.     }
  42.   }
  43. &lt;/script>
  44. &lt;/head>
  45. &lt;body>
  46. &lt;h1 style="font-size:0.8em;">「jqueryで親ウィンドウに値を渡す」の子ウィンドウ側&lt;/h1>
  47. &lt;a href="" onclick="disp('「諦める!」 それも勇気だ!')">メッセージ1&lt;/a>&lt;br />
  48. &lt;a href="" onclick="disp('夜なのに 僕が見るのは あおいそら')">メッセージ2&lt;/a>&lt;br />
  49. &lt;/body>
  50. &lt;/html>
  51. </pre>
  52. </div>
  53. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  54. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  55. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  56. </div>
  57. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  58. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  59. </div>
  60. </body>
  61. </html>