新しいウインドウを開く方法としては、アンカーから開く場合と window.open から開く場合に大きく分かれますが、前者のほうは開かれたほうから親ウインドウを参照可能(window.opener)ですが、親ウインドウから子ウインドウの参照ができないので、子ウインドウ側で、親ウインドウにウインドウを引き渡す必要があります。
※ Window を閉じる場合、IE だけ確認ダイアログが出るのでそれを回避するコードを実装しています
▼ 親ウインドウ
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>リンクから開いたウインドウのアクセス(親)</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
// ▼ この変数に、開いたウインドウから値をセットします
var childWindow = null;
var userAgent = window.navigator.userAgent.toLowerCase();
// ***********************************************
// 画面初期化後のイベント( jQuery )
// ***********************************************
$(function() {
$("#closeButton")
.attr("type","button")
.val("閉じる")
.click(function(){
if (userAgent.indexOf("msie") > -1 || userAgent.indexOf("trident/7.0") > -1 ) {
(childWindow.open("","_self")).close();
}
else {
childWindow.close();
}
});
});
</script>
</head>
<body>
<input id="closeButton">
<br><br>
<a href="link2winChild.php?id=childWindow" target="_blank">新しいウインドウを開く</a>
</body>
</html>
▼ 子ウインドウ( php です )
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>リンクから開いたウインドウのアクセス(子)</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
// ***********************************************
// 画面初期化後のイベント( jQuery )
// ***********************************************
$(function() {
window.opener.<?= $_GET['id'] ?> = window;
$("#closeButton")
.attr("type","button")
.val("閉じる")
.click(function(){
if (window.opener.userAgent.indexOf("msie") > -1 || window.opener.userAgent.indexOf("trident/7.0") > -1 ) {
(window.open("","_self")).close();
}
else {
window.close();
}
});
});
</script>
</head>
<body>
<input id="closeButton">
</body>
</html>
これに対して、window.open の場合は、相互に参照する事ができます。 ▼ 親ウインドウ
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>window.open から開いたウインドウのアクセス(親)</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
// ▼ この変数に、window.open の戻り値をセットします
var childWindow = null;
// ***********************************************
// 画面初期化後のイベント( jQuery )
// ***********************************************
$(function() {
$("#closeButton")
.attr("type","button")
.val("閉じる")
.click(function(){
childWindow.close();
});
$("#openContent")
.attr("href","#")
.click(function( event ) {
// メソッドでウインドウを開く
// status 以降はブラウザによって違います
childWindow = window.open(
"open2winChild.php",
null,
"height=400"+
",width=400"+
",top=0"+
",left="+(screen.width-400-20)+
",status=yes"+
",toolbar=no"+
",menubar=no"+
",location=no"
);
event.preventDefault();
});
});
</script>
</head>
<body>
<input id="closeButton">
<br><br>
<a id="openContent">新しいウインドウを開く</a>
</body>
</html>
▼ 子ウインドウ
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>window.open から開いたウインドウのアクセス(子)</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
// ***********************************************
// 画面初期化後のイベント( jQuery )
// ***********************************************
$(function() {
$("#closeButton")
.attr("type","button")
.val("閉じる")
.click(function(){
window.close();
});
});
</script>
</head>
<body>
<input id="closeButton">
</body>
</html>
また、window.open の拡張形として showModalDialog というメソッドに関していろいろ情報がありますが、結論として WEB ではいろいろ無理があるようです。今後は jQuert UI のメソッドを使うのが一番簡単な代替だと思いますが、ダイアログ内を別ウインドウとして管理したい場合は、IFRAME を使う事になると思います
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery ダイアログ</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
// ***********************************************
// 画面初期化後のイベント( jQuery )
// ***********************************************
$(function() {
$("#dialog1")
.attr("type","button")
.val("モーダルダイアログ")
.click(function(){
$( "#text-message" ).text("ダイアログ内部のメッセージです");
$( "#dialog-message" ).dialog({
modal: true,
title: "ダイアログのタイトルです",
close: function() {
console.log("x ボタンがクリックされました");
},
buttons: [
{
text: "確認",
click: function() {
$( this ).dialog( "close" );
console.log("確認 ボタンがクリックされました");
}
},
{
text: "キャンセル",
click: function() {
$( this ).dialog( "close" );
console.log("キャンセル ボタンがクリックされました");
}
}
]
});
});
$("#dialog2")
.attr("type","button")
.val("モーダルダイアログ(IFRAME 利用)")
.click(function(){
$( "#dialog-iframe" ).dialog({
modal: true,
title: "ダイアログのタイトルです",
width: 720,
close: function() {
console.log("x ボタンがクリックされました");
},
buttons: [
{
text: "確認",
click: function() {
$( this ).dialog( "close" );
console.log("確認 ボタンがクリックされました");
}
},
{
text: "キャンセル",
click: function() {
$( this ).dialog( "close" );
console.log("キャンセル ボタンがクリックされました");
}
}
]
});
});
});
</script>
</head>
<body>
<input id="dialog1">
<br><br>
<input id="dialog2">
<div id="dialog-message" title="" style='display:none;'>
<p id="text-message" style="font-weight:bold;color:#CB3232;">
</p>
</div>
<div id="dialog-iframe" title="" style='display:none;'>
<iframe
src="range.php"
name="iframe_win"
id="iframe_win"
frameborder="0"
scrolling="no"
width="680"
height="380"
style=''
></iframe>
</div>
</body>
</html>
一つ目のダイアログは、単純に DIV を使用したものです。二つ目は、その DIV の中に IFRAME で別のウインドウの情報を表示しています。
![]()
|
|
【jQueryの最新記事】
- clipboard.js で、2種類のクリップボード処理オブジェクトを作成して使い分ける
- 一般的な PHP のアップロードページに対して jQuery の $.ajax でファイルをアップロードする
- jQuery の $.ajax で JSON 文字列を WEB より取得して TABLE を作成する
- jQuery で DIV の中の DIV を縦横中央に配置する
- jQuery で ページ内の画像のリンク切れを探してテーブルに URL を表示する
- クリックした入力フィールド以外をブラックアウトする jQuery プラグイン
- FORM の内容を localStorage に保存する jQuery プラグイン
- tableMagic : 単純に JSON から TABLE、TR、TH、TR、TD を作成する jQuery のプラグイン
- 滑らかなスクロール(scroll bar)を提供する jQuery プラグイン jQuery.NiceScroll
- jQuery : F1 キーでヘルプを起動させないようにする
- jQuery プラグイン jquery.balloon.js に jQuery UI を追加してバルーン毎に違ったアニメーション効果を設定する
- jquery.balloon.js で、balloon をメッセージ表示に使う( ゲーム等で使う為 )
- jQuery プラグイン jquery.balloon.js で複数バルーンにそれぞれ別のコンテンツを設定する為には、.each を使う
- jQuery プラグイン jquery.balloon.js を使用する『画像バルーン』の処理 (Excel で作成した吹き出し)
- jQuery プラグイン jquery.balloon.js の外部からのコントロール
- NAVER : Image*After の城と教会編
- 親(parent) フレームに jQuery が無い場合に jQuery をインストールする。
- jQuery で書かれた、幻想的な動く星座のようなシーン
- jQuery の addClass で、CSS で作成するサイフの出来上がる過程を見れるようにしました
- jQuery/JavaScript でフルスクリーンAPI( スクロールしないし、こりゃあ、WebGL の為にあるような・・・ )







二つ目は、その DIV の中に IFRAME で別のウインドウの情報を表示しています。





















