【父網頁】
<javascript>
<script>
// 以Post的方式傳值
// url: 開的視窗網址
// name: 開的視窗名稱(可有可無)
// targetid: 回傳後要顯示在哪個元件上
// keys、values: 均以 ['key1','key2']、['value1','value2'] 成對表示
function openWindowWithPost(url, name, targetid, keys, values) {
_targetid = targetid;
var newWindow = window.open(url, name, "width=" + 600 + "px,height=" + 400 + "px,left=" + 0 + ",top=" + 0);
//同时将焦点事件绑定,目的是,当点击父窗口时,如果子窗口尚未关闭,那依然回到子窗口,做到类似模式窗口的效果
window.onfocus = function () { if (newWindow.closed == false) { newWindow.focus(); }; };
var html = "";
html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";
if (keys && values && (keys.length == values.length)) {
for (var i = 0; i < keys.length; i++) {
html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
}
}
html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()<\/script></body></html>";
newWindow.document.write(html);
return newWindow;
}
// 子網頁回傳回來的值在這邊處理
function handleReturnValue(val) {
$("#" + _targetid)[0].value = val.join();
}
</script>
<html>
<input type="text" id="returnData" >
<input type="button" value="開子視窗" onclick="openWindowWithPost('子視窗的網頁','NewWindowName','returnData',['memberID'],['mem1','mem2'])" />
【子網頁】
<javascript>
<script type="text/javascript">
// 判斷瀏覽器種類
function getBrowserType() {
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
if (window.ActiveXObject)
Sys.ie = ua.match(/msie ([\d.]+)/)[1]
else if (document.getBoxObjectFor)
Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
else if (window.MessageEvent && !document.getBoxObjectFor)
Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1]
else if (window.opera)
Sys.opera = ua.match(/opera.([\d.]+)/)[1]
else if (window.openDatabase)
Sys.safari = ua.match(/version\/([\d.]+)/)[1];
if (Sys.ie) return "IE";
if (Sys.chrome) return 'Chrome';
if (Sys.firefox) return "Firefox";
if (Sys.opera) return "Opera";
if (Sys.safari) return "Safari";
return "IE";
}
// 當關閉子網頁後,要做的動作
function closeWindow() {
// val: 為要回傳給父網頁的物件(或值)
var val = $("#cbxMembers input:checked").map(function(){
return $(this).val();
}).get(); // 回傳父視窗的值
if ("IE" == getBrowserType())
self.returnValue = val; //IE:直接回傳
else
window.opener.handleReturnValue(val); //非IE,調用父視窗的函數來回傳值
self.close();
}
</script>
<html>
<input type="button" value="確定"<input type="button" name="button1" value="確定" onclick="closeWindow();" />
參考:
http://www.weibo.com/p/230418671432cf0102v6rf?pids=Pl_Official_CardMixFeed__4&feed_filter=1
http://www.blogjava.net/kait/archive/2011/05/27/351138.html
沒有留言:
張貼留言