20130331【Java Script】サブウィンドウから呼び出し元の情報を取得する

お題

サブウィンドウから呼び出し元の情報を取得する

 

■呼び出し元

<html>
<head>
<script>
function winOpen(){
  window.open("sub.html","sub");
}
</script>
</head>
<body>
<form name="sample">
<input type="button" value="サンプル" onclick="winOpen()">
<input type="hidden" name="num" value="123">
<input type="hidden" name="str" value="string">
</form>
</body>
</html>

■sub.html

<html>
<head>
</head>
<body>
<script>

var num=window.opener.document.sample.num.value;
var str=window.opener.document.sample.str.value;

document.write(num+"<br>");
document.write(str+"<br>");
</script>
</body>
</html>

 

sub.html出力結果

123
string

 

<ポイント>

window.opener.document.form名.input名.valueオープン元のフォームの情報を取得する